1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2003 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. */
  51. package com.sun.org.apache.xerces.internal.impl.xs.models;
  52. import com.sun.org.apache.xerces.internal.impl.Constants;
  53. import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
  54. import com.sun.org.apache.xerces.internal.impl.dtd.models.CMNode;
  55. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  56. /**
  57. * <p>Creates nodes.</p>
  58. *
  59. * @author Neeraj Bajaj
  60. * @version $Revision: 1.2 $, $Date: 2004/01/22 20:36:54 $
  61. */
  62. public class CMNodeFactory {
  63. /**
  64. * Property identifier: error reporter.
  65. */
  66. private static final String ERROR_REPORTER =
  67. Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
  68. /**
  69. * <p>Output extra debugging messages to {@link System.err}.</p>
  70. */
  71. private static final boolean DEBUG = false;
  72. /**
  73. * <p>Count of number of nodes created.</p>
  74. */
  75. private int nodeCount = 0;
  76. /**
  77. * Error reporter. This property identifier is:
  78. * http://apache.org/xml/properties/internal/error-reporter
  79. */
  80. private XMLErrorReporter fErrorReporter = null;
  81. /**
  82. * Default constructor.
  83. */
  84. public CMNodeFactory() {
  85. if (DEBUG) {
  86. System.err.println("CMNodeFactory()");
  87. }
  88. }
  89. /**
  90. * <p>Reset internal state using <code>componentManager</code> provided values.</p>
  91. *
  92. * @param componentManager {@link XMLComponentManager} to provide new values for internal state.
  93. */
  94. public void reset(XMLComponentManager componentManager) {
  95. if (DEBUG) {
  96. System.err.println("CMNodeFactory#reset("
  97. + "componentManager[" + componentManager.toString() + "])");
  98. }
  99. // error reporter
  100. fErrorReporter = (XMLErrorReporter) componentManager.getProperty(ERROR_REPORTER);
  101. } //reset()
  102. /**
  103. * <p>Create a new leaf node as defined by the params.</p>
  104. *
  105. * @param type Type of leaf node to return.
  106. * @param leaf Leaf <code>Object</code>
  107. * @param id ID of leaf to return.
  108. * @param position Position of leaf to return.
  109. *
  110. * @return New node as defined by the params.
  111. */
  112. public CMNode getCMLeafNode(int type, Object leaf, int id, int position) {
  113. // this is a new node
  114. nodeCount++;
  115. if (DEBUG) {
  116. System.err.println("CMNodeFactory#getCMLeafNode("
  117. + "type[" + type + "], "
  118. + "leaf[" + leaf.toString() + "], "
  119. + "id[" + id + "], "
  120. + "position[" + position + "])\n"
  121. + "\tnodeCount=" + nodeCount);
  122. }
  123. // create new node as defined by the params
  124. return new XSCMLeaf(type, leaf, id, position);
  125. }
  126. /**
  127. * <p>Create a leaf node as defined by the params.</p>
  128. *
  129. * @param type Type of node to create.
  130. * @param childNode Child node.
  131. *
  132. * @return New node as defined by the params.
  133. */
  134. public CMNode getCMUniOpNode(int type, CMNode childNode) {
  135. // this is a new node
  136. nodeCount++;
  137. if (DEBUG) {
  138. System.err.println("CMNodeFactory#getCMUniOpNode("
  139. + "type[" + type + "], "
  140. + "childNode[" + childNode.toString() + "])\n"
  141. + "\tnodeCount=" + nodeCount);
  142. }
  143. // create new node as defined by the params
  144. return new XSCMUniOp(type, childNode);
  145. }
  146. /**
  147. * <p>Create a leaf node as defined by the params.</p>
  148. *
  149. * @param type Type of node to create.
  150. * @param leftNode Left node.
  151. * @param rightNode Right node.
  152. *
  153. * @return New node as defined by the params.
  154. */
  155. public CMNode getCMBinOpNode(int type, CMNode leftNode, CMNode rightNode) {
  156. // this is a new node
  157. nodeCount++;
  158. if (DEBUG) {
  159. System.err.println("CMNodeFactory#getCMBinOpNode("
  160. + "type[" + type + "], "
  161. + "leftNode[" + leftNode.toString() + "], "
  162. + "rightNode[" + rightNode.toString() + "])\n"
  163. + "\tnodeCount=" + nodeCount);
  164. }
  165. // create new node as defined by the params
  166. return new XSCMBinOp(type, leftNode, rightNode);
  167. }
  168. /**
  169. * <p>Reset the internal node count to 0.</p>
  170. */
  171. public void resetNodeCount() {
  172. nodeCount = 0;
  173. if (DEBUG) {
  174. System.err.println("CMNodeFactory#resetNodeCount: "
  175. + "nodeCount=" + nodeCount + " (after reset)");
  176. }
  177. }
  178. /**
  179. * <p>Sets the value of a property. This method is called by the component
  180. * manager any time after reset when a property changes value.</p>
  181. *
  182. * <p> <strong>Note:</strong> Components should silently ignore properties
  183. * that do not affect the operation of the component.</p>
  184. *
  185. * @param propertyId The property identifier.
  186. * @param value The value of the property.
  187. */
  188. public void setProperty(String propertyId, Object value) {
  189. if (DEBUG) {
  190. System.err.println("CMNodeFactory#setProperty("
  191. + "propertyId[" + propertyId + "], "
  192. + "value[" + value.toString() + "])");
  193. }
  194. // Xerces properties?
  195. if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
  196. String property = propertyId.substring(Constants.XERCES_PROPERTY_PREFIX.length());
  197. // error reporter?
  198. if (property.equals(Constants.ERROR_REPORTER_PROPERTY)) {
  199. fErrorReporter = (XMLErrorReporter) value;
  200. return;
  201. }
  202. // silently ignore unknown Xerces property
  203. return;
  204. } else {
  205. // silently ignore unknown non-Xerces property
  206. return;
  207. }
  208. } // setProperty(String,Object)
  209. } // CMNodeFactory()