001: package net.sf.saxon.instruct;
002:
003: import net.sf.saxon.event.SequenceReceiver;
004: import net.sf.saxon.expr.ExpressionTool;
005: import net.sf.saxon.expr.StaticContext;
006: import net.sf.saxon.expr.StaticProperty;
007: import net.sf.saxon.expr.XPathContext;
008: import net.sf.saxon.om.NamePool;
009: import net.sf.saxon.pattern.NodeKindTest;
010: import net.sf.saxon.style.StandardNames;
011: import net.sf.saxon.trans.DynamicError;
012: import net.sf.saxon.trans.XPathException;
013: import net.sf.saxon.type.ItemType;
014: import net.sf.saxon.type.TypeHierarchy;
015:
016: import java.io.PrintStream;
017:
018: /**
019: * An instruction representing an xsl:comment element in the stylesheet.
020: */
021:
022: public final class Comment extends SimpleNodeConstructor {
023:
024: /**
025: * Construct the instruction
026: */
027:
028: public Comment() {
029: }
030:
031: /**
032: * Get the instruction name, for diagnostics and tracing
033: * return the string "xsl:comment"
034: */
035:
036: public int getInstructionNameCode() {
037: return StandardNames.XSL_COMMENT;
038: }
039:
040: public ItemType getItemType(TypeHierarchy th) {
041: return NodeKindTest.COMMENT;
042: }
043:
044: public int getCardinality() {
045: return StaticProperty.EXACTLY_ONE;
046: }
047:
048: public void localTypeCheck(StaticContext env,
049: ItemType contextItemType) {
050: }
051:
052: /**
053: * Process this instruction, to output a Comment Node
054: * @param context the dynamic context for this transformation
055: * @return a TailCall representing a call delegated to the caller. Always
056: * returns null in this implementation
057: */
058:
059: public TailCall processLeavingTail(XPathContext context)
060: throws XPathException {
061: String comment = expandChildren(context).toString();
062: comment = checkContent(comment, context);
063: SequenceReceiver out = context.getReceiver();
064: out.comment(comment, locationId, 0);
065: return null;
066: }
067:
068: /**
069: * Check the content of the node, and adjust it if necessary
070: *
071: * @param comment the supplied content
072: * @param context the dynamic context
073: * @return the original content, unless adjustments are needed
074: * @throws net.sf.saxon.trans.DynamicError
075: * if the content is invalid
076: */
077:
078: protected String checkContent(String comment, XPathContext context)
079: throws DynamicError {
080: while (true) {
081: int hh = comment.indexOf("--");
082: if (hh < 0)
083: break;
084: if (isXSLT(context)) {
085: comment = comment.substring(0, hh + 1) + ' '
086: + comment.substring(hh + 1);
087: } else {
088: DynamicError err = new DynamicError(
089: "Invalid characters (--) in comment", this );
090: err.setErrorCode("XQDY0072");
091: err.setXPathContext(context);
092: throw DynamicError.makeDynamicError(dynamicError(this ,
093: err, context));
094: }
095: }
096: if (comment.length() > 0
097: && comment.charAt(comment.length() - 1) == '-') {
098: if (isXSLT(context)) {
099: comment = comment + ' ';
100: } else {
101: DynamicError err = new DynamicError(
102: "Comment cannot end in '-'", this );
103: err.setErrorCode("XQDY0072");
104: err.setXPathContext(context);
105: throw DynamicError.makeDynamicError(dynamicError(this ,
106: err, context));
107: }
108: }
109: return comment;
110: }
111:
112: public void display(int level, NamePool pool, PrintStream out) {
113: out.println(ExpressionTool.indent(level) + "comment");
114: super .display(level + 1, pool, out);
115: }
116:
117: }
118: //
119: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
120: // you may not use this file except in compliance with the License. You may obtain a copy of the
121: // License at http://www.mozilla.org/MPL/
122: //
123: // Software distributed under the License is distributed on an "AS IS" basis,
124: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
125: // See the License for the specific language governing rights and limitations under the License.
126: //
127: // The Original Code is: all this file.
128: //
129: // The Initial Developer of the Original Code is Michael H. Kay.
130: //
131: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
132: //
133: // Contributor(s): none.
134: //
|