Source Code Cross Referenced for NamespaceSupport.java in  » XML » xerces-2_9_1 » org » apache » xerces » util » 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 » XML » xerces 2_9_1 » org.apache.xerces.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.xerces.util;
019:
020:        import java.util.Enumeration;
021:        import java.util.NoSuchElementException;
022:
023:        import org.apache.xerces.xni.NamespaceContext;
024:
025:        /**
026:         * Namespace support for XML document handlers. This class doesn't 
027:         * perform any error checking and assumes that all strings passed
028:         * as arguments to methods are unique symbols. The SymbolTable class
029:         * can be used for this purpose.
030:         *
031:         * @author Andy Clark, IBM
032:         *
033:         * @version $Id: NamespaceSupport.java 447241 2006-09-18 05:12:57Z mrglavas $
034:         */
035:        public class NamespaceSupport implements  NamespaceContext {
036:
037:            //
038:            // Data
039:            //
040:
041:            /** 
042:             * Namespace binding information. This array is composed of a
043:             * series of tuples containing the namespace binding information:
044:             * <prefix, uri>. The default size can be set to anything
045:             * as long as it is a power of 2 greater than 1.
046:             *
047:             * @see #fNamespaceSize
048:             * @see #fContext
049:             */
050:            protected String[] fNamespace = new String[16 * 2];
051:
052:            /** The top of the namespace information array. */
053:            protected int fNamespaceSize;
054:
055:            // NOTE: The constructor depends on the initial context size 
056:            //       being at least 1. -Ac
057:
058:            /** 
059:             * Context indexes. This array contains indexes into the namespace
060:             * information array. The index at the current context is the start
061:             * index of declared namespace bindings and runs to the size of the
062:             * namespace information array.
063:             *
064:             * @see #fNamespaceSize
065:             */
066:            protected int[] fContext = new int[8];
067:
068:            /** The current context. */
069:            protected int fCurrentContext;
070:
071:            protected String[] fPrefixes = new String[16];
072:
073:            //
074:            // Constructors
075:            //
076:
077:            /** Default constructor. */
078:            public NamespaceSupport() {
079:            } // <init>()
080:
081:            /** 
082:             * Constructs a namespace context object and initializes it with
083:             * the prefixes declared in the specified context.
084:             */
085:            public NamespaceSupport(NamespaceContext context) {
086:                pushContext();
087:                // copy declaration in the context
088:                Enumeration prefixes = context.getAllPrefixes();
089:                while (prefixes.hasMoreElements()) {
090:                    String prefix = (String) prefixes.nextElement();
091:                    String uri = context.getURI(prefix);
092:                    declarePrefix(prefix, uri);
093:                }
094:            } // <init>(NamespaceContext)
095:
096:            //
097:            // Public methods
098:            //
099:
100:            /**
101:             * @see org.apache.xerces.xni.NamespaceContext#reset()
102:             */
103:            public void reset() {
104:
105:                // reset namespace and context info
106:                fNamespaceSize = 0;
107:                fCurrentContext = 0;
108:                fContext[fCurrentContext] = fNamespaceSize;
109:
110:                // bind "xml" prefix to the XML uri
111:                fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XML;
112:                fNamespace[fNamespaceSize++] = NamespaceContext.XML_URI;
113:                // bind "xmlns" prefix to the XMLNS uri
114:                fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XMLNS;
115:                fNamespace[fNamespaceSize++] = NamespaceContext.XMLNS_URI;
116:                ++fCurrentContext;
117:
118:            } // reset(SymbolTable)
119:
120:            /**
121:             * @see org.apache.xerces.xni.NamespaceContext#pushContext()
122:             */
123:            public void pushContext() {
124:
125:                // extend the array, if necessary
126:                if (fCurrentContext + 1 == fContext.length) {
127:                    int[] contextarray = new int[fContext.length * 2];
128:                    System.arraycopy(fContext, 0, contextarray, 0,
129:                            fContext.length);
130:                    fContext = contextarray;
131:                }
132:
133:                // push context
134:                fContext[++fCurrentContext] = fNamespaceSize;
135:
136:            } // pushContext()
137:
138:            /**
139:             * @see org.apache.xerces.xni.NamespaceContext#popContext()
140:             */
141:            public void popContext() {
142:                fNamespaceSize = fContext[fCurrentContext--];
143:            } // popContext()
144:
145:            /**
146:             * @see org.apache.xerces.xni.NamespaceContext#declarePrefix(String, String)
147:             */
148:            public boolean declarePrefix(String prefix, String uri) {
149:                // ignore "xml" and "xmlns" prefixes
150:                if (prefix == XMLSymbols.PREFIX_XML
151:                        || prefix == XMLSymbols.PREFIX_XMLNS) {
152:                    return false;
153:                }
154:
155:                // see if prefix already exists in current context
156:                for (int i = fNamespaceSize; i > fContext[fCurrentContext]; i -= 2) {
157:                    if (fNamespace[i - 2] == prefix) {
158:                        // REVISIT: [Q] Should the new binding override the
159:                        //          previously declared binding or should it
160:                        //          it be ignored? -Ac
161:                        // NOTE:    The SAX2 "NamespaceSupport" helper allows
162:                        //          re-bindings with the new binding overwriting
163:                        //          the previous binding. -Ac
164:                        fNamespace[i - 1] = uri;
165:                        return true;
166:                    }
167:                }
168:
169:                // resize array, if needed
170:                if (fNamespaceSize == fNamespace.length) {
171:                    String[] namespacearray = new String[fNamespaceSize * 2];
172:                    System.arraycopy(fNamespace, 0, namespacearray, 0,
173:                            fNamespaceSize);
174:                    fNamespace = namespacearray;
175:                }
176:
177:                // bind prefix to uri in current context
178:                fNamespace[fNamespaceSize++] = prefix;
179:                fNamespace[fNamespaceSize++] = uri;
180:
181:                return true;
182:
183:            } // declarePrefix(String,String):boolean
184:
185:            /**
186:             * @see org.apache.xerces.xni.NamespaceContext#getURI(String)
187:             */
188:            public String getURI(String prefix) {
189:
190:                // find prefix in current context
191:                for (int i = fNamespaceSize; i > 0; i -= 2) {
192:                    if (fNamespace[i - 2] == prefix) {
193:                        return fNamespace[i - 1];
194:                    }
195:                }
196:
197:                // prefix not found
198:                return null;
199:
200:            } // getURI(String):String
201:
202:            /**
203:             * @see org.apache.xerces.xni.NamespaceContext#getPrefix(String)
204:             */
205:            public String getPrefix(String uri) {
206:
207:                // find uri in current context
208:                for (int i = fNamespaceSize; i > 0; i -= 2) {
209:                    if (fNamespace[i - 1] == uri) {
210:                        if (getURI(fNamespace[i - 2]) == uri)
211:                            return fNamespace[i - 2];
212:                    }
213:                }
214:
215:                // uri not found
216:                return null;
217:
218:            } // getPrefix(String):String
219:
220:            /**
221:             * @see org.apache.xerces.xni.NamespaceContext#getDeclaredPrefixCount()
222:             */
223:            public int getDeclaredPrefixCount() {
224:                return (fNamespaceSize - fContext[fCurrentContext]) / 2;
225:            } // getDeclaredPrefixCount():int
226:
227:            /**
228:             * @see org.apache.xerces.xni.NamespaceContext#getDeclaredPrefixAt(int)
229:             */
230:            public String getDeclaredPrefixAt(int index) {
231:                return fNamespace[fContext[fCurrentContext] + index * 2];
232:            } // getDeclaredPrefixAt(int):String
233:
234:            /**
235:             * @see org.apache.xerces.xni.NamespaceContext#getAllPrefixes()
236:             */
237:            public Enumeration getAllPrefixes() {
238:                int count = 0;
239:                if (fPrefixes.length < (fNamespace.length / 2)) {
240:                    // resize prefix array          
241:                    String[] prefixes = new String[fNamespaceSize];
242:                    fPrefixes = prefixes;
243:                }
244:                String prefix = null;
245:                boolean unique = true;
246:                for (int i = 2; i < (fNamespaceSize - 2); i += 2) {
247:                    prefix = fNamespace[i + 2];
248:                    for (int k = 0; k < count; k++) {
249:                        if (fPrefixes[k] == prefix) {
250:                            unique = false;
251:                            break;
252:                        }
253:                    }
254:                    if (unique) {
255:                        fPrefixes[count++] = prefix;
256:                    }
257:                    unique = true;
258:                }
259:                return new Prefixes(fPrefixes, count);
260:            }
261:
262:            /*
263:             * non-NamespaceContext methods
264:             */
265:
266:            /** 
267:             * Checks whether a binding or unbinding for
268:             * the given prefix exists in the context.
269:             * 
270:             * @param prefix The prefix to look up. 
271:             * 
272:             * @return true if the given prefix exists in the context
273:             */
274:            public boolean containsPrefix(String prefix) {
275:
276:                // find prefix in current context
277:                for (int i = fNamespaceSize; i > 0; i -= 2) {
278:                    if (fNamespace[i - 2] == prefix) {
279:                        return true;
280:                    }
281:                }
282:
283:                // prefix not found
284:                return false;
285:            }
286:
287:            protected final class Prefixes implements  Enumeration {
288:                private String[] prefixes;
289:                private int counter = 0;
290:                private int size = 0;
291:
292:                /**
293:                 * Constructor for Prefixes.
294:                 */
295:                public Prefixes(String[] prefixes, int size) {
296:                    this .prefixes = prefixes;
297:                    this .size = size;
298:                }
299:
300:                /**
301:                 * @see java.util.Enumeration#hasMoreElements()
302:                 */
303:                public boolean hasMoreElements() {
304:                    return (counter < size);
305:                }
306:
307:                /**
308:                 * @see java.util.Enumeration#nextElement()
309:                 */
310:                public Object nextElement() {
311:                    if (counter < size) {
312:                        return fPrefixes[counter++];
313:                    }
314:                    throw new NoSuchElementException(
315:                            "Illegal access to Namespace prefixes enumeration.");
316:                }
317:
318:                public String toString() {
319:                    StringBuffer buf = new StringBuffer();
320:                    for (int i = 0; i < size; i++) {
321:                        buf.append(prefixes[i]);
322:                        buf.append(" ");
323:                    }
324:
325:                    return buf.toString();
326:                }
327:
328:            }
329:
330:        } // class NamespaceSupport
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.