Skip to content

Commit 3f03653

Browse files
committed
Initial version
0 parents  commit 3f03653

11 files changed

Lines changed: 639 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.classpath
2+
.project
3+
.settings
4+
.DS_Store
5+
dependency-reduced-pom.xml
6+
target/
7+
*.iml
8+
*.ipr
9+
*.idea
10+
*.iws
11+
*.*~
12+
bin/
13+
/*/src/main/java/META-INF
14+
/*/src/META-INF
15+
.gitignore.swp
16+
.faces-config.xml.jsfdia
17+
docs/reference/src/main/docbook/en-US/version_info.xml

‎pom.xml‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>org.ocpsoft</groupId>
7+
<artifactId>urlbuilder</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<repositories>
10+
<repository>
11+
<id>JBOSS_NEXUS</id>
12+
<url>http://repository.jboss.org/nexus/content/groups/public</url>
13+
</repository>
14+
</repositories>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.10</version>
21+
</dependency>
22+
</dependencies>
23+
24+
<build>
25+
<finalName>urlbuilder</finalName>
26+
<plugins>
27+
<plugin>
28+
<artifactId>maven-compiler-plugin</artifactId>
29+
<version>2.3.2</version>
30+
<configuration>
31+
<source>1.6</source>
32+
<target>1.6</target>
33+
<encoding>UTF-8</encoding>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.ocpsoft.urlbuilder;
2+
3+
/**
4+
* Immutable Internet address.
5+
*
6+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
7+
*/
8+
public interface Address
9+
{
10+
11+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package org.ocpsoft.urlbuilder;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedHashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import org.ocpsoft.urlbuilder.util.CaptureType;
9+
import org.ocpsoft.urlbuilder.util.CapturingGroup;
10+
import org.ocpsoft.urlbuilder.util.ParseTools;
11+
12+
public class AddressBuilder implements Address
13+
{
14+
private volatile CharSequence protocol;
15+
private volatile CharSequence host;
16+
private volatile Integer port;
17+
private volatile CharSequence path;
18+
private Map<CharSequence, List<Object>> parameters = new LinkedHashMap<CharSequence, List<Object>>();
19+
private Map<CharSequence, List<Object>> queries = new LinkedHashMap<CharSequence, List<Object>>();
20+
21+
private AddressBuilder()
22+
{}
23+
24+
public static AddressBuilder begin()
25+
{
26+
return new AddressBuilder();
27+
}
28+
29+
public Address build()
30+
{
31+
return this;
32+
}
33+
34+
public AddressBuilder protocol(String protocol)
35+
{
36+
this.protocol = protocol;
37+
return this;
38+
}
39+
40+
public AddressBuilder host(CharSequence host)
41+
{
42+
this.host = host;
43+
return this;
44+
}
45+
46+
public AddressBuilder port(int port)
47+
{
48+
this.port = port;
49+
return this;
50+
}
51+
52+
public AddressBuilder path(CharSequence path)
53+
{
54+
this.path = path;
55+
return this;
56+
}
57+
58+
public AddressBuilder query(CharSequence name, Object... values)
59+
{
60+
this.queries.put(name.toString(), Arrays.asList(values));
61+
return this;
62+
}
63+
64+
public AddressBuilder set(CharSequence name, Object... values)
65+
{
66+
this.parameters.put(name.toString(), Arrays.asList(values));
67+
return this;
68+
}
69+
70+
@Override
71+
public String toString()
72+
{
73+
StringBuilder result = new StringBuilder();
74+
75+
if (isSet(protocol))
76+
result.append(parameterize(protocol)).append("://");
77+
78+
if (isSet(host))
79+
result.append(parameterize(host));
80+
81+
if (isSet(port))
82+
{
83+
result.append(":").append(port);
84+
}
85+
86+
if (isSet(path))
87+
{
88+
if (path.charAt(0) != '/')
89+
result.append('/');
90+
result.append(parameterize(path));
91+
}
92+
93+
if (!queries.isEmpty())
94+
{
95+
result.append('?');
96+
boolean first = true;
97+
for (CharSequence name : queries.keySet()) {
98+
List<Object> values = queries.get(name);
99+
for (Object value : values) {
100+
101+
if (!first)
102+
result.append('&');
103+
else
104+
first = false;
105+
106+
result.append(name).append('=').append(value);
107+
}
108+
}
109+
}
110+
111+
return result.toString();
112+
}
113+
114+
private CharSequence parameterize(CharSequence sequence)
115+
{
116+
StringBuilder result = new StringBuilder();
117+
int cursor = 0;
118+
int lastEnd = 0;
119+
while (cursor < sequence.length())
120+
{
121+
switch (sequence.charAt(cursor))
122+
{
123+
case '{':
124+
result.append(sequence.subSequence(lastEnd, cursor));
125+
126+
int startPos = cursor;
127+
CapturingGroup group = ParseTools.balancedCapture(sequence, startPos, sequence.length() - 1,
128+
CaptureType.BRACE);
129+
cursor = group.getEnd();
130+
lastEnd = group.getEnd() + 1;
131+
132+
String name = group.getCaptured().toString();
133+
134+
List<Object> value = parameters.get(name);
135+
if (value == null || value.isEmpty())
136+
throw new IllegalStateException("No parameter [" + name + "] was set in the pattern [" + sequence
137+
+ "]. Call address.set(\"" + name + "\", value); or remove the parameter from the pattern.");
138+
139+
result.append(value.get(0));
140+
141+
break;
142+
143+
default:
144+
break;
145+
}
146+
147+
cursor++;
148+
}
149+
150+
if (cursor >= lastEnd)
151+
result.append(sequence.subSequence(lastEnd, cursor));
152+
return result;
153+
}
154+
155+
private boolean isSet(Integer port)
156+
{
157+
return port != null;
158+
}
159+
160+
private boolean isSet(CharSequence value)
161+
{
162+
return value != null && value.length() > 0;
163+
}
164+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2012 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.urlbuilder.util;
17+
18+
/**
19+
* Utility methods for making precondition/postcondition assertions.
20+
*
21+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
22+
*/
23+
public final class Assert
24+
{
25+
private Assert()
26+
{}
27+
28+
/**
29+
* Throw an exception if the given {@link Object} is not null.
30+
*/
31+
public static void isNull(final Object object, final String message) throws IllegalArgumentException
32+
{
33+
if (object != null)
34+
{
35+
throw new IllegalArgumentException(message);
36+
}
37+
}
38+
39+
/**
40+
* Throw an exception if the given {@link Object} is null.
41+
*/
42+
public static void notNull(final Object object, final String message) throws IllegalArgumentException
43+
{
44+
if (object == null)
45+
{
46+
throw new IllegalArgumentException(message);
47+
}
48+
}
49+
50+
/**
51+
* Throw an exception if the given value is not true.
52+
*/
53+
public static void assertTrue(final boolean value, final String message) throws IllegalArgumentException
54+
{
55+
if (value != true)
56+
{
57+
throw new IllegalArgumentException(message);
58+
}
59+
}
60+
61+
/**
62+
* Throw an exception if the given value is not false.
63+
*/
64+
public static void assertFalse(final boolean value, final String message) throws IllegalArgumentException
65+
{
66+
if (value != false)
67+
{
68+
throw new IllegalArgumentException(message);
69+
}
70+
}
71+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.ocpsoft.urlbuilder.util;
2+
3+
public enum CaptureType
4+
{
5+
BRACE('{', '}'), BRACKET('[', ']'), PAREN('(', ')'), REGEX('/', '/');
6+
7+
private char begin;
8+
private char end;
9+
10+
private CaptureType(final char begin, final char end)
11+
{
12+
this.begin = begin;
13+
this.end = end;
14+
}
15+
16+
public char getBegin()
17+
{
18+
return begin;
19+
}
20+
21+
public char getEnd()
22+
{
23+
return end;
24+
}
25+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.urlbuilder.util;
17+
18+
19+
/**
20+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
21+
*/
22+
public class CapturingGroup
23+
{
24+
private final CharSequence chars;
25+
private final int start;
26+
private final int end;
27+
28+
public CapturingGroup(final CharSequence path, final int start, final int end)
29+
{
30+
this.chars = path;
31+
this.start = start;
32+
this.end = end;
33+
}
34+
35+
public int getStart()
36+
{
37+
return start;
38+
}
39+
40+
public int getEnd()
41+
{
42+
return end;
43+
}
44+
45+
public CharSequence getCaptured()
46+
{
47+
return chars.subSequence(start + 1, end);
48+
}
49+
50+
@Override
51+
public String toString()
52+
{
53+
return "CapturingGroup [start=" + start + ", end=" + end + "]";
54+
}
55+
}

0 commit comments

Comments
 (0)