KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > serverimpl > docpreparation > QueriesProcessor


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.publisher.serverimpl.docpreparation;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.xml.sax.Locator JavaDoc;
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.helpers.AttributesImpl JavaDoc;
23 import org.outerj.daisy.repository.query.QueryManager;
24 import org.outerj.daisy.repository.query.EvaluationContext;
25 import org.outerj.daisy.publisher.serverimpl.StripDocumentHandler;
26 import org.outerj.daisy.publisher.serverimpl.DummyLexicalHandler;
27 import org.outerj.daisy.publisher.serverimpl.requestmodel.PublisherVersionMode;
28 import org.apache.xmlbeans.XmlObject;
29
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33
34 /**
35  * This handler will replace all p tags with class "query" with the result
36  * of executing the query contained in it. In case of an error a p tag
37  * with class "daisy-error" will be generated, containing a description of
38  * the error.
39  */

40 public class QueriesProcessor implements ContentHandler JavaDoc {
41     protected final QueryManager queryManager;
42     protected final ContentHandler JavaDoc consumer;
43     protected final ContentProcessor owner;
44     protected final Locale JavaDoc locale;
45     private boolean inQuery = false;
46     private int queryElementNesting;
47     private StringBuffer JavaDoc queryBuffer;
48     private int nestedElementCounter = 0;
49
50     public QueriesProcessor(ContentHandler JavaDoc consumer, ContentProcessor owner) {
51         this.owner = owner;
52         this.queryManager = owner.getPreparationPipe().getRepository().getQueryManager();
53         this.consumer = consumer;
54         this.locale = owner.getPreparationPipe().getLocale();
55     }
56
57     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
58         nestedElementCounter++;
59         if (localName.equals("pre") && namespaceURI.equals("")) {
60             String JavaDoc clazz = atts.getValue("class");
61             if (!inQuery && clazz != null && clazz.equals(getSensitiveClass())) {
62                 inQuery = true;
63                 queryElementNesting = nestedElementCounter;
64                 queryBuffer = new StringBuffer JavaDoc(400);
65             }
66         }
67
68         if (!inQuery) {
69             consumer.startElement(namespaceURI, localName, qName, atts);
70         }
71     }
72
73     protected String JavaDoc getSensitiveClass() {
74         return "query";
75     }
76
77     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
78         if (!inQuery) {
79             consumer.endElement(namespaceURI, localName, qName);
80         }
81
82         if (inQuery && queryElementNesting == nestedElementCounter) {
83             inQuery = false;
84             String JavaDoc query = queryBuffer.toString();
85             executeQuery(query);
86         }
87
88         nestedElementCounter--;
89     }
90
91     protected void executeQuery(String JavaDoc query) throws SAXException JavaDoc {
92         XmlObject result = null;
93         try {
94             EvaluationContext evaluationContext = new EvaluationContext();
95             evaluationContext.setContextDocument(owner.getDocument(), owner.getVersion());
96             result = queryManager.performQuery(query, null, getQueryOptions(), locale, evaluationContext);
97         } catch (Exception JavaDoc e) {
98             outputFailedQueryMessage(e, query);
99         }
100
101         // output query result
102
if (result != null) {
103             result.save(new StripDocumentHandler(consumer), new DummyLexicalHandler());
104         }
105     }
106
107     protected Map JavaDoc getQueryOptions() {
108         if (owner.getPublisherContext().getVersionMode() == PublisherVersionMode.LAST) {
109             Map JavaDoc queryOptions = new HashMap JavaDoc(3);
110             queryOptions.put("search_last_version", "true");
111             return queryOptions;
112         } else {
113             return null;
114         }
115     }
116
117     protected void outputFailedQueryMessage(Exception JavaDoc e, String JavaDoc query) throws SAXException JavaDoc {
118         // Output a paragraph containing description of the error
119
AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
120         attrs.addAttribute("", "class", "class", "CDATA", "daisy-error");
121         consumer.startElement("", "p", "p", attrs);
122
123         insertString("Could not perform the following embedded query:");
124         insertBr();
125         insertString(query);
126         insertBr();
127         insertString("Encountered the following errors:");
128         insertBr();
129         insertString(e.getMessage());
130         Throwable JavaDoc cause = e.getCause();
131         while (cause != null) {
132             insertBr();
133             insertString(cause.getMessage());
134             cause = cause.getCause();
135         }
136
137         consumer.endElement("", "p", "p");
138     }
139
140     private void insertBr() throws SAXException JavaDoc {
141         consumer.startElement("", "br", "br", new AttributesImpl JavaDoc());
142         consumer.endElement("", "br", "br");
143     }
144
145     private void insertString(String JavaDoc message) throws SAXException JavaDoc {
146         if (message == null)
147             message = "null";
148         consumer.characters(message.toCharArray(), 0, message.length());
149     }
150
151     public void endDocument() throws SAXException JavaDoc {
152         if (!inQuery)
153             consumer.endDocument();
154     }
155
156     public void startDocument() throws SAXException JavaDoc {
157         if (!inQuery)
158             consumer.startDocument();
159     }
160
161     public void characters(char ch[], int start, int length) throws SAXException JavaDoc {
162         if (inQuery) {
163             queryBuffer.append(ch, start, length);
164         } else {
165             consumer.characters(ch, start, length);
166         }
167     }
168
169     public void ignorableWhitespace(char ch[], int start, int length) throws SAXException JavaDoc {
170         if (!inQuery)
171             consumer.ignorableWhitespace(ch, start, length);
172     }
173
174     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
175         if (!inQuery)
176             consumer.endPrefixMapping(prefix);
177     }
178
179     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
180         if (!inQuery)
181             consumer.skippedEntity(name);
182     }
183
184     public void setDocumentLocator(Locator JavaDoc locator) {
185         if (!inQuery)
186             consumer.setDocumentLocator(locator);
187     }
188
189     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
190         if (!inQuery)
191             consumer.processingInstruction(target, data);
192     }
193
194     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
195         if (!inQuery)
196             consumer.startPrefixMapping(prefix, uri);
197     }
198
199 }
200
Popular Tags