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: /* $Id$ */
019:
020: package org.apache.fop.util;
021:
022: import javax.xml.transform.TransformerConfigurationException;
023: import javax.xml.transform.dom.DOMResult;
024: import javax.xml.transform.sax.SAXTransformerFactory;
025: import javax.xml.transform.sax.TransformerHandler;
026:
027: import org.w3c.dom.DOMImplementation;
028: import org.w3c.dom.Document;
029: import org.xml.sax.Attributes;
030: import org.xml.sax.ContentHandler;
031: import org.xml.sax.SAXException;
032:
033: /**
034: * ContentHandlerFactory which constructs ContentHandlers that build DOM Documents.
035: */
036: public class DOMBuilderContentHandlerFactory implements
037: ContentHandlerFactory {
038:
039: private static SAXTransformerFactory tFactory = (SAXTransformerFactory) SAXTransformerFactory
040: .newInstance();
041:
042: private String namespaceURI;
043: private DOMImplementation domImplementation;
044:
045: /**
046: * Main Constructor
047: * @param namespaceURI the main namespace URI for the DOM to be parsed
048: * @param domImplementation the DOMImplementation to use for build the DOM
049: */
050: public DOMBuilderContentHandlerFactory(String namespaceURI,
051: DOMImplementation domImplementation) {
052: this .namespaceURI = namespaceURI;
053: this .domImplementation = domImplementation;
054: }
055:
056: /** {@inheritDoc} */
057: public String[] getSupportedNamespaces() {
058: return new String[] { namespaceURI };
059: }
060:
061: /** {@inheritDoc} */
062: public ContentHandler createContentHandler() throws SAXException {
063: return new Handler();
064: }
065:
066: private class Handler extends DelegatingContentHandler implements
067: ContentHandlerFactory.ObjectSource {
068:
069: private Document doc;
070: private ObjectBuiltListener obListener;
071:
072: public Handler() throws SAXException {
073: super ();
074: }
075:
076: public Document getDocument() {
077: return this .doc;
078: }
079:
080: /**
081: * {@inheritDoc}
082: */
083: public Object getObject() {
084: return getDocument();
085: }
086:
087: /**
088: * {@inheritDoc}
089: */
090: public void setObjectBuiltListener(ObjectBuiltListener listener) {
091: this .obListener = listener;
092: }
093:
094: /**
095: * {@inheritDoc}
096: */
097: public void startDocument() throws SAXException {
098: //Suppress startDocument() call if doc has not been set, yet. It will be done later.
099: if (doc != null) {
100: super .startDocument();
101: }
102: }
103:
104: /**
105: * {@inheritDoc}
106: */
107: public void startElement(String uri, String localName,
108: String qName, Attributes atts) throws SAXException {
109: if (doc == null) {
110: TransformerHandler handler;
111: try {
112: handler = tFactory.newTransformerHandler();
113: } catch (TransformerConfigurationException e) {
114: throw new SAXException(
115: "Error creating a new TransformerHandler",
116: e);
117: }
118: doc = domImplementation.createDocument(namespaceURI,
119: qName, null);
120: //It's easier to work with an empty document, so remove the root element
121: doc.removeChild(doc.getDocumentElement());
122: handler.setResult(new DOMResult(doc));
123: setDelegateContentHandler(handler);
124: setDelegateLexicalHandler(handler);
125: setDelegateDTDHandler(handler);
126: handler.startDocument();
127: }
128: super .startElement(uri, localName, qName, atts);
129: }
130:
131: /**
132: * {@inheritDoc}
133: */
134: public void endDocument() throws SAXException {
135: super.endDocument();
136: if (obListener != null) {
137: obListener.notifyObjectBuilt(getObject());
138: }
139: }
140:
141: }
142:
143: }
|