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: }
|