1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.contrib.ajax; |
16 |
| |
17 |
| import java.io.IOException; |
18 |
| import java.io.OutputStream; |
19 |
| import java.io.StringWriter; |
20 |
| |
21 |
| import javax.xml.parsers.DocumentBuilder; |
22 |
| import javax.xml.parsers.DocumentBuilderFactory; |
23 |
| import javax.xml.transform.OutputKeys; |
24 |
| import javax.xml.transform.Transformer; |
25 |
| import javax.xml.transform.TransformerFactory; |
26 |
| import javax.xml.transform.dom.DOMSource; |
27 |
| import javax.xml.transform.stream.StreamResult; |
28 |
| |
29 |
| import org.apache.hivemind.ApplicationRuntimeException; |
30 |
| import org.apache.tapestry.IComponent; |
31 |
| import org.apache.tapestry.IPage; |
32 |
| import org.apache.tapestry.IRequestCycle; |
33 |
| import org.apache.tapestry.engine.IEngineService; |
34 |
| import org.apache.tapestry.engine.ILink; |
35 |
| import org.apache.tapestry.error.RequestExceptionReporter; |
36 |
| import org.apache.tapestry.request.RequestContext; |
37 |
| import org.apache.tapestry.services.ServiceConstants; |
38 |
| import org.apache.tapestry.util.ContentType; |
39 |
| import org.apache.tapestry.web.WebResponse; |
40 |
| import org.w3c.dom.Document; |
41 |
| import org.w3c.dom.Node; |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| public class XTileService implements IEngineService |
49 |
| { |
50 |
| public static final String SERVICE_NAME = "xtile"; |
51 |
| |
52 |
| private RequestExceptionReporter _exceptionReporter; |
53 |
| private WebResponse _response; |
54 |
| |
55 |
0
| public String getName()
|
56 |
| { |
57 |
0
| return SERVICE_NAME;
|
58 |
| } |
59 |
| |
60 |
0
| public ILink getLink(IRequestCycle cycle, boolean post, Object parameter) {
|
61 |
0
| throw new UnsupportedOperationException();
|
62 |
| } |
63 |
| |
64 |
0
| public void service(IRequestCycle cycle) throws IOException {
|
65 |
0
| String pageName = cycle.getParameter(ServiceConstants.PAGE);
|
66 |
0
| String componentId = cycle.getParameter(ServiceConstants.COMPONENT);
|
67 |
| |
68 |
0
| IPage componentPage = cycle.getPage(pageName);
|
69 |
0
| IComponent component = componentPage.getNestedComponent(componentId);
|
70 |
| |
71 |
0
| if (!(component instanceof IXTile))
|
72 |
0
| throw new ApplicationRuntimeException("Incorrect component type: was " + component.getClass() + " but must be " + IXTile.class,
|
73 |
| component, null, null); |
74 |
| |
75 |
0
| IXTile xtile = (IXTile) component;
|
76 |
| |
77 |
| |
78 |
0
| RequestContext context = cycle.getRequestContext();
|
79 |
0
| String[] params = context.getParameters(ServiceConstants.PARAMETER);
|
80 |
0
| cycle.setServiceParameters(params);
|
81 |
0
| xtile.trigger(cycle);
|
82 |
| |
83 |
| |
84 |
0
| Object[] args = cycle.getServiceParameters();
|
85 |
0
| String strArgs = generateOutputString(args);
|
86 |
0
| if (strArgs != null) {
|
87 |
0
| OutputStream output = _response.getOutputStream(new ContentType("text/xml"));
|
88 |
0
| output.write(strArgs.getBytes("utf-8"));
|
89 |
| } |
90 |
| } |
91 |
| |
92 |
0
| protected String generateOutputString(Object[] args)
|
93 |
| { |
94 |
0
| try {
|
95 |
0
| DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
96 |
0
| dbf.setValidating(false);
|
97 |
0
| DocumentBuilder db = dbf.newDocumentBuilder();
|
98 |
0
| Document doc = db.newDocument();
|
99 |
| |
100 |
0
| Node rootNode = doc.createElement("data");
|
101 |
0
| doc.appendChild(rootNode);
|
102 |
| |
103 |
0
| if (args != null) {
|
104 |
0
| for (int i = 0; i < args.length; i++) {
|
105 |
0
| Object value = args[i];
|
106 |
| |
107 |
0
| Node spNode = doc.createElement("sp");
|
108 |
0
| rootNode.appendChild(spNode);
|
109 |
| |
110 |
0
| Node valueNode = doc.createTextNode(value.toString());
|
111 |
0
| spNode.appendChild(valueNode);
|
112 |
| } |
113 |
| } |
114 |
| |
115 |
0
| TransformerFactory trf = TransformerFactory.newInstance();
|
116 |
0
| Transformer tr = trf.newTransformer();
|
117 |
0
| tr.setOutputProperty(OutputKeys.INDENT, "yes");
|
118 |
| |
119 |
0
| DOMSource domSrc = new DOMSource(doc);
|
120 |
0
| StringWriter writer = new StringWriter();
|
121 |
0
| StreamResult res = new StreamResult(writer);
|
122 |
0
| tr.transform(domSrc, res);
|
123 |
0
| writer.close();
|
124 |
| |
125 |
0
| return writer.toString();
|
126 |
| } |
127 |
| catch (Exception e) { |
128 |
0
| _exceptionReporter.reportRequestException("Cannot generate XML", e);
|
129 |
0
| return null;
|
130 |
| } |
131 |
| } |
132 |
| |
133 |
0
| public void setExceptionReporter(RequestExceptionReporter exceptionReporter)
|
134 |
| { |
135 |
0
| _exceptionReporter = exceptionReporter;
|
136 |
| } |
137 |
| |
138 |
0
| public void setResponse(WebResponse response)
|
139 |
| { |
140 |
0
| _response = response;
|
141 |
| } |
142 |
| |
143 |
0
| public static void main(String[] args) {
|
144 |
0
| XTileService objService = new XTileService();
|
145 |
0
| System.out.println(objService.generateOutputString(new Object[] { "test > work", new Integer(20) }));
|
146 |
| } |
147 |
| |
148 |
| |
149 |
| } |