KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > mapping > AttributePattern


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.mapping;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.regex.Matcher JavaDoc;
31 import java.util.regex.Pattern JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34
35 import org.riotfamily.common.util.FormatUtils;
36 import org.springframework.beans.PropertyAccessor;
37 import org.springframework.util.Assert;
38 import org.springframework.util.StringUtils;
39
40 public class AttributePattern {
41     
42     public static final String JavaDoc EXPOSED_ATTRIBUTES =
43             AttributePattern.class.getName() + ".exposedAttributes";
44
45     private static final Pattern JavaDoc ATTRIBUTE_NAME_PATTERN =
46             Pattern.compile("@\\{(.+?)((\\*)|\\:(.*?))?\\}");
47
48     private static final Pattern JavaDoc STAR_PATTERN =
49             Pattern.compile("\\\\\\*");
50
51     private static final Pattern JavaDoc DOUBLE_STAR_PATTERN =
52             Pattern.compile("\\\\\\*\\\\\\*");
53
54     private String JavaDoc attributePattern;
55     
56     private Pattern JavaDoc pattern;
57
58     private ArrayList JavaDoc attributeNames;
59     
60     private ArrayList JavaDoc attributeTypes;
61
62     public AttributePattern(String JavaDoc attributePattern) {
63         this.attributePattern = attributePattern;
64         attributeNames = new ArrayList JavaDoc();
65         attributeTypes = new ArrayList JavaDoc();
66         Matcher JavaDoc m = ATTRIBUTE_NAME_PATTERN.matcher(attributePattern);
67         while (m.find()) {
68             attributeNames.add(m.group(1));
69             attributeTypes.add(m.group(4));
70         }
71         pattern = Pattern.compile(convertAttributePatternToRegex(attributePattern));
72     }
73
74     public int getNumberOfWildcards() {
75         return attributeNames.size();
76     }
77     
78     // Example pattern: /resources/*/@{resource*}
79
private String JavaDoc convertAttributePatternToRegex(final String JavaDoc antPattern) {
80         String JavaDoc regex = FormatUtils.escapeChars(antPattern, "()", '\\'); // ... just in case
81
regex = ATTRIBUTE_NAME_PATTERN.matcher(antPattern).replaceAll("(*$3)"); // /resources/*/(**)
82
regex = "^" + FormatUtils.escapeChars(regex, ".+*?{^$", '\\') + "$"; // ^/resources/\*/(\*\*)$
83
regex = DOUBLE_STAR_PATTERN.matcher(regex).replaceAll(".*?"); // ^/resources/\*/(.*?)$
84
regex = STAR_PATTERN.matcher(regex).replaceAll("[^/]*"); // ^/resources/[^/]*/.*?$
85
return regex;
86     }
87
88     public void expose(String JavaDoc urlPath, HttpServletRequest JavaDoc request) {
89         Map JavaDoc attributes = new HashMap JavaDoc();
90         Matcher JavaDoc m = pattern.matcher(urlPath);
91         Assert.isTrue(m.matches());
92         for (int i = 0; i < getNumberOfWildcards(); i++) {
93             String JavaDoc s = m.group(i + 1);
94             String JavaDoc type = (String JavaDoc) attributeTypes.get(i);
95             String JavaDoc name = (String JavaDoc) attributeNames.get(i);
96             Object JavaDoc value = convert(s, type);
97             request.setAttribute(name, value);
98             attributes.put(name, value);
99         }
100         request.setAttribute(EXPOSED_ATTRIBUTES, attributes);
101     }
102
103     private Object JavaDoc convert(String JavaDoc s, String JavaDoc type) {
104         if (type == null || type.equalsIgnoreCase("String")) {
105             return s;
106         }
107         if (type.equalsIgnoreCase("Integer")) {
108             return Integer.valueOf(s);
109         }
110         else if (type.equalsIgnoreCase("Long")) {
111             return Long.valueOf(s);
112         }
113         else if (type.equalsIgnoreCase("Short")) {
114             return Short.valueOf(s);
115         }
116         else if (type.equalsIgnoreCase("Double")) {
117             return Double.valueOf(s);
118         }
119         else if (type.equalsIgnoreCase("Float")) {
120             return Float.valueOf(s);
121         }
122         else if (type.equalsIgnoreCase("Boolean")) {
123             return Boolean.valueOf(s);
124         }
125         else if (type.equalsIgnoreCase("Character")) {
126             return new Character JavaDoc(s.charAt(0));
127         }
128         else {
129             throw new IllegalArgumentException JavaDoc("Unsupported type: " + type
130                     + " - must be Integer, Long, Short, Double, Float,"
131                     + " Boolean or Character");
132         }
133     }
134     public boolean matches(Map JavaDoc attributes) {
135         if (attributes != null) {
136             Collection JavaDoc names = attributes.keySet();
137             return names.size() == getNumberOfWildcards() &&
138                 attributeNames.containsAll(names);
139         }
140         else {
141             return attributeNames.isEmpty();
142         }
143     }
144
145     public String JavaDoc fillInAttributes(PropertyAccessor attributes) {
146         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
147         Matcher JavaDoc m = ATTRIBUTE_NAME_PATTERN.matcher(attributePattern);
148         while (m.find()) {
149             String JavaDoc name = m.group(1);
150             Object JavaDoc value = attributes.getPropertyValue(name);
151             if (value == null) {
152                 return null;
153             }
154             String JavaDoc replacement = StringUtils.replace(value.toString(), "$", "\\$");
155             m.appendReplacement(url, replacement);
156         }
157         m.appendTail(url);
158         return url.toString();
159     }
160     
161     public String JavaDoc fillInAttribute(Object JavaDoc value) {
162         Assert.state(getNumberOfWildcards() == 1,
163                 "Pattern must contain exactly one wildcard.");
164         
165         String JavaDoc replacement = StringUtils.replace(value.toString(), "$", "\\$");
166         Matcher JavaDoc m = ATTRIBUTE_NAME_PATTERN.matcher(attributePattern);
167         return m.replaceFirst(replacement);
168     }
169     
170     public String JavaDoc toString() {
171         return attributePattern;
172     }
173     
174     public static String JavaDoc convertToAntPattern(String JavaDoc urlPattern) {
175         return ATTRIBUTE_NAME_PATTERN.matcher(urlPattern).replaceAll("*$3");
176     }
177
178 }
Popular Tags