KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > publicationprocess > PreparedDocumentsBuilder


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.books.publisher.impl.publicationprocess;
17
18
19 import org.xml.sax.*;
20 import org.xml.sax.helpers.NamespaceSupport JavaDoc;
21 import org.apache.cocoon.xml.SaxBuffer;
22 import org.outerj.daisy.frontend.PreparedDocuments;
23 import org.outerj.daisy.repository.VariantKey;
24
25 import javax.xml.parsers.SAXParserFactory JavaDoc;
26 import javax.xml.parsers.SAXParser JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Enumeration JavaDoc;
29
30 public class PreparedDocumentsBuilder {
31     private static final String JavaDoc PUBLISHER_NAMESPACE = "http://outerx.org/daisy/1.0#publisher";
32
33     public static PreparedDocuments build(InputStream JavaDoc is) throws Exception JavaDoc {
34         SAXParserFactory JavaDoc saxParserFactory = SAXParserFactory.newInstance();
35         saxParserFactory.setNamespaceAware(true);
36         saxParserFactory.setValidating(false);
37         SAXParser JavaDoc saxParser = saxParserFactory.newSAXParser();
38
39         PreparedDocumentsHandler preparedDocumentsHandler = new PreparedDocumentsHandler();
40         XMLReader xmlReader = saxParser.getXMLReader();
41         xmlReader.setContentHandler(preparedDocumentsHandler);
42
43         InputSource inputSource = new InputSource(is);
44         xmlReader.parse(inputSource);
45
46         PreparedDocuments preparedDocuments = preparedDocumentsHandler.getPreparedDocuments();
47         if (preparedDocuments.getPreparedDocument(1) == null) {
48             throw new Exception JavaDoc("Invalid preparedDocuments, there is no prepared document with ID 1.");
49         }
50         return preparedDocuments;
51     }
52
53     private static class PreparedDocumentsHandler implements ContentHandler {
54         private int elementNesting = 0;
55         private int currentPreparedDocumentId;
56         private VariantKey documentKey;
57         private SaxBuffer buffer;
58         private NamespaceSupport JavaDoc namespaceSupport = new NamespaceSupport JavaDoc();
59         private PreparedDocuments preparedDocuments = new PreparedDocuments();
60
61         public PreparedDocuments getPreparedDocuments() {
62             return preparedDocuments;
63         }
64
65         public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException {
66             if (elementNesting < 2)
67                 namespaceSupport.pushContext();
68
69             if (elementNesting == 0) {
70                 if (!(namespaceURI.equals(PUBLISHER_NAMESPACE) && localName.equals("preparedDocuments"))) {
71                     throw new SAXException("Expect as root element \"p:preparedDocuments\" but got \"" + qName + "\".");
72                 }
73             } else if (elementNesting == 1) {
74                 if (namespaceURI.equals(PUBLISHER_NAMESPACE) && localName.equals("preparedDocument")) {
75                     currentPreparedDocumentId = Integer.parseInt(atts.getValue("id"));
76                     long documentId = Long.parseLong(atts.getValue("documentId"));
77                     long branchId = Long.parseLong(atts.getValue("branchId"));
78                     long languageId = Long.parseLong(atts.getValue("languageId"));
79                     documentKey = new VariantKey(documentId, branchId, languageId);
80                     buffer = new SaxBuffer();
81                     buffer.startDocument();
82                     // Make sure current namespace declarations are passed on
83
Enumeration JavaDoc prefixEnum = namespaceSupport.getPrefixes();
84                     while (prefixEnum.hasMoreElements()) {
85                         String JavaDoc prefix = (String JavaDoc)prefixEnum.nextElement();
86                         if (!prefix.equals("xml"))
87                             buffer.startPrefixMapping(prefix, namespaceSupport.getURI(prefix));
88                     }
89                 } else {
90                     throw new SAXException("Encountered an unexpected element in preparedDocuments: \"" + qName + "\".");
91                 }
92             } else if (elementNesting >= 2) {
93                 buffer.startElement(namespaceURI, localName, qName, atts);
94             }
95             elementNesting++;
96         }
97
98         public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException {
99             elementNesting--;
100             if (elementNesting == 1) {
101                 buffer.endDocument();
102                 preparedDocuments.putPreparedDocument(currentPreparedDocumentId, documentKey, buffer);
103                 buffer = null;
104                 documentKey = null;
105             } else if (elementNesting >= 2) {
106                 buffer.endElement(namespaceURI, localName, qName);
107             }
108         }
109
110         public void endDocument() throws SAXException {
111             // do nothing
112
}
113
114         public void startDocument() throws SAXException {
115             // do nothing
116
}
117
118         public void characters(char ch[], int start, int length) throws SAXException {
119             if (elementNesting >= 2)
120                 buffer.characters(ch, start, length);
121         }
122
123         public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
124             if (elementNesting >= 2)
125                 buffer.ignorableWhitespace(ch, start, length);
126         }
127
128         public void endPrefixMapping(String JavaDoc prefix) throws SAXException {
129             if (elementNesting >= 2)
130                 buffer.endPrefixMapping(prefix);
131         }
132
133         public void skippedEntity(String JavaDoc name) throws SAXException {
134             if (elementNesting >= 2)
135                 buffer.skippedEntity(name);
136         }
137
138         public void setDocumentLocator(Locator locator) {
139             if (elementNesting >= 2)
140                 buffer.setDocumentLocator(locator);
141         }
142
143         public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException {
144             if (elementNesting >= 2)
145                 buffer.processingInstruction(target, data);
146         }
147
148         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException {
149             if (elementNesting >= 2)
150                 buffer.startPrefixMapping(prefix, uri);
151             else
152                 namespaceSupport.declarePrefix(prefix, uri);
153         }
154     }
155 }
156
Popular Tags