Source Code Cross Referenced for QNameUtils.java in  » Web-Services » spring-ws-1.0.0 » org » springframework » xml » namespace » 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 » Web Services » spring ws 1.0.0 » org.springframework.xml.namespace 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2005 the original author or authors.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.springframework.xml.namespace;
018:
019:        import javax.xml.namespace.QName;
020:
021:        import org.springframework.util.Assert;
022:        import org.springframework.util.StringUtils;
023:        import org.w3c.dom.Node;
024:
025:        /**
026:         * Helper class for using {@link QName}.
027:         *
028:         * @author Arjen Poutsma
029:         * @see javax.xml.namespace.QName
030:         * @since 1.0.0
031:         */
032:        public abstract class QNameUtils {
033:
034:            /** Indicates whether {@link QName} has a prefix. The first release of the class did not have this. */
035:            private static boolean qNameHasPrefix;
036:
037:            static {
038:                try {
039:                    QName.class.getDeclaredConstructor(new Class[] {
040:                            String.class, String.class, String.class });
041:                    qNameHasPrefix = true;
042:                } catch (NoSuchMethodException e) {
043:                    qNameHasPrefix = false;
044:                }
045:            }
046:
047:            /**
048:             * Creates a new <code>QName</code> with the given parameters. Sets the prefix if possible, i.e. if the
049:             * <code>QName(String, String, String)</code> constructor can be found. If this constructor is not available (as is
050:             * the case on older implementations of JAX-RPC), the prefix is ignored.
051:             *
052:             * @param namespaceUri namespace URI of the <code>QName</code>
053:             * @param localPart    local part of the <code>QName</code>
054:             * @param prefix       prefix of the <code>QName</code>. May be ignored.
055:             * @return the created <code>QName</code>
056:             * @see QName#QName(String,String,String)
057:             */
058:            public static QName createQName(String namespaceUri,
059:                    String localPart, String prefix) {
060:                if (qNameHasPrefix) {
061:                    return new QName(namespaceUri, localPart, prefix);
062:                } else {
063:                    return new QName(namespaceUri, localPart);
064:                }
065:            }
066:
067:            /**
068:             * Returns the prefix of the given <code>QName</code>. Returns the prefix if available, i.e. if the
069:             * <code>QName.getPrefix()</code> method can be found. If this method is not available (as is the case on older
070:             * implementations of JAX-RPC), an empty string is returned.
071:             *
072:             * @param qName the <code>QName</code> to return the prefix from
073:             * @return the prefix, if available, or an empty string
074:             * @see javax.xml.namespace.QName#getPrefix()
075:             */
076:            public static String getPrefix(QName qName) {
077:                return qNameHasPrefix ? qName.getPrefix() : "";
078:            }
079:
080:            /**
081:             * Validates the given String as a QName
082:             *
083:             * @param text the qualified name
084:             * @return <code>true</code> if valid, <code>false</code> otherwise
085:             */
086:            public static boolean validateQName(String text) {
087:                if (!StringUtils.hasLength(text)) {
088:                    return false;
089:                }
090:                if (text.charAt(0) == '{') {
091:                    int i = text.indexOf('}');
092:
093:                    if (i == -1 || i == text.length() - 1) {
094:                        return false;
095:                    }
096:                }
097:                return true;
098:            }
099:
100:            /**
101:             * Returns the qualified name of the given DOM Node.
102:             *
103:             * @param node the node
104:             * @return the qualified name of the node
105:             */
106:            public static QName getQNameForNode(Node node) {
107:                if (node.getNamespaceURI() != null && node.getPrefix() != null
108:                        && node.getLocalName() != null) {
109:                    return createQName(node.getNamespaceURI(), node
110:                            .getLocalName(), node.getPrefix());
111:                } else if (node.getNamespaceURI() != null
112:                        && node.getLocalName() != null) {
113:                    return new QName(node.getNamespaceURI(), node
114:                            .getLocalName());
115:                } else if (node.getLocalName() != null) {
116:                    return new QName(node.getLocalName());
117:                } else {
118:                    // as a last resort, use the node name
119:                    return new QName(node.getNodeName());
120:                }
121:            }
122:
123:            /**
124:             * Convert a <code>QName</code> to a qualified name, as used by DOM and SAX. The returned string has a format of
125:             * <code>prefix:localName</code> if the prefix is set, or just <code>localName</code> if not.
126:             *
127:             * @param qName the <code>QName</code>
128:             * @return the qualified name
129:             */
130:            public static String toQualifiedName(QName qName) {
131:                String prefix = getPrefix(qName);
132:                if (!StringUtils.hasLength(prefix)) {
133:                    return qName.getLocalPart();
134:                } else {
135:                    return prefix + ":" + qName.getLocalPart();
136:                }
137:            }
138:
139:            /**
140:             * Convert a namespace URI and DOM or SAX qualified name to a <code>QName</code>. The qualified name can have the
141:             * form <code>prefix:localname</code> or <code>localName</code>.
142:             *
143:             * @param namespaceUri  the namespace URI
144:             * @param qualifiedName the qualified name
145:             * @return a QName
146:             */
147:            public static QName toQName(String namespaceUri,
148:                    String qualifiedName) {
149:                int idx = qualifiedName.indexOf(':');
150:                if (idx == -1) {
151:                    return new QName(namespaceUri, qualifiedName);
152:                } else {
153:                    return createQName(namespaceUri, qualifiedName
154:                            .substring(idx + 1), qualifiedName
155:                            .substring(0, idx));
156:                }
157:            }
158:
159:            /**
160:             * Parse the given qualified name string into a <code>QName</code>. Expects the syntax <code>localPart</code>,
161:             * <code>{namespace}localPart</code>, or <code>{namespace}prefix:localPart</code>. This format resembles the
162:             * <code>toString()</code> representation of <code>QName</code> itself, but allows for prefixes to be specified as
163:             * well.
164:             *
165:             * @return a corresponding QName instance
166:             * @throws IllegalArgumentException when the given string is <code>null</code> or empty.
167:             */
168:            public static QName parseQNameString(String qNameString) {
169:                Assert.hasLength(qNameString,
170:                        "QName text may not be null or empty");
171:                if (qNameString.charAt(0) != '{') {
172:                    return new QName(qNameString);
173:                } else {
174:                    int endOfNamespaceURI = qNameString.indexOf('}');
175:                    if (endOfNamespaceURI == -1) {
176:                        throw new IllegalArgumentException(
177:                                "Cannot create QName from \"" + qNameString
178:                                        + "\", missing closing \"}\"");
179:                    }
180:                    int prefixSeperator = qNameString.indexOf(':',
181:                            endOfNamespaceURI + 1);
182:                    String namespaceURI = qNameString.substring(1,
183:                            endOfNamespaceURI);
184:                    if (prefixSeperator == -1) {
185:                        return new QName(namespaceURI, qNameString
186:                                .substring(endOfNamespaceURI + 1));
187:                    } else {
188:                        return createQName(namespaceURI, qNameString
189:                                .substring(prefixSeperator + 1), qNameString
190:                                .substring(endOfNamespaceURI + 1,
191:                                        prefixSeperator));
192:                    }
193:                }
194:
195:            }
196:
197:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.