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