1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.transform;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.Writer;
27 import java.net.URI;
28 import java.net.URL;
29
30 import javax.faces.component.UIComponentBase;
31 import javax.faces.context.FacesContext;
32 import javax.faces.el.ValueBinding;
33 import javax.xml.transform.Transformer;
34 import javax.xml.transform.TransformerException;
35 import javax.xml.transform.TransformerFactory;
36 import javax.xml.transform.stream.StreamResult;
37 import javax.xml.transform.stream.StreamSource;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public abstract class AbstractXmlTransform extends UIComponentBase
53 {
54 public static final String COMPONENT_TYPE = "org.apache.myfaces.tomahawk.XmlTransform";
55 public static final String COMPONENT_FAMILY = "org.apache.myfaces.tomahawk.Transform";
56
57 private Object contentStream;
58 private Object styleStream;
59
60
61 public String getFamily()
62 {
63 return COMPONENT_FAMILY;
64 }
65
66 public void encodeBegin(FacesContext context)
67 throws IOException
68 {
69 InputStream xmlStream = (InputStream)getContentStream();
70 String xml = getContent();
71 String xmlLocation = getContentLocation();
72
73 InputStream xslStream = (InputStream)getStyleStream();
74 String xsl = getStylesheet();
75 String xslLocation = getStylesheetLocation();
76
77 if (context == null) throw new NullPointerException("context");
78 if (!isRendered()) return;
79
80 if (getContent() == null && getContentLocation() == null && getContentStream() == null)
81 throw new NullPointerException("content/contentLocation/contentStream cannot all be null");
82
83
84 if (xmlLocation != null)
85 {
86 ClassLoader loader = Thread.currentThread().getContextClassLoader();
87 if (loader == null)
88 {
89 loader = AbstractXmlTransform.class.getClassLoader();
90 }
91
92 URL url = loader.getResource(xmlLocation);
93 xmlStream = new FileInputStream(new File(URI.create(url.toString())));
94 }
95
96 if (xslLocation != null)
97 {
98 ClassLoader loader = Thread.currentThread().getContextClassLoader();
99 if (loader == null)
100 {
101 loader = AbstractXmlTransform.class.getClassLoader();
102 }
103
104 URL url = loader.getResource(xslLocation);
105 xslStream = new FileInputStream(new File(URI.create(url.toString())));
106 }
107
108 if (xml != null)
109 {
110 xmlStream = new ByteArrayInputStream(xml.getBytes());
111 }
112
113 if (xsl != null)
114 {
115 xslStream = new ByteArrayInputStream(xsl.getBytes());
116 }
117
118 if (xmlStream != null && xslStream != null)
119 {
120 transformContent(xmlStream, xslStream);
121 }
122 }
123
124
125
126
127
128
129
130
131 private void transformContent(InputStream content, InputStream stylesheet)
132 throws IOException
133 {
134 try
135 {
136 TransformerFactory tFactory = TransformerFactory.newInstance();
137 Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheet));
138
139 Writer responseWriter = FacesContext.getCurrentInstance().getResponseWriter();
140 transformer.transform(new StreamSource(content), new StreamResult(responseWriter));
141 }
142 catch (TransformerException te)
143 {
144 throw new IOException("Error while transforming XML: " + te.getMessage());
145 }
146 }
147
148
149 public void encodeChildren(FacesContext context)
150 throws IOException
151 {}
152
153
154 public void encodeEnd(FacesContext context)
155 throws IOException
156 {}
157
158
159
160
161
162
163 public abstract String getContent();
164
165
166
167
168
169
170 public abstract String getContentLocation();
171
172
173
174
175
176
177 public abstract String getStylesheet();
178
179 public void setContentStream(Object contentStream)
180 {
181 this.contentStream = contentStream;
182 }
183
184
185
186
187
188
189 public Object getContentStream()
190 {
191 if (contentStream != null) return contentStream;
192
193 ValueBinding vb = getValueBinding("contentStream");
194 return (vb != null) ? vb.getValue(getFacesContext()) : null;
195 }
196
197
198
199
200
201
202
203 public abstract String getStylesheetLocation();
204
205 public void setStyleStream(Object styleStream)
206 {
207 this.styleStream = styleStream;
208 }
209
210
211
212
213
214
215 public Object getStyleStream()
216 {
217 if (styleStream != null) return styleStream;
218
219 ValueBinding vb = getValueBinding("styleStream");
220 return (vb != null) ? vb.getValue(getFacesContext()) : null;
221 }
222
223 }