KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxanywhere > XMLHandler


1 /*
2 Copyright 2005 Vitaliy Shevchuk (shevit@users.sourceforge.net)
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 */

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 JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.parsers.ParserConfigurationException JavaDoc;
29 import javax.xml.transform.Transformer JavaDoc;
30 import javax.xml.transform.TransformerException JavaDoc;
31 import javax.xml.transform.TransformerFactory JavaDoc;
32 import javax.xml.transform.dom.DOMSource JavaDoc;
33 import javax.xml.transform.stream.StreamResult JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.io.StringWriter JavaDoc;
37 import java.util.*;
38
39 /**
40  * Date: 23 juil. 2005
41  * Time: 21:50:21
42  */

43 public class XMLHandler {
44
45     public static void sendZones(BufferResponseWrapper bufferResponseWrapper, Set refreshZones) {
46         Document JavaDoc doc = newDocument();
47         Element JavaDoc 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 JavaDoc zone = (String JavaDoc) iterator.next();
54             String JavaDoc content = AAUtils.getZoneContent(zone, bufferResponseWrapper);
55
56             //if zone added to refresh list but not present in content, then exclude zone info in response
57
if(content == null) {
58                 continue;
59             }
60             Element JavaDoc 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 JavaDoc script = (String JavaDoc) scripts.get(i);
68             int posScriptEnd = script.indexOf('>');
69             if (posScriptEnd != -1)
70                 script = script.substring(posScriptEnd + 1);
71
72             Element JavaDoc 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 JavaDoc image = (String JavaDoc) it.next();
79             Element JavaDoc 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 JavaDoc content, Element JavaDoc zoneNode, Document JavaDoc doc, List scripts, Set images, Element JavaDoc 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 JavaDoc addRootElement(Document JavaDoc doc, String JavaDoc rootTagName) {
99         Element JavaDoc root = doc.createElement(rootTagName);
100         doc.appendChild(root);
101         return root;
102     }
103
104     private static void sendDOMDocument(HttpServletResponse JavaDoc originalResponse, Document JavaDoc doc) {
105         try {
106
107             Transformer JavaDoc transformer = TransformerFactory.newInstance().newTransformer();
108
109             transformer.transform(new DOMSource JavaDoc(doc), new StreamResult JavaDoc(originalResponse.getOutputStream()));
110
111             originalResponse.flushBuffer();
112         } catch (TransformerException JavaDoc e) {
113             throw new RuntimeException JavaDoc(e);
114         } catch (IOException JavaDoc e) {
115             throw new RuntimeException JavaDoc(e);
116         }
117
118     }
119
120     private static Document JavaDoc newDocument() {
121         try {
122             return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
123         } catch (ParserConfigurationException JavaDoc e) {
124             throw new RuntimeException JavaDoc(e);
125         }
126     }
127
128
129     private static void appendText(Element JavaDoc zoneNode, Document JavaDoc doc, String JavaDoc content) {
130         zoneNode.appendChild(doc.createCDATASection(content.replaceAll("\r", "")));
131     }
132
133     public static void handleError(HttpServletResponse JavaDoc response, Throwable JavaDoc exception) {
134         Document JavaDoc doc = newDocument();
135
136         Element JavaDoc root = addRootElement(doc, AAConstants.AA_XML_EXCEPTION);
137         root.setAttribute(AAConstants.AA_XML_TYPE, exception.getClass().getName());
138         StringWriter JavaDoc sw = new StringWriter JavaDoc();
139         exception.printStackTrace(new PrintWriter JavaDoc(sw));
140         appendText(root, doc, sw.toString());
141
142         sendDOMDocument(response, doc);
143
144     }
145
146     public static void sendRedirect(BufferResponseWrapper bufferResponseWrapper) {
147         Document JavaDoc doc = newDocument();
148         Element JavaDoc root = addRootElement(doc, AAConstants.AA_XML_REDIRECT);
149         String JavaDoc redirect = bufferResponseWrapper.getRedirect();
150         appendText(root, doc, redirect);
151
152         sendDOMDocument(bufferResponseWrapper.getOriginalResponse(), doc);
153     }
154 }
155
Popular Tags