KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > SaxonOutputKeys


1 package net.sf.saxon.event;
2 import net.sf.saxon.Err;
3 import net.sf.saxon.om.XMLChar;
4 import net.sf.saxon.trans.DynamicError;
5
6 import javax.xml.transform.OutputKeys JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8
9 /**
10  * Provides string constants that can be used to set
11  * output properties for a Transformer, or to retrieve
12  * output properties from a Transformer or Templates object.
13  *
14  * These keys are private Saxon keys that supplement the standard keys
15  * defined in javax.xml.transform.OutputKeys. As well as Saxon extension
16  * attributes, the list includes new attributes defined in XSLT 2.0 which
17  * are not yet supported in JAXP
18  */

19
20 public class SaxonOutputKeys {
21
22     /**
23      * This class is not instantiated
24      */

25
26     private SaxonOutputKeys() {}
27
28     /**
29      * indentSpaces = integer.
30      *
31      * <p>Defines the number of spaces used for indentation of output</p>
32      */

33
34     public static final String JavaDoc INDENT_SPACES = "{http://saxon.sf.net/}indent-spaces";
35
36     /**
37      * stylesheet-version. This serialization parameter is set automatically by the XSLT processor
38      * to the value of the version attribute on the principal stylesheet module.
39      */

40
41     public static final String JavaDoc STYLESHEET_VERSION = "{http://saxon.sf.net/}stylesheet-version";
42
43     /**
44      * use-character-map = list-of-qnames.
45      *
46      * <p>Defines the character maps used in this output definition. The QNames
47      * are represented in Clark notation as {uri}local-name.</p>
48      */

49
50     public static final String JavaDoc USE_CHARACTER_MAPS = "use-character-maps";
51
52
53     /**
54      * include-content-type = "yes" | "no". This attribute is defined in XSLT 2.0
55      *
56      * <p>Indicates whether the META tag is to be added to HTML output</p>
57      */

58
59     public static final String JavaDoc INCLUDE_CONTENT_TYPE = "include-content-type";
60
61    /**
62      * undeclare-prefixes = "yes" | "no". This attribute is defined in XSLT 2.0
63      *
64      * <p>Indicates XML 1.1 namespace undeclarations are to be output when required</p>
65      */

66
67     public static final String JavaDoc UNDECLARE_PREFIXES = "undeclare-prefixes";
68
69     /**
70      * escape-uri-attributes = "yes" | "no". This attribute is defined in XSLT 2.0
71      *
72      * <p>Indicates whether HTML attributes of type URI are to be URI-escaped</p>
73      */

74
75     public static final String JavaDoc ESCAPE_URI_ATTRIBUTES = "escape-uri-attibutes";
76
77     /**
78      * representation = rep1[;rep2].
79      *
80      * <p>Indicates the preferred way of representing non-ASCII characters in HTML
81      * and XML output. rep1 is for characters in the range 128-256, rep2 for those
82      * above 256.</p>
83      */

84     public static final String JavaDoc CHARACTER_REPRESENTATION = "{http://saxon.sf.net/}character-representation";
85
86     /**
87      * saxon:next-in-chain = URI.
88      *
89      * <p>Indicates that the output is to be piped into another XSLT stylesheet
90      * to perform another transformation. The auxiliary property NEXT_IN_CHAIN_BASE_URI
91      * records the base URI of the stylesheet element where this attribute was found.</p>
92      */

93     public static final String JavaDoc NEXT_IN_CHAIN = "{http://saxon.sf.net/}next-in-chain";
94     public static final String JavaDoc NEXT_IN_CHAIN_BASE_URI = "{http://saxon.sf.net/}next-in-chain-base-uri";
95
96     /**
97     * byte-order-mark = yes|no.
98     *
99     * <p>Indicates whether UTF-8/UTF-16 output is to start with a byte order mark. Values are "yes" or "no",
100     * default is "no"
101     */

102
103     public static final String JavaDoc BYTE_ORDER_MARK = "byte-order-mark";
104
105     /**
106      * normalization-form = NFC|NFD|NFKC|NFKD|non.
107      *
108      * <p>Indicates that a given Unicode normalization form (or no normalization) is required.
109      */

110
111      public static final String JavaDoc NORMALIZATION_FORM = "normalization-form";
112
113
114     /**
115     * saxon:require-well-formed = yes|no.
116     *
117     * <p>Indicates whether a user-supplied ContentHandler requires the stream of SAX events to be
118     * well-formed (that is, to have a single element node and no text nodes as children of the root).
119     * The default is "no".</p>
120     */

121
122     public static final String JavaDoc REQUIRE_WELL_FORMED = "{http://saxon.sf.net/}require-well-formed";
123
124     /**
125      * wrap="yes"|"no".
126      * <p>
127      * This property is only available in the XQuery API. The value "yes" indicates that the result
128      * sequence produced by the query is to be wrapped, that is, each item in the result is represented
129      * as a separate element. This format allows any sequence to be represented as an XML document,
130      * including for example sequences consisting of parentless attribute nodes.
131      */

132
133     public static final String JavaDoc WRAP = "{http://saxon.sf.net/}require-well-formed";
134
135     /**
136      * Check that a supplied output property is valid.
137      * @param key the name of the property
138      * @param value the value of the property. This may be set to null, in which case
139      * only the property name is checked.
140      */

141
142     public static final void checkOutputProperty(String JavaDoc key, String JavaDoc value) throws DynamicError {
143         if (!key.startsWith("{") || key.startsWith("{http://saxon.sf.net/}" )) {
144             if (key.equals(OutputKeys.CDATA_SECTION_ELEMENTS)) {
145                 if (value != null) {
146                     checkListOfClarkNames(key, value);
147                 }
148             } else if (key.equals(OutputKeys.DOCTYPE_PUBLIC)) {
149                 // no constraints
150
} else if (key.equals(OutputKeys.DOCTYPE_SYSTEM)) {
151                 // no constraints
152
} else if (key.equals(OutputKeys.ENCODING)) {
153                 // no constraints
154
} else if (key.equals(OutputKeys.INDENT)) {
155                 if (value != null) {
156                     checkYesOrNo(key, value);
157                 }
158             } else if (key.equals(OutputKeys.MEDIA_TYPE)) {
159                 // no constraints
160
} else if (key.equals(OutputKeys.METHOD)) {
161                 if (value != null) {
162                     checkMethod(value);
163                 }
164             } else if (key.equals(OutputKeys.OMIT_XML_DECLARATION)) {
165                 if (value != null) {
166                     checkYesOrNo(key, value);
167                 }
168             } else if (key.equals(OutputKeys.STANDALONE)) {
169                 if (value != null && !value.equals("omit")) {
170                     checkYesOrNo(key, value);
171                 }
172             } else if (key.equals(OutputKeys.VERSION)) {
173                 // no constraints
174
} else if (key.equals(STYLESHEET_VERSION)) {
175                 // no constraints
176
} else if (key.equals(INDENT_SPACES)) {
177                 if (value != null) {
178                     checkNonNegativeInteger(key, value);
179                 }
180             } else if (key.equals(INCLUDE_CONTENT_TYPE)) {
181                 if (value != null) {
182                     checkYesOrNo(key, value);
183                 }
184             } else if (key.equals(ESCAPE_URI_ATTRIBUTES)) {
185                 if (value != null) {
186                     checkYesOrNo(key, value);
187                 }
188             } else if (key.equals(CHARACTER_REPRESENTATION)) {
189                 // no validation performed
190
} else if (key.equals(NEXT_IN_CHAIN)) {
191                 // no validation performed
192
} else if (key.equals(NEXT_IN_CHAIN_BASE_URI)) {
193                 // no validation performed
194
} else if (key.equals(UNDECLARE_PREFIXES)) {
195                 if (value != null) {
196                     checkYesOrNo(key, value);
197                 }
198             } else if (key.equals(USE_CHARACTER_MAPS)) {
199                 if (value != null) {
200                     checkListOfClarkNames(key, value);
201                 }
202             } else if (key.equals(REQUIRE_WELL_FORMED)) {
203                 if (value != null) {
204                     checkYesOrNo(key, value);
205                 }
206             } else if (key.equals(BYTE_ORDER_MARK)) {
207                 if (value != null) {
208                     checkYesOrNo(key, value);
209                 }
210             } else if (key.equals(WRAP)) {
211                 if (value != null) {
212                     checkYesOrNo(key, value);
213                 }
214             } else {
215                 throw new DynamicError("Unknown serialization parameter " + Err.wrap(key));
216             }
217         } else {
218             return;
219         }
220     }
221
222     private static void checkYesOrNo(String JavaDoc key, String JavaDoc value) throws DynamicError {
223         if ("yes".equals(value) || "no".equals(value)) {
224             // OK
225
} else {
226             throw new DynamicError("Serialization parameter " + Err.wrap(key) + " must have the value yes or no");
227         }
228     }
229
230     private static void checkMethod(String JavaDoc value) throws DynamicError {
231         if ("xml".equals(value)) return;
232         if ("html".equals(value)) return;
233         if ("xhtml".equals(value)) return;
234         if ("text".equals(value)) return;
235         if (isValidClarkName(value)) return;
236         throw new DynamicError("Invalid value for serialization method: " +
237                 "must be xml, html, xhtml, text, or a QName in '{uri}local' form");
238
239     }
240
241     private static boolean isValidClarkName(String JavaDoc value) {
242         if (value.charAt(0) != '{') return false;
243         int closer = value.indexOf('}');
244         if (closer < 2) return false;
245         if (closer == value.length()-1) return false;
246         if (!XMLChar.isValidNCName(value.substring(closer+1))) return false;
247         return true;
248     }
249
250     private static void checkNonNegativeInteger(String JavaDoc key, String JavaDoc value) throws DynamicError {
251         try {
252             int n = Integer.parseInt(value);
253             if (n < 0) {
254                 throw new DynamicError("Value of " + Err.wrap(key) + " must be a non-negative integer");
255             }
256         } catch (NumberFormatException JavaDoc err) {
257             throw new DynamicError("Value of " + Err.wrap(key) + " must be a non-negative integer");
258         }
259     }
260
261     private static void checkListOfClarkNames(String JavaDoc key, String JavaDoc value) throws DynamicError {
262         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(value);
263         while (tok.hasMoreTokens()) {
264             String JavaDoc s = tok.nextToken();
265             if (isValidClarkName(s) || XMLChar.isValidNCName(s)) {
266                 // ok
267
} else {
268                 throw new DynamicError("Value of " + Err.wrap(key) +
269                         " must be a list of QNames in '{uri}local' notation");
270             }
271         }
272     }
273
274 }
275
276 //
277
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
278
// you may not use this file except in compliance with the License. You may obtain a copy of the
279
// License at http://www.mozilla.org/MPL/
280
//
281
// Software distributed under the License is distributed on an "AS IS" basis,
282
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
283
// See the License for the specific language governing rights and limitations under the License.
284
//
285
// The Original Code is: all this file.
286
//
287
// The Initial Developer of the Original Code is Michael H. Kay.
288
//
289
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
290
//
291
// Contributor(s): none.
292
//
Popular Tags