KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > util > DOMBuilderContentHandlerFactory


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id$ */
19
20 package org.apache.fop.util;
21
22 import javax.xml.transform.TransformerConfigurationException JavaDoc;
23 import javax.xml.transform.dom.DOMResult JavaDoc;
24 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
25 import javax.xml.transform.sax.TransformerHandler JavaDoc;
26
27 import org.w3c.dom.DOMImplementation JavaDoc;
28 import org.w3c.dom.Document JavaDoc;
29 import org.xml.sax.Attributes JavaDoc;
30 import org.xml.sax.ContentHandler JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32
33 /**
34  * ContentHandlerFactory which constructs ContentHandlers that build DOM Documents.
35  */

36 public class DOMBuilderContentHandlerFactory implements ContentHandlerFactory {
37
38     private static SAXTransformerFactory JavaDoc tFactory
39             = (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
40
41     private String JavaDoc namespaceURI;
42     private DOMImplementation JavaDoc domImplementation;
43     
44     /**
45      * Main Constructor
46      * @param namespaceURI the main namespace URI for the DOM to be parsed
47      * @param domImplementation the DOMImplementation to use for build the DOM
48      */

49     public DOMBuilderContentHandlerFactory(String JavaDoc namespaceURI,
50                 DOMImplementation JavaDoc domImplementation) {
51         this.namespaceURI = namespaceURI;
52         this.domImplementation = domImplementation;
53     }
54     
55     /** @see org.apache.fop.util.ContentHandlerFactory#getSupportedNamespaces() */
56     public String JavaDoc[] getSupportedNamespaces() {
57         return new String JavaDoc[] {namespaceURI};
58     }
59
60     /** @see org.apache.fop.util.ContentHandlerFactory#createContentHandler() */
61     public ContentHandler JavaDoc createContentHandler() throws SAXException JavaDoc {
62         return new Handler JavaDoc();
63     }
64     
65     private class Handler extends DelegatingContentHandler
66                 implements ContentHandlerFactory.ObjectSource {
67      
68         private Document JavaDoc doc;
69         private ObjectBuiltListener obListener;
70         
71         public Handler() throws SAXException JavaDoc {
72             super();
73         }
74
75         public Document JavaDoc getDocument() {
76             return this.doc;
77         }
78         
79         /**
80          * @see org.apache.fop.util.ContentHandlerFactory.ObjectSource#getObject()
81          */

82         public Object JavaDoc getObject() {
83             return getDocument();
84         }
85
86         /**
87          * @see org.apache.fop.util.ContentHandlerFactory.ObjectSource
88          */

89         public void setObjectBuiltListener(ObjectBuiltListener listener) {
90             this.obListener = listener;
91         }
92         
93         /**
94          * @see org.apache.fop.util.DelegatingContentHandler#startDocument()
95          */

96         public void startDocument() throws SAXException JavaDoc {
97             //Suppress startDocument() call if doc has not been set, yet. It will be done later.
98
if (doc != null) {
99                 super.startDocument();
100             }
101         }
102
103         /**
104          * @see org.apache.fop.util.DelegatingContentHandler
105          */

106         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
107                     throws SAXException JavaDoc {
108             if (doc == null) {
109                 TransformerHandler JavaDoc handler;
110                 try {
111                     handler = tFactory.newTransformerHandler();
112                 } catch (TransformerConfigurationException JavaDoc e) {
113                     throw new SAXException JavaDoc("Error creating a new TransformerHandler", e);
114                 }
115                 doc = domImplementation.createDocument(namespaceURI, qName, null);
116                 //It's easier to work with an empty document, so remove the root element
117
doc.removeChild(doc.getDocumentElement());
118                 handler.setResult(new DOMResult JavaDoc(doc));
119                 setDelegateContentHandler(handler);
120                 setDelegateLexicalHandler(handler);
121                 setDelegateDTDHandler(handler);
122                 handler.startDocument();
123             }
124             super.startElement(uri, localName, qName, atts);
125         }
126
127         /**
128          * @see org.apache.fop.util.DelegatingContentHandler#endDocument()
129          */

130         public void endDocument() throws SAXException JavaDoc {
131             super.endDocument();
132             if (obListener != null) {
133                 obListener.notifyObjectBuilt(getObject());
134             }
135         }
136
137     }
138
139 }
140
Popular Tags