1 16 package org.outerj.daisy.publisher.serverimpl.docpreparation; 17 18 import org.xml.sax.ContentHandler ; 19 import org.xml.sax.SAXException ; 20 import org.xml.sax.Locator ; 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.helpers.AttributesImpl ; 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 ; 31 import java.util.Map ; 32 import java.util.HashMap ; 33 34 40 public class QueriesProcessor implements ContentHandler { 41 protected final QueryManager queryManager; 42 protected final ContentHandler consumer; 43 protected final ContentProcessor owner; 44 protected final Locale locale; 45 private boolean inQuery = false; 46 private int queryElementNesting; 47 private StringBuffer queryBuffer; 48 private int nestedElementCounter = 0; 49 50 public QueriesProcessor(ContentHandler 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 namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 58 nestedElementCounter++; 59 if (localName.equals("pre") && namespaceURI.equals("")) { 60 String clazz = atts.getValue("class"); 61 if (!inQuery && clazz != null && clazz.equals(getSensitiveClass())) { 62 inQuery = true; 63 queryElementNesting = nestedElementCounter; 64 queryBuffer = new StringBuffer (400); 65 } 66 } 67 68 if (!inQuery) { 69 consumer.startElement(namespaceURI, localName, qName, atts); 70 } 71 } 72 73 protected String getSensitiveClass() { 74 return "query"; 75 } 76 77 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 78 if (!inQuery) { 79 consumer.endElement(namespaceURI, localName, qName); 80 } 81 82 if (inQuery && queryElementNesting == nestedElementCounter) { 83 inQuery = false; 84 String query = queryBuffer.toString(); 85 executeQuery(query); 86 } 87 88 nestedElementCounter--; 89 } 90 91 protected void executeQuery(String query) throws SAXException { 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 e) { 98 outputFailedQueryMessage(e, query); 99 } 100 101 if (result != null) { 103 result.save(new StripDocumentHandler(consumer), new DummyLexicalHandler()); 104 } 105 } 106 107 protected Map getQueryOptions() { 108 if (owner.getPublisherContext().getVersionMode() == PublisherVersionMode.LAST) { 109 Map queryOptions = new HashMap (3); 110 queryOptions.put("search_last_version", "true"); 111 return queryOptions; 112 } else { 113 return null; 114 } 115 } 116 117 protected void outputFailedQueryMessage(Exception e, String query) throws SAXException { 118 AttributesImpl attrs = new AttributesImpl (); 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 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 { 141 consumer.startElement("", "br", "br", new AttributesImpl ()); 142 consumer.endElement("", "br", "br"); 143 } 144 145 private void insertString(String message) throws SAXException { 146 if (message == null) 147 message = "null"; 148 consumer.characters(message.toCharArray(), 0, message.length()); 149 } 150 151 public void endDocument() throws SAXException { 152 if (!inQuery) 153 consumer.endDocument(); 154 } 155 156 public void startDocument() throws SAXException { 157 if (!inQuery) 158 consumer.startDocument(); 159 } 160 161 public void characters(char ch[], int start, int length) throws SAXException { 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 { 170 if (!inQuery) 171 consumer.ignorableWhitespace(ch, start, length); 172 } 173 174 public void endPrefixMapping(String prefix) throws SAXException { 175 if (!inQuery) 176 consumer.endPrefixMapping(prefix); 177 } 178 179 public void skippedEntity(String name) throws SAXException { 180 if (!inQuery) 181 consumer.skippedEntity(name); 182 } 183 184 public void setDocumentLocator(Locator locator) { 185 if (!inQuery) 186 consumer.setDocumentLocator(locator); 187 } 188 189 public void processingInstruction(String target, String data) throws SAXException { 190 if (!inQuery) 191 consumer.processingInstruction(target, data); 192 } 193 194 public void startPrefixMapping(String prefix, String uri) throws SAXException { 195 if (!inQuery) 196 consumer.startPrefixMapping(prefix, uri); 197 } 198 199 } 200 | Popular Tags |