Source Code Cross Referenced for SAXConfigurationHandler.java in  » Search-Engine » compass-2.0 » org » compass » core » util » config » 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 » Search Engine » compass 2.0 » org.compass.core.util.config 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2006 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.compass.core.util.config;
018:
019:        import java.util.ArrayList;
020:        import java.util.BitSet;
021:
022:        import org.xml.sax.Attributes;
023:        import org.xml.sax.ErrorHandler;
024:        import org.xml.sax.Locator;
025:        import org.xml.sax.SAXException;
026:        import org.xml.sax.SAXParseException;
027:        import org.xml.sax.helpers.DefaultHandler;
028:
029:        /**
030:         * A SAXConfigurationHandler helps build Configurations out of sax events.
031:         */
032:        public class SAXConfigurationHandler extends DefaultHandler implements 
033:                ErrorHandler {
034:
035:            /**
036:             * Likely number of nested configuration items. If more is encountered the
037:             * lists will grow automatically.
038:             */
039:
040:            private static final int EXPECTED_DEPTH = 5;
041:
042:            private final ArrayList m_elements = new ArrayList(EXPECTED_DEPTH);
043:
044:            private final ArrayList m_values = new ArrayList(EXPECTED_DEPTH);
045:
046:            /**
047:             * Contains true at index n if space in the configuration with depth n is to
048:             * be preserved.
049:             */
050:            private final BitSet m_preserveSpace = new BitSet();
051:
052:            private ConfigurationHelper m_configuration;
053:
054:            private Locator m_locator;
055:
056:            /**
057:             * Get the configuration object that was built.
058:             */
059:            public ConfigurationHelper getConfiguration() {
060:                return m_configuration;
061:            }
062:
063:            /**
064:             * Clears all data from this configuration handler.
065:             */
066:            public void clear() {
067:                m_elements.clear();
068:                m_values.clear();
069:                m_locator = null;
070:            }
071:
072:            /**
073:             * Set the document <code>Locator</code> to use.
074:             */
075:            public void setDocumentLocator(final Locator locator) {
076:                m_locator = locator;
077:            }
078:
079:            /**
080:             * Handling hook for character data.
081:             */
082:            public void characters(final char[] ch, int start, int end)
083:                    throws SAXException {
084:                // it is possible to play micro-optimization here by doing
085:                // manual trimming and thus preserve some precious bits
086:                // of memory, but it's really not important enough to justify
087:                // resulting code complexity
088:                final int depth = m_values.size() - 1;
089:                final StringBuffer valueBuffer = (StringBuffer) m_values
090:                        .get(depth);
091:                valueBuffer.append(ch, start, end);
092:            }
093:
094:            /**
095:             * Handling hook for finishing parsing of an element.
096:             */
097:            public void endElement(final String namespaceURI,
098:                    final String localName, final String rawName)
099:                    throws SAXException {
100:                final int depth = m_elements.size() - 1;
101:                final XmlConfigurationHelper finishedConfiguration = (XmlConfigurationHelper) m_elements
102:                        .remove(depth);
103:                final String accumulatedValue = ((StringBuffer) m_values
104:                        .remove(depth)).toString();
105:                if (finishedConfiguration.getChildren().length == 0) {
106:                    // leaf node
107:                    String finishedValue;
108:                    if (m_preserveSpace.get(depth)) {
109:                        finishedValue = accumulatedValue;
110:                    } else if (0 == accumulatedValue.length()) {
111:                        finishedValue = null;
112:                    } else {
113:                        finishedValue = accumulatedValue.trim();
114:                    }
115:                    finishedConfiguration.setValue(finishedValue);
116:                } else {
117:                    final String trimmedValue = accumulatedValue.trim();
118:                    if (trimmedValue.length() > 0) {
119:                        throw new SAXException(
120:                                "Not allowed to define mixed content in the "
121:                                        + "element "
122:                                        + finishedConfiguration.getName()
123:                                        + " at "
124:                                        + finishedConfiguration.getLocation());
125:                    }
126:                }
127:                if (0 == depth) {
128:                    m_configuration = finishedConfiguration;
129:                }
130:            }
131:
132:            /**
133:             * Create a new <code>DefaultConfiguration</code> with the specified local
134:             * name and location.
135:             */
136:            protected XmlConfigurationHelper createConfiguration(
137:                    final String localName, final String location) {
138:                return new XmlConfigurationHelper(localName, location);
139:            }
140:
141:            /**
142:             * Handling hook for starting parsing of an element.
143:             */
144:            public void startElement(final String namespaceURI,
145:                    final String localName, final String rawName,
146:                    final Attributes attributes) throws SAXException {
147:                final XmlConfigurationHelper configuration = createConfiguration(
148:                        rawName, getLocationString());
149:                // depth of new configuration (not decrementing here, configuration
150:                // is to be added)
151:                final int depth = m_elements.size();
152:                boolean preserveSpace = false; // top level element trims space by
153:                // default
154:                if (depth > 0) {
155:                    final XmlConfigurationHelper parent = (XmlConfigurationHelper) m_elements
156:                            .get(depth - 1);
157:                    parent.addChild(configuration);
158:                    // inherits parent's space preservation policy
159:                    preserveSpace = m_preserveSpace.get(depth - 1);
160:                }
161:                m_elements.add(configuration);
162:                m_values.add(new StringBuffer());
163:                final int attributesSize = attributes.getLength();
164:                for (int i = 0; i < attributesSize; i++) {
165:                    final String name = attributes.getQName(i);
166:                    final String value = attributes.getValue(i);
167:                    if (!name.equals("xml:space")) {
168:                        configuration.setAttribute(name, value);
169:                    } else {
170:                        preserveSpace = value.equals("preserve");
171:                    }
172:                }
173:                if (preserveSpace) {
174:                    m_preserveSpace.set(depth);
175:                } else {
176:                    m_preserveSpace.clear(depth);
177:                }
178:            }
179:
180:            /**
181:             * This just throws an exception on a parse error.
182:             */
183:            public void error(final SAXParseException exception)
184:                    throws SAXException {
185:                throw exception;
186:            }
187:
188:            /**
189:             * This just throws an exception on a parse error.
190:             */
191:            public void warning(final SAXParseException exception)
192:                    throws SAXException {
193:                throw exception;
194:            }
195:
196:            /**
197:             * This just throws an exception on a parse error.
198:             */
199:            public void fatalError(final SAXParseException exception)
200:                    throws SAXException {
201:                throw exception;
202:            }
203:
204:            /**
205:             * Returns a string showing the current system ID, line number and column
206:             * number.
207:             */
208:
209:            protected String getLocationString() {
210:                if (null == m_locator) {
211:                    return "Unknown";
212:                } else {
213:                    final int columnNumber = m_locator.getColumnNumber();
214:                    return m_locator.getSystemId() + ":"
215:                            + m_locator.getLineNumber()
216:                            + (columnNumber >= 0 ? (":" + columnNumber) : "");
217:                }
218:            }
219:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.