KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > instruct > Comment


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.event.SequenceReceiver;
3 import net.sf.saxon.expr.ExpressionTool;
4 import net.sf.saxon.expr.StaticContext;
5 import net.sf.saxon.expr.StaticProperty;
6 import net.sf.saxon.expr.XPathContext;
7 import net.sf.saxon.om.NamePool;
8 import net.sf.saxon.pattern.NodeKindTest;
9 import net.sf.saxon.style.StandardNames;
10 import net.sf.saxon.trans.DynamicError;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.type.ItemType;
13
14 import java.io.PrintStream JavaDoc;
15
16
17 /**
18 * An instruction representing an xsl:comment element in the stylesheet.
19 */

20
21 public final class Comment extends SimpleNodeConstructor {
22
23     /**
24     * Construct the instruction
25     */

26
27     public Comment() {}
28
29     /**
30     * Get the instruction name, for diagnostics and tracing
31     * return the string "xsl:comment"
32     */

33
34     public int getInstructionNameCode() {
35         return StandardNames.XSL_COMMENT;
36     }
37
38     public ItemType getItemType() {
39         return NodeKindTest.COMMENT;
40     }
41
42     public int getCardinality() {
43         return StaticProperty.EXACTLY_ONE;
44     }
45
46     public void localTypeCheck(StaticContext env, ItemType contextItemType) {}
47
48
49     /**
50     * Process this instruction, to output a Comment Node
51     * @param context the dynamic context for this transformation
52     * @return a TailCall representing a call delegated to the caller. Always
53     * returns null in this implementation
54     */

55
56     public TailCall processLeavingTail(XPathContext context) throws XPathException {
57         String JavaDoc comment = expandChildren(context).toString();
58         comment = checkContent(comment, context);
59         SequenceReceiver out = context.getReceiver();
60         out.comment(comment, locationId, 0);
61         return null;
62     }
63
64     /**
65      * Check the content of the node, and adjust it if necessary
66      *
67      * @param comment the supplied content
68      * @param context the dynamic context
69      * @return the original content, unless adjustments are needed
70      * @throws net.sf.saxon.trans.DynamicError
71      * if the content is invalid
72      */

73
74     protected String JavaDoc checkContent(String JavaDoc comment, XPathContext context) throws DynamicError {
75         while(true) {
76             int hh = comment.indexOf("--");
77             if (hh < 0) break;
78             if (isXSLT(context)) {
79                 comment = comment.substring(0, hh+1) + ' ' + comment.substring(hh+1);
80             } else {
81                 DynamicError err = new DynamicError("Invalid characters (--) in comment", this);
82                 err.setErrorCode("XQDY0072");
83                 err.setXPathContext(context);
84                 throw DynamicError.makeDynamicError(dynamicError(this, err, context));
85             }
86         }
87         if (comment.length()>0 && comment.charAt(comment.length()-1)=='-') {
88             if (isXSLT(context)) {
89                 comment = comment + ' ';
90             } else {
91                 DynamicError err = new DynamicError("Comment cannot end in '-'", this);
92                 err.setErrorCode("XQDY0072");
93                 err.setXPathContext(context);
94                 throw DynamicError.makeDynamicError(dynamicError(this, err, context));
95             }
96         }
97         return comment;
98     }
99
100     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
101         out.println(ExpressionTool.indent(level) + "comment");
102         super.display(level+1, pool, out);
103     }
104
105 }
106 //
107
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
108
// you may not use this file except in compliance with the License. You may obtain a copy of the
109
// License at http://www.mozilla.org/MPL/
110
//
111
// Software distributed under the License is distributed on an "AS IS" basis,
112
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
113
// See the License for the specific language governing rights and limitations under the License.
114
//
115
// The Original Code is: all this file.
116
//
117
// The Initial Developer of the Original Code is Michael H. Kay.
118
//
119
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
120
//
121
// Contributor(s): none.
122
//
123
Popular Tags