001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: ElemComment.java 468643 2006-10-28 06:56:03Z minchau $
020 */
021 package org.apache.xalan.templates;
022
023 import javax.xml.transform.TransformerException;
024
025 import org.apache.xalan.res.XSLTErrorResources;
026 import org.apache.xalan.transformer.TransformerImpl;
027
028 /**
029 * Implement xsl:comment.
030 * <pre>
031 * <!ELEMENT xsl:comment %char-template;>
032 * <!ATTLIST xsl:comment %space-att;>
033 * </pre>
034 * @see <a href="http://www.w3.org/TR/xslt#section-Creating-Comments">section-Creating-Comments in XSLT Specification</a>
035 * @xsl.usage advanced
036 */
037 public class ElemComment extends ElemTemplateElement
038 {
039 static final long serialVersionUID = -8813199122875770142L;
040
041 /**
042 * Get an int constant identifying the type of element.
043 * @see org.apache.xalan.templates.Constants
044 *
045 * @return The token ID for this element
046 */
047 public int getXSLToken()
048 {
049 return Constants.ELEMNAME_COMMENT;
050 }
051
052 /**
053 * Return the node name.
054 *
055 * @return This element's name
056 */
057 public String getNodeName()
058 {
059 return Constants.ELEMNAME_COMMENT_STRING;
060 }
061
062 /**
063 * Execute the xsl:comment transformation
064 *
065 *
066 * @param transformer non-null reference to the the current transform-time state.
067 *
068 * @throws TransformerException
069 */
070 public void execute(
071 TransformerImpl transformer)
072 throws TransformerException
073 {
074 if (transformer.getDebug())
075 transformer.getTraceManager().fireTraceEvent(this);
076 try
077 {
078 // Note the content model is:
079 // <!ENTITY % instructions "
080 // %char-instructions;
081 // | xsl:processing-instruction
082 // | xsl:comment
083 // | xsl:element
084 // | xsl:attribute
085 // ">
086 String data = transformer.transformToString(this);
087
088 transformer.getResultTreeHandler().comment(data);
089 }
090 catch(org.xml.sax.SAXException se)
091 {
092 throw new TransformerException(se);
093 }
094 finally
095 {
096 if (transformer.getDebug())
097 transformer.getTraceManager().fireTraceEndEvent(this);
098 }
099 }
100
101 /**
102 * Add a child to the child list.
103 *
104 * @param newChild Child to add to this node's child list
105 *
106 * @return Child that was just added to child list
107 *
108 * @throws DOMException
109 */
110 public ElemTemplateElement appendChild(ElemTemplateElement newChild)
111 {
112
113 int type = ((ElemTemplateElement) newChild).getXSLToken();
114
115 switch (type)
116 {
117
118 // char-instructions
119 case Constants.ELEMNAME_TEXTLITERALRESULT :
120 case Constants.ELEMNAME_APPLY_TEMPLATES :
121 case Constants.ELEMNAME_APPLY_IMPORTS :
122 case Constants.ELEMNAME_CALLTEMPLATE :
123 case Constants.ELEMNAME_FOREACH :
124 case Constants.ELEMNAME_VALUEOF :
125 case Constants.ELEMNAME_COPY_OF :
126 case Constants.ELEMNAME_NUMBER :
127 case Constants.ELEMNAME_CHOOSE :
128 case Constants.ELEMNAME_IF :
129 case Constants.ELEMNAME_TEXT :
130 case Constants.ELEMNAME_COPY :
131 case Constants.ELEMNAME_VARIABLE :
132 case Constants.ELEMNAME_MESSAGE :
133
134 // instructions
135 // case Constants.ELEMNAME_PI:
136 // case Constants.ELEMNAME_COMMENT:
137 // case Constants.ELEMNAME_ELEMENT:
138 // case Constants.ELEMNAME_ATTRIBUTE:
139 break;
140 default :
141 error(XSLTErrorResources.ER_CANNOT_ADD,
142 new Object[]{ newChild.getNodeName(),
143 this.getNodeName() }); //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
144
145 //" to " + this.m_elemName);
146 }
147
148 return super.appendChild(newChild);
149 }
150 }