View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2003-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  
22  package com.sun.org.apache.xerces.internal.impl.xs.models;
23  
24  import com.sun.org.apache.xerces.internal.impl.Constants;
25  import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
26  import com.sun.org.apache.xerces.internal.impl.dtd.models.CMNode;
27  import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
28  import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager;
29  import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
30  import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
31  
32  /**
33   *
34   * @xerces.internal
35   *
36   * @author  Neeraj Bajaj
37   *
38   * @version $Id: CMNodeFactory.java,v 1.7 2010-11-01 04:39:58 joehw Exp $
39   */
40  public class CMNodeFactory {
41  
42  
43      /** Property identifier: error reporter. */
44      private static final String ERROR_REPORTER =
45          Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
46  
47      /** property identifier: security manager. */
48      private static final String SECURITY_MANAGER =
49          Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY;
50  
51      private static final boolean DEBUG = false ;
52  
53      //
54      private static final int MULTIPLICITY = 1 ;
55  
56      //count of number of nodes created
57      private int nodeCount = 0;
58  
59      //No. of nodes allowed.
60      private int maxNodeLimit ;
61  
62  
63      /**
64       * Error reporter. This property identifier is:
65       * http://apache.org/xml/properties/internal/error-reporter
66       */
67      private XMLErrorReporter fErrorReporter;
68  
69      // stores defaults for different security holes (maxOccurLimit in current context) if it has
70      // been set on the configuration.
71      private XMLSecurityManager fSecurityManager = null;
72  
73      /** default constructor */
74      public CMNodeFactory() {
75      }
76  
77      public void reset(XMLComponentManager componentManager){
78          fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER);
79          try {
80              fSecurityManager = (XMLSecurityManager)componentManager.getProperty(SECURITY_MANAGER);
81              //we are setting the limit of number of nodes to 3times the maxOccur value..
82              if(fSecurityManager != null){
83                  maxNodeLimit = fSecurityManager.getLimit(XMLSecurityManager.Limit.MAX_OCCUR_NODE_LIMIT) * MULTIPLICITY ;
84              }
85          }
86          catch (XMLConfigurationException e) {
87              fSecurityManager = null;
88          }
89  
90      }//reset()
91  
92      public CMNode getCMLeafNode(int type, Object leaf, int id, int position) {
93          return new XSCMLeaf(type, leaf, id, position) ;
94      }
95  
96      public CMNode getCMRepeatingLeafNode(int type, Object leaf,
97              int minOccurs, int maxOccurs, int id, int position) {
98          nodeCountCheck();
99          return new XSCMRepeatingLeaf(type, leaf, minOccurs, maxOccurs, id, position);
100     }
101 
102     public CMNode getCMUniOpNode(int type, CMNode childNode) {
103         nodeCountCheck();
104         return new XSCMUniOp(type, childNode) ;
105     }
106 
107     public CMNode getCMBinOpNode(int type, CMNode leftNode, CMNode rightNode) {
108         return new XSCMBinOp(type, leftNode, rightNode) ;
109     }
110 
111     public void nodeCountCheck(){
112         if( fSecurityManager != null && !fSecurityManager.isNoLimit(maxNodeLimit) &&
113                 nodeCount++ > maxNodeLimit){
114             if(DEBUG){
115                 System.out.println("nodeCount = " + nodeCount ) ;
116                 System.out.println("nodeLimit = " + maxNodeLimit ) ;
117             }
118             fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, "maxOccurLimit", new Object[]{ new Integer(maxNodeLimit) }, XMLErrorReporter.SEVERITY_FATAL_ERROR);
119             // similarly to entity manager behaviour, take into accont
120             // behaviour if continue-after-fatal-error is set.
121             nodeCount = 0;
122         }
123 
124     }//nodeCountCheck()
125 
126     //reset the node count
127     public void resetNodeCount(){
128         nodeCount = 0 ;
129     }
130         /**
131      * Sets the value of a property. This method is called by the component
132      * manager any time after reset when a property changes value.
133      * <p>
134      * <strong>Note:</strong> Components should silently ignore properties
135      * that do not affect the operation of the component.
136      *
137      * @param propertyId The property identifier.
138      * @param value      The value of the property.
139      *
140      * @throws SAXNotRecognizedException The component should not throw
141      *                                   this exception.
142      * @throws SAXNotSupportedException The component should not throw
143      *                                  this exception.
144      */
145     public void setProperty(String propertyId, Object value)
146         throws XMLConfigurationException {
147 
148         // Xerces properties
149         if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
150                 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
151 
152             if (suffixLength == Constants.SECURITY_MANAGER_PROPERTY.length() &&
153                 propertyId.endsWith(Constants.SECURITY_MANAGER_PROPERTY)) {
154                 fSecurityManager = (XMLSecurityManager)value;
155                 maxNodeLimit = (fSecurityManager != null) ?
156                         fSecurityManager.getLimit(XMLSecurityManager.Limit.MAX_OCCUR_NODE_LIMIT) * MULTIPLICITY : 0 ;
157                 return;
158             }
159             if (suffixLength == Constants.ERROR_REPORTER_PROPERTY.length() &&
160                 propertyId.endsWith(Constants.ERROR_REPORTER_PROPERTY)) {
161                 fErrorReporter = (XMLErrorReporter)value;
162                 return;
163             }
164         }
165 
166     } // setProperty(String,Object)
167 
168 }//CMNodeFactory()