KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > style > XSLGeneralOutput


1 package com.icl.saxon.style;
2 import com.icl.saxon.tree.AttributeCollection;
3 import com.icl.saxon.*;
4
5 import com.icl.saxon.om.NamePool;
6 import com.icl.saxon.om.Name;
7 import com.icl.saxon.om.Namespace;
8 import com.icl.saxon.om.NamespaceException;
9 import com.icl.saxon.expr.*;
10 import com.icl.saxon.output.*;
11 import org.xml.sax.*;
12 import java.io.*;
13 import java.util.*;
14 import java.text.*;
15 import javax.xml.transform.*;
16 import javax.xml.transform.sax.*;
17
18 /**
19 * Common superclass for the xsl:output and xsl:document (formerly saxon:output)
20 * elements <br>
21 */

22
23 abstract class XSLGeneralOutput extends StyleElement {
24
25     Expression href = null;
26     Expression userData = null;
27     Expression method = null;
28     Expression version = null;
29     Expression indent = null;
30     Expression encoding = null;
31     Expression mediaType = null;
32     Expression doctypeSystem = null;
33     Expression doctypePublic = null;
34     Expression omitDeclaration = null;
35     Expression standalone = null;
36     Expression cdataElements = null;
37     Expression omitMetaTag = null;
38     Expression nextInChain = null;
39     Expression representation = null;
40     Expression indentSpaces = null;
41     Hashtable userAttributes = null;
42
43     Emitter handler = null;
44
45     public void prepareAttributes() throws TransformerConfigurationException {
46
47         StandardNames sn = getStandardNames();
48         AttributeCollection atts = getAttributeList();
49                        
50         for (int a=0; a<atts.getLength(); a++) {
51             int nc = atts.getNameCode(a);
52             int f = nc & 0xfffff;
53             if (f==sn.HREF) {
54                 href = makeAttributeValueTemplate(atts.getValue(a));
55             } else if (f==sn.METHOD) {
56                 method = makeAttributeValueTemplate(atts.getValue(a));
57             } else if (f==sn.VERSION) {
58                 version = makeAttributeValueTemplate(atts.getValue(a));
59             } else if (f==sn.ENCODING) {
60                 encoding = makeAttributeValueTemplate(atts.getValue(a));
61             } else if (f==sn.OMIT_XML_DECLARATION) {
62                 omitDeclaration = makeAttributeValueTemplate(atts.getValue(a));
63             } else if (f==sn.STANDALONE) {
64                 standalone = makeAttributeValueTemplate(atts.getValue(a));
65             } else if (f==sn.DOCTYPE_PUBLIC) {
66                 doctypePublic = makeAttributeValueTemplate(atts.getValue(a));
67             } else if (f==sn.DOCTYPE_SYSTEM) {
68                 doctypeSystem = makeAttributeValueTemplate(atts.getValue(a));
69             } else if (f==sn.CDATA_SECTION_ELEMENTS) {
70                 cdataElements = makeAttributeValueTemplate(atts.getValue(a));
71             } else if (f==sn.INDENT) {
72                 indent = makeAttributeValueTemplate(atts.getValue(a));
73             } else if (f==sn.MEDIA_TYPE) {
74                 mediaType = makeAttributeValueTemplate(atts.getValue(a));
75             } else if (f==sn.SAXON_OMIT_META_TAG) {
76                 omitMetaTag = makeAttributeValueTemplate(atts.getValue(a));
77             } else if (f==sn.SAXON_CHARACTER_REPRESENTATION) {
78                 representation = makeAttributeValueTemplate(atts.getValue(a));
79             } else if (f==sn.SAXON_INDENT_SPACES) {
80                 indentSpaces = makeAttributeValueTemplate(atts.getValue(a));
81             } else if (f==sn.SAXON_NEXT_IN_CHAIN) {
82                 nextInChain = makeAttributeValueTemplate(atts.getValue(a));
83             } else {
84                 String attributeURI = getNamePool().getURI(nc);
85                 if ("".equals(attributeURI) ||
86                         Namespace.XSLT.equals(attributeURI) ||
87                         Namespace.SAXON.equals(attributeURI)) {
88                     checkUnknownAttribute(nc);
89                 } else {
90                     String name = "{" + attributeURI + "}" + atts.getLocalName(a);
91                     Expression val = makeAttributeValueTemplate(atts.getValue(a));
92                     if (userAttributes==null) {
93                         userAttributes = new Hashtable(5);
94                     }
95                     userAttributes.put(name, val);
96                 }
97             }
98         }
99     }
100
101     /**
102     * Evaluate any properties defined as attribute value templates; validate them;
103     * and return the values as additions to a set of Properties
104     */

105     
106     protected Properties updateOutputProperties(Properties details, Context context)
107     throws TransformerException {
108         if (method != null) {
109             String data = method.evaluateAsString(context);
110             if (data.equals("xml") || data.equals("html") || data.equals("text")) {
111                 details.put(OutputKeys.METHOD, data);
112             } else {
113                 int methodNameCode;
114                 NamePool pool = getNamePool();
115                 try {
116                     methodNameCode = makeNameCode(data, false);
117                 } catch (NamespaceException err) {
118                     throw styleError(err.getMessage());
119                 }
120                 if (pool.getURICode(methodNameCode)==0) {
121                     throw styleError("method must be xml, html, or text, or a prefixed name");
122                 }
123                 details.put(OutputKeys.METHOD,
124                     "{" + pool.getURI(methodNameCode) + "}" + pool.getLocalName(methodNameCode) );
125             }
126         }
127
128         if (version != null) {
129             String data = version.evaluateAsString(context);
130             details.put(OutputKeys.VERSION, data);
131         }
132
133         if (indent != null) {
134             String data = indent.evaluateAsString(context);
135             if (data==null || data.equals("yes") || data.equals("no")) {
136                 details.put(OutputKeys.INDENT, data);
137             } else {
138                 throw styleError("indent must be yes or no or an integer");
139             }
140         }
141         
142         if (indentSpaces != null) {
143             String data = indentSpaces.evaluateAsString(context);
144             try {
145                 int indentSpaces = Integer.parseInt(data);
146                 details.put(OutputKeys.INDENT, "yes");
147                 details.put(SaxonOutputKeys.INDENT_SPACES, data);
148             } catch (NumberFormatException err) {
149                 throw styleError("indent-spaces must be an integer");
150             }
151         }
152
153         if (encoding != null) {
154             String data = encoding.evaluateAsString(context);
155             details.put(OutputKeys.ENCODING, data);
156         }
157
158         if (mediaType != null) {
159             String data = mediaType.evaluateAsString(context);
160             details.put(OutputKeys.MEDIA_TYPE, data);
161         }
162
163         if (doctypeSystem != null) {
164             String data = doctypeSystem.evaluateAsString(context);
165             details.put(OutputKeys.DOCTYPE_SYSTEM, data);
166         }
167         
168         if (doctypePublic != null) {
169             String data = doctypePublic.evaluateAsString(context);
170             details.put(OutputKeys.DOCTYPE_PUBLIC, data);
171         }
172
173         if (omitDeclaration != null) {
174             String data = omitDeclaration.evaluateAsString(context);
175             if (data.equals("yes") || data.equals("no")) {
176                 details.put(OutputKeys.OMIT_XML_DECLARATION, data);
177             } else {
178                 throw styleError("omit-xml-declaration attribute must be yes or no");
179             }
180         }
181
182         if (standalone != null) {
183             String data = standalone.evaluateAsString(context);
184             if (data.equals("yes") || data.equals("no")) {
185                 details.put(OutputKeys.STANDALONE, data);
186             } else {
187                 throw styleError("standalone attribute must be yes or no");
188             }
189         }
190
191         if (cdataElements != null) {
192             String data = cdataElements.evaluateAsString(context);
193             String existing = details.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS);
194             String s = " ";
195             StringTokenizer st = new StringTokenizer(data);
196             NamePool pool = context.getController().getNamePool();
197             while (st.hasMoreTokens()) {
198                 String displayname = st.nextToken();
199                 if (!Name.isQName(displayname)) {
200                     throw styleError("CDATA element " + displayname + " is not a valid QName");
201                 }
202                 int namecode;
203                 try {
204                     namecode = makeNameCode(displayname, true);
205                 } catch (NamespaceException err) {
206                     throw styleError(err.getMessage());
207                 }
208                 s += " {" + pool.getURI(namecode) + '}' + pool.getLocalName(namecode);
209                 details.put(OutputKeys.CDATA_SECTION_ELEMENTS, existing+s);
210             }
211         }
212
213         if (representation != null) {
214             String data = representation.evaluateAsString(context);
215             details.put(SaxonOutputKeys.CHARACTER_REPRESENTATION, data);
216         }
217
218         if (omitMetaTag != null) {
219             String data = omitMetaTag.evaluateAsString(context);
220             if (data.equals("yes") || data.equals("no")) {
221                 details.put(SaxonOutputKeys.OMIT_META_TAG, data);
222             } else {
223                 throw styleError("saxon:omit-meta-tag attribute must be yes or no");
224             }
225         }
226
227         if (nextInChain != null) {
228             String data = nextInChain.evaluateAsString(context);
229             details.put(SaxonOutputKeys.NEXT_IN_CHAIN, data);
230             details.put(SaxonOutputKeys.NEXT_IN_CHAIN_BASE_URI, getSystemId());
231         }
232
233         // deal with user-defined attributes
234

235         if (userAttributes!=null) {
236             Enumeration enum = userAttributes.keys();
237             while (enum.hasMoreElements()) {
238                 String attName = (String)enum.nextElement();
239                 Expression exp = (Expression)userAttributes.get(attName);
240                 String data = exp.evaluateAsString(context);
241                 details.put(attName, data);
242             }
243         }
244         
245         return details;
246     }
247
248     /**
249     * Prepare another stylesheet to handle the output of this one
250     */

251
252     protected TransformerHandler prepareNextStylesheet(String href, Context context)
253     throws TransformerException {
254
255         //TODO: should cache the results, we are recompiling the referenced
256
//stylesheet each time it's used
257

258         TransformerFactoryImpl factory =
259             getPreparedStyleSheet().getTransformerFactory();
260         URIResolver resolver = context.getController().getURIResolver();
261         Source source = resolver.resolve(href, getSystemId());
262         SAXSource saxSource = factory.getSAXSource(source, true);
263
264         Templates next = factory.newTemplates(source);
265         TransformerHandler nextTransformer = factory.newTransformerHandler(next);
266         return nextTransformer;
267     }
268      
269 }
270
271 //
272
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
273
// you may not use this file except in compliance with the License. You may obtain a copy of the
274
// License at http://www.mozilla.org/MPL/
275
//
276
// Software distributed under the License is distributed on an "AS IS" basis,
277
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
278
// See the License for the specific language governing rights and limitations under the License.
279
//
280
// The Original Code is: all this file.
281
//
282
// The Initial Developer of the Original Code is
283
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
284
//
285
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
286
//
287
// Contributor(s): none.
288
//
289
Popular Tags