KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > Serialize


1 package net.sf.saxon.functions;
2 import net.sf.saxon.event.SequenceReceiver;
3 import net.sf.saxon.expr.StaticContext;
4 import net.sf.saxon.expr.XPathContext;
5 import net.sf.saxon.om.Item;
6 import net.sf.saxon.om.NodeInfo;
7 import net.sf.saxon.om.Validation;
8 import net.sf.saxon.style.ExpressionContext;
9 import net.sf.saxon.trace.Location;
10 import net.sf.saxon.trans.DynamicError;
11 import net.sf.saxon.trans.StaticError;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.value.StringValue;
14
15 import javax.xml.transform.OutputKeys JavaDoc;
16 import javax.xml.transform.stream.StreamResult JavaDoc;
17 import java.io.StringWriter JavaDoc;
18 import java.util.Properties JavaDoc;
19
20
21 /**
22 * This class implements the saxon:serialize() extension function,
23 * which is specially-recognized by the system because it needs access
24 * to parts of the static context
25 */

26
27 public class Serialize extends SystemFunction implements XSLTFunction {
28
29     Properties JavaDoc outputProperties;
30     private transient boolean checked = false;
31         // the second time checkArguments is called, it's a global check so the static context is inaccurate
32

33     /**
34     * Method supplied by each class of function to check arguments during parsing, when all
35     * the argument expressions have been read
36     */

37
38     public void checkArguments(StaticContext env) throws XPathException {
39         if (checked) return;
40         checked = true;
41         super.checkArguments(env);
42         if (env instanceof ExpressionContext) {
43             // We're in XSLT
44

45             // We require the output format name to be known at compile time. If we allowed it
46
// to be dynamic, we would not only need to save the namespace context, but also to
47
// save details of all xsl:output declarations for use at run-time.
48

49             if (!(argument[1] instanceof StringValue)) {
50                 throw new StaticError("Second argument of saxon:serialize must be known at compile time");
51             }
52             String JavaDoc format = ((StringValue)argument[1]).getStringValue();
53             int fingerprint = -1;
54             if (!format.equals("")) {
55                 fingerprint = ((ExpressionContext)env).getFingerprint(format, false);
56                 if (fingerprint==-1) {
57                     throw new StaticError("Output format '" + format + "' has not been defined");
58                 }
59             }
60             outputProperties = ((ExpressionContext)env).getXSLStylesheet().gatherOutputProperties(fingerprint);
61         } else {
62             // we're not in XSLT: treat the second argument as the method property, default the rest
63
outputProperties = new Properties JavaDoc();
64             if (!(argument[1] instanceof StringValue)) {
65                 throw new StaticError("Second argument of saxon:serialize must be known at compile time");
66             }
67             outputProperties.setProperty(OutputKeys.METHOD, ((StringValue)argument[1]).getStringValue());
68         }
69     }
70
71     /**
72     * Evaluate the function
73     */

74
75     public Item evaluateItem(XPathContext c) throws XPathException {
76         NodeInfo node = (NodeInfo)argument[0].evaluateItem(c);
77         if (node==null) {
78             return StringValue.EMPTY_STRING;
79         }
80
81         try {
82             StringWriter JavaDoc result = new StringWriter JavaDoc();
83             XPathContext c2 = c.newMinorContext();
84             c.setOriginatingConstructType(Location.SAXON_SERIALIZE);
85
86             c2.changeOutputDestination(outputProperties,
87                                                new StreamResult JavaDoc(result),
88                                                false,
89                                                Validation.PRESERVE,
90                                                null);
91             SequenceReceiver out = c2.getReceiver();
92             out.open();
93             node.copy(out, NodeInfo.ALL_NAMESPACES, true, locationId);
94             out.close();
95             return new StringValue(result.toString());
96         } catch (XPathException err) {
97             throw new DynamicError(err);
98         }
99     }
100 }
101
102
103
104
105 //
106
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
107
// you may not use this file except in compliance with the License. You may obtain a copy of the
108
// License at http://www.mozilla.org/MPL/
109
//
110
// Software distributed under the License is distributed on an "AS IS" basis,
111
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
112
// See the License for the specific language governing rights and limitations under the License.
113
//
114
// The Original Code is: all this file.
115
//
116
// The Initial Developer of the Original Code is Michael H. Kay.
117
//
118
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
119
//
120
// Contributor(s): none.
121
//
122
Popular Tags