Source Code Cross Referenced for URIRoute.java in  » J2EE » fleXive » com » flexive » faces » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » fleXive » com.flexive.faces 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /***************************************************************
002:         *  This file is part of the [fleXive](R) project.
003:         *
004:         *  Copyright (c) 1999-2007
005:         *  UCS - unique computing solutions gmbh (http://www.ucs.at)
006:         *  All rights reserved
007:         *
008:         *  The [fleXive](R) project is free software; you can redistribute
009:         *  it and/or modify it under the terms of the GNU General Public
010:         *  License as published by the Free Software Foundation;
011:         *  either version 2 of the License, or (at your option) any
012:         *  later version.
013:         *
014:         *  The GNU General Public License can be found at
015:         *  http://www.gnu.org/copyleft/gpl.html.
016:         *  A copy is found in the textfile GPL.txt and important notices to the
017:         *  license from the author are found in LICENSE.txt distributed with
018:         *  these libraries.
019:         *
020:         *  This library is distributed in the hope that it will be useful,
021:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
022:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023:         *  GNU General Public License for more details.
024:         *
025:         *  For further information about UCS - unique computing solutions gmbh,
026:         *  please see the company website: http://www.ucs.at
027:         *
028:         *  For further information about [fleXive](R), please see the
029:         *  project website: http://www.flexive.org
030:         *
031:         *
032:         *  This copyright notice MUST APPEAR in all copies of the file!
033:         ***************************************************************/package com.flexive.faces;
034:
035:        import com.flexive.shared.exceptions.FxInvalidParameterException;
036:        import com.flexive.shared.exceptions.FxNotFoundException;
037:        import org.apache.commons.lang.StringUtils;
038:        import org.apache.commons.logging.Log;
039:        import org.apache.commons.logging.LogFactory;
040:
041:        import java.util.HashMap;
042:        import java.util.Map;
043:        import java.util.regex.Matcher;
044:        import java.util.regex.Pattern;
045:
046:        /**
047:         * <p>A base implementation of an URI route. This class allows to define static mappers
048:         * for URI. External URIs can be parsed using {@link #getMatcher(String)} and created
049:         * with {@link #getMappedUri(java.util.Map)}. This implementation provides simple string-based,
050:         * named parameters that can be specified in the URI using "${parameter}", e.g.
051:         * "/myUri/${myParam}.xhtml". Subclasses like the {@link ContentURIRoute} provide more
052:         * specialized parameters.</p>
053:         * <p>
054:         * Although this class provides a complete implementation, it is declared abstract since
055:         * any usable URI mapper must define at least one static parameter.
056:         * </p>
057:         *
058:         * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
059:         * @version $Rev: 1 $
060:         */
061:        public abstract class URIRoute {
062:            private static final transient Log LOG = LogFactory
063:                    .getLog(URIRoute.class);
064:
065:            protected static final Map<String, String> PARAMETERS = new HashMap<String, String>();
066:            protected final Pattern pattern;
067:            protected final String format;
068:            protected final String target;
069:            private final Map<String, Integer> positions = new HashMap<String, Integer>();
070:
071:            protected URIRoute(String target, String format) {
072:                this .target = target;
073:                this .format = format;
074:                this .pattern = Pattern.compile(buildRegexp(format));
075:            }
076:
077:            /**
078:             * Returns the target URI of this mapper.
079:             *
080:             * @return the target URI of this mapper.
081:             */
082:            public String getTarget() {
083:                return target;
084:            }
085:
086:            /**
087:             * Return the URI format of this mapper.
088:             *
089:             * @return the URI format of this mapper.
090:             */
091:            public String getFormat() {
092:                return format;
093:            }
094:
095:            /**
096:             * Return a matcher for the given URI. The matcher allows to extract parameters
097:             * set in the URI.
098:             *
099:             * @param uri the URI (matching this mapper's URI format) to be parsed
100:             * @return a matcher for the given URI
101:             */
102:            public URIMatcher getMatcher(String uri) {
103:                final Matcher matcher = pattern.matcher(uri);
104:                matcher.find();
105:                return new URIMatcher(this , uri, matcher);
106:            }
107:
108:            /**
109:             * Replaces the given parameters in this mapper's URI format and returns the created URI.
110:             * Note that the given parameters must include <b>all</b> parameters set in the format,
111:             * although not all parameters specified have to be included in the format.
112:             *
113:             * @param parameters the parameter values to be replaced
114:             * @return a formatted URI
115:             */
116:            public String getMappedUri(Map<String, String> parameters) {
117:                String uri = format;
118:                int replacements = 0;
119:                for (Map.Entry<String, String> entry : parameters.entrySet()) {
120:                    if (hasParameter(entry.getKey())) {
121:                        uri = replaceUriParameter(uri, entry.getKey(), entry
122:                                .getValue());
123:                        replacements++;
124:                    }
125:                }
126:                if (replacements != positions.size()) {
127:                    throw new FxInvalidParameterException("parameters", LOG,
128:                            "ex.uriRoute.parameter.missing", format, uri)
129:                            .asRuntimeException();
130:                }
131:                return uri;
132:            }
133:
134:            private String buildRegexp(String format) {
135:                final StringBuilder out = new StringBuilder();
136:                int pos = 0;
137:                int nextParameter = format.indexOf("${", pos);
138:                int parameterPos = 0;
139:                while (nextParameter != -1) {
140:                    out.append(format, pos, nextParameter);
141:                    final int endIndex = format.indexOf('}', nextParameter + 2);
142:                    if (endIndex == -1) {
143:                        throw new FxInvalidParameterException("format", LOG,
144:                                "ex.uriRoute.format.closing", format)
145:                                .asRuntimeException();
146:                    }
147:                    final String name = format.substring(nextParameter + 2,
148:                            endIndex);
149:                    if (StringUtils.isBlank(name)) {
150:                        throw new FxInvalidParameterException("format", LOG,
151:                                "ex.uriRoute.format.emptyParameter", format)
152:                                .asRuntimeException();
153:                    }
154:                    if (positions.containsKey(name)) {
155:                        throw new FxInvalidParameterException("format", LOG,
156:                                "ex.uriRoute.format.uniqueParameter", name,
157:                                format).asRuntimeException();
158:                    }
159:                    positions.put(name, ++parameterPos);
160:                    out.append(getParameterRegexp(name));
161:                    pos = endIndex + 1;
162:                    nextParameter = format.indexOf("${", pos);
163:                }
164:                // append characters after last match and return formatted string
165:                return out.append(format, pos, format.length()).toString();
166:            }
167:
168:            /**
169:             * Replace the given parameter in the URI format string.
170:             *
171:             * @param format        the format string
172:             * @param parameterName the parameter name
173:             * @param value         the value to be set for the parameter
174:             * @return the replaced format string
175:             */
176:            protected String replaceUriParameter(String format,
177:                    String parameterName, String value) {
178:                // TODO create a more efficient version with string buffers
179:                return StringUtils.replace(format, "${" + parameterName + "}",
180:                        value);
181:            }
182:
183:            /**
184:             * Return the position of the given parameter name in the format string. If the parameter
185:             * is not set in the format string, a FxRuntimeException is thrown.
186:             *
187:             * @param parameterName the parameter name
188:             * @return the 1-based position of the parameter in the format string
189:             */
190:            int getPosition(String parameterName) {
191:                if (!positions.containsKey(parameterName)) {
192:                    throw new FxNotFoundException("parameterName", LOG,
193:                            "ex.uriRoute.parameter.unknown", parameterName)
194:                            .asRuntimeException();
195:                }
196:                return positions.get(parameterName);
197:            }
198:
199:            /**
200:             * Returns true if the given parameter is set in this mapper's format string.
201:             *
202:             * @param parameterName the parameter name to be checked
203:             * @return true if the given parameter is set in this mapper's format string.
204:             */
205:            boolean hasParameter(String parameterName) {
206:                return positions.containsKey(parameterName);
207:            }
208:
209:            /**
210:             * Returns the regular expression of the given parameter, or throws a
211:             * FxRuntimeException if the parameter is not available in this mapper.
212:             *
213:             * @param name the parameter name
214:             * @return the regular expression of the given parameter
215:             */
216:            protected String getParameterRegexp(String name) {
217:                if (!PARAMETERS.containsKey(name)) {
218:                    throw new FxInvalidParameterException("name", LOG,
219:                            "ex.uriRoute.parameter.unknown", name)
220:                            .asRuntimeException();
221:                }
222:                return PARAMETERS.get(name);
223:            }
224:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.