1 17 18 package org.ajaxanywhere; 19 20 import org.ajaxanywhere.parser.ResponseBean; 21 import org.ajaxanywhere.parser.ResponseParser; 22 import org.ajaxanywhere.parser.ResponseParserFactory; 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 import javax.xml.parsers.ParserConfigurationException ; 29 import javax.xml.transform.Transformer ; 30 import javax.xml.transform.TransformerException ; 31 import javax.xml.transform.TransformerFactory ; 32 import javax.xml.transform.dom.DOMSource ; 33 import javax.xml.transform.stream.StreamResult ; 34 import java.io.IOException ; 35 import java.io.PrintWriter ; 36 import java.io.StringWriter ; 37 import java.util.*; 38 39 43 public class XMLHandler { 44 45 public static void sendZones(BufferResponseWrapper bufferResponseWrapper, Set refreshZones) { 46 Document doc = newDocument(); 47 Element root = addRootElement(doc, AAConstants.AA_XML_ZONES); 48 49 List scripts = new ArrayList(); 50 Set images = new HashSet(); 51 52 for (Iterator iterator = refreshZones.iterator(); iterator.hasNext();) { 53 String zone = (String ) iterator.next(); 54 String content = AAUtils.getZoneContent(zone, bufferResponseWrapper); 55 56 if(content == null) { 58 continue; 59 } 60 Element zoneNode = doc.createElement(AAConstants.AA_XML_ZONE); 61 zoneNode.setAttribute(AAConstants.AA_XML_NAME, zone); 62 63 handleZoneContent(content, zoneNode, doc, scripts, images, root); 64 } 65 66 for (int i = 0; i < scripts.size(); i++) { 67 String script = (String ) scripts.get(i); 68 int posScriptEnd = script.indexOf('>'); 69 if (posScriptEnd != -1) 70 script = script.substring(posScriptEnd + 1); 71 72 Element scriptNode = doc.createElement(AAConstants.AA_XML_SCRIPT); 73 appendText(scriptNode, doc, script); 74 root.appendChild(scriptNode); 75 } 76 77 for (Iterator it = images.iterator(); it.hasNext();) { 78 String image = (String ) it.next(); 79 Element imageNode = doc.createElement(AAConstants.AA_XML_IMAGE); 80 appendText(imageNode, doc, image); 81 root.appendChild(imageNode); 82 } 83 84 sendDOMDocument(bufferResponseWrapper.getOriginalResponse(), doc); 85 } 86 87 private static void handleZoneContent(String content, Element zoneNode, Document doc, List scripts, Set images, Element root) { 88 ResponseParser parser = ResponseParserFactory.getInstance().getParser(); 89 ResponseBean result = parser.parse(content); 90 91 appendText(zoneNode, doc, result.getHtmlContent()); 92 scripts.addAll(result.getScriptContents()); 93 images.addAll(result.getImages()); 94 root.appendChild(zoneNode); 95 } 96 97 98 private static Element addRootElement(Document doc, String rootTagName) { 99 Element root = doc.createElement(rootTagName); 100 doc.appendChild(root); 101 return root; 102 } 103 104 private static void sendDOMDocument(HttpServletResponse originalResponse, Document doc) { 105 try { 106 107 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 108 109 transformer.transform(new DOMSource (doc), new StreamResult (originalResponse.getOutputStream())); 110 111 originalResponse.flushBuffer(); 112 } catch (TransformerException e) { 113 throw new RuntimeException (e); 114 } catch (IOException e) { 115 throw new RuntimeException (e); 116 } 117 118 } 119 120 private static Document newDocument() { 121 try { 122 return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 123 } catch (ParserConfigurationException e) { 124 throw new RuntimeException (e); 125 } 126 } 127 128 129 private static void appendText(Element zoneNode, Document doc, String content) { 130 zoneNode.appendChild(doc.createCDATASection(content.replaceAll("\r", ""))); 131 } 132 133 public static void handleError(HttpServletResponse response, Throwable exception) { 134 Document doc = newDocument(); 135 136 Element root = addRootElement(doc, AAConstants.AA_XML_EXCEPTION); 137 root.setAttribute(AAConstants.AA_XML_TYPE, exception.getClass().getName()); 138 StringWriter sw = new StringWriter (); 139 exception.printStackTrace(new PrintWriter (sw)); 140 appendText(root, doc, sw.toString()); 141 142 sendDOMDocument(response, doc); 143 144 } 145 146 public static void sendRedirect(BufferResponseWrapper bufferResponseWrapper) { 147 Document doc = newDocument(); 148 Element root = addRootElement(doc, AAConstants.AA_XML_REDIRECT); 149 String redirect = bufferResponseWrapper.getRedirect(); 150 appendText(root, doc, redirect); 151 152 sendDOMDocument(bufferResponseWrapper.getOriginalResponse(), doc); 153 } 154 } 155 | Popular Tags |