GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
UriQueryProps.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.common.net;
29
30import java.net.URISyntaxException;
31import java.util.HashMap;
32import java.util.Iterator;
33import java.util.Map;
34import java.util.Map.Entry;
35
36/**
37 * Helper class to process URI's query, handled as properties.
38 * <p>
39 * The order of the URI segments (any properties) are <i>not</i> preserved.
40 * </p>
41 * <pre>
42 * URI: [scheme:][//authority][path][?query][#fragment]
43 * w/ authority: [user-info@]host[:port]
44 * Note: 'path' starts w/ fwd slash
45 * </pre>
46 * <p>
47 * Since 2.3.0 renamed from {@code URIQueryProps} to {@code UriQueryProps},
48 * and using {@link Uri} instead of {@link java.net.URI}.
49 * </p>
50 */
51public class UriQueryProps {
52 private static final String QMARK = "?";
53 private static final char ASSIG = '=';
54 private static final String EMPTY = "";
55 private final String query_separator;
56
57 private final HashMap<String, String> properties = new HashMap<String, String>();
58
59 private UriQueryProps(final char querySeparator) {
60 query_separator = String.valueOf(querySeparator);
61 }
62
63 public final Map<String, String> getProperties() { return properties; }
64 public final char getQuerySeparator() { return query_separator.charAt(0); }
65
66 public final Uri.Encoded appendQuery(Uri.Encoded baseQuery) {
67 boolean needsSep = false;
68 final StringBuilder sb = new StringBuilder();
69 if ( null != baseQuery ) {
70 if( baseQuery.startsWith(QMARK) ) {
71 baseQuery = baseQuery.substring(1); // cut off '?'
72 }
73 sb.append(baseQuery.get());
74 if( !baseQuery.endsWith(query_separator) ) {
75 needsSep = true;
76 }
77 }
78 final Iterator<Entry<String, String>> entries = properties.entrySet().iterator();
79 while(entries.hasNext()) {
80 if(needsSep) {
81 sb.append(query_separator);
82 }
83 final Entry<String, String> entry = entries.next();
84 sb.append(entry.getKey());
85 if( EMPTY != entry.getValue() ) {
86 sb.append(ASSIG).append(entry.getValue());
87 }
88 needsSep = true;
89 }
90 return new Uri.Encoded(sb.toString(), Uri.QUERY_LEGAL);
91 }
92
93 public final Uri appendQuery(final Uri base) throws URISyntaxException {
94 return base.getNewQuery( appendQuery( base.query ) );
95 }
96
97 /**
98 *
99 * @param uri
100 * @param querySeparator should be either <i>;</i> or <i>&</i>, <i>;</i> is encouraged due to troubles of escaping <i>&</i>.
101 * @return
102 * @throws IllegalArgumentException if <code>querySeparator</code> is illegal, i.e. neither <i>;</i> nor <i>&</i>
103 */
104 public static final UriQueryProps create(final Uri uri, final char querySeparator) throws IllegalArgumentException {
105 if( ';' != querySeparator && '&' != querySeparator ) {
106 throw new IllegalArgumentException("querySeparator is invalid: "+querySeparator);
107 }
108 final UriQueryProps data = new UriQueryProps(querySeparator);
109 final String q = Uri.decode(uri.query);
110 final int q_l = null != q ? q.length() : -1;
111 int q_e = -1;
112 while(q_e < q_l) {
113 final int q_b = q_e + 1; // next term
114 q_e = q.indexOf(querySeparator, q_b);
115 if(0 == q_e) {
116 // single separator
117 continue;
118 }
119 if(0 > q_e) {
120 // end
121 q_e = q_l;
122 }
123 // n-part
124 final String part = q.substring(q_b, q_e);
125 final int assignment = part.indexOf(ASSIG);
126 if(0 < assignment) {
127 // assignment
128 final String k = part.substring(0, assignment);
129 final String v = part.substring(assignment+1);
130 data.properties.put(k, v);
131 } else {
132 // property key only
133 data.properties.put(part, EMPTY);
134 }
135 }
136 return data;
137 }
138}
Helper class to process URI's query, handled as properties.
final Uri.Encoded appendQuery(Uri.Encoded baseQuery)
final Uri appendQuery(final Uri base)
static final UriQueryProps create(final Uri uri, final char querySeparator)
final Map< String, String > getProperties()
Immutable RFC3986 encoded string.
Definition: Uri.java:296
This class implements an immutable Uri as defined by RFC 2396.
Definition: Uri.java:160
final Uri getNewQuery(final Encoded newQuery)
Returns a new Uri instance w/ the given new query newQuery.
Definition: Uri.java:1740
static String decode(final Encoded encoded)
Safe Encoded#decode() call on optional encoded instance.
Definition: Uri.java:572
static final String QUERY_LEGAL
Valid charset for RFC 2396 query, additional to legal alphanum characters.
Definition: Uri.java:253