KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > models > CMNodeFactory


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.xerces.impl.xs.models;
19
20 import org.apache.xerces.impl.XMLErrorReporter;
21 import org.apache.xerces.xni.parser.XMLComponentManager;
22 import org.apache.xerces.util.SecurityManager ;
23 import org.apache.xerces.impl.dtd.models.CMNode;
24 import org.apache.xerces.xni.parser.XMLConfigurationException;
25 import org.apache.xerces.impl.xs.XSMessageFormatter;
26 import org.apache.xerces.impl.Constants;
27
28 /**
29  *
30  * @xerces.internal
31  *
32  * @author Neeraj Bajaj
33  *
34  */

35 public class CMNodeFactory {
36     
37
38     /** Property identifier: error reporter. */
39     private static final String JavaDoc ERROR_REPORTER =
40         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
41     
42     /** property identifier: security manager. */
43     private static final String JavaDoc SECURITY_MANAGER =
44         Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY;
45
46     private static final boolean DEBUG = false ;
47     
48     //
49
private static final int MULTIPLICITY = 1 ;
50
51     //count of number of nodes created
52
private int nodeCount = 0;
53     
54     //No. of nodes allowed.
55
private int maxNodeLimit ;
56
57     
58     /**
59      * Error reporter. This property identifier is:
60      * http://apache.org/xml/properties/internal/error-reporter
61      */

62     private XMLErrorReporter fErrorReporter;
63
64     // stores defaults for different security holes (maxOccurLimit in current context) if it has
65
// been set on the configuration.
66
private SecurityManager JavaDoc fSecurityManager = null;
67     
68     /** default constructor */
69     public CMNodeFactory() {
70     }
71     
72     public void reset(XMLComponentManager componentManager){
73         fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER);
74         try {
75             fSecurityManager = (SecurityManager JavaDoc)componentManager.getProperty(SECURITY_MANAGER);
76             //we are setting the limit of number of nodes to 3times the maxOccur value..
77
if(fSecurityManager != null){
78                 maxNodeLimit = fSecurityManager.getMaxOccurNodeLimit() * MULTIPLICITY ;
79             }
80         }
81         catch (XMLConfigurationException e) {
82             fSecurityManager = null;
83         }
84         
85     }//reset()
86

87     public CMNode getCMLeafNode(int type, Object JavaDoc leaf, int id, int position) {
88         nodeCountCheck() ;
89         return new XSCMLeaf(type, leaf, id, position) ;
90     }
91     
92     public CMNode getCMUniOpNode(int type, CMNode childNode) {
93         nodeCountCheck();
94         return new XSCMUniOp(type, childNode) ;
95     }
96     
97     public CMNode getCMBinOpNode(int type, CMNode leftNode, CMNode rightNode) {
98         nodeCountCheck() ;
99         return new XSCMBinOp(type, leftNode, rightNode) ;
100     }
101     
102     public void nodeCountCheck(){
103         if( fSecurityManager != null && nodeCount++ > maxNodeLimit){
104             if(DEBUG){
105                 System.out.println("nodeCount = " + nodeCount ) ;
106                 System.out.println("nodeLimit = " + maxNodeLimit ) ;
107             }
108             fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, "maxOccurLimit", new Object JavaDoc[]{ new Integer JavaDoc(maxNodeLimit) }, XMLErrorReporter.SEVERITY_FATAL_ERROR);
109             // similarly to entity manager behaviour, take into accont
110
// behaviour if continue-after-fatal-error is set.
111
nodeCount = 0;
112         }
113         
114     }//nodeCountCheck()
115

116     //reset the node count
117
public void resetNodeCount(){
118         nodeCount = 0 ;
119     }
120         /**
121      * Sets the value of a property. This method is called by the component
122      * manager any time after reset when a property changes value.
123      * <p>
124      * <strong>Note:</strong> Components should silently ignore properties
125      * that do not affect the operation of the component.
126      *
127      * @param propertyId The property identifier.
128      * @param value The value of the property.
129      *
130      * @throws SAXNotRecognizedException The component should not throw
131      * this exception.
132      * @throws SAXNotSupportedException The component should not throw
133      * this exception.
134      */

135     public void setProperty(String JavaDoc propertyId, Object JavaDoc value)
136         throws XMLConfigurationException {
137
138         // Xerces properties
139
if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
140             final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
141             
142             if (suffixLength == Constants.SECURITY_MANAGER_PROPERTY.length() &&
143                 propertyId.endsWith(Constants.SECURITY_MANAGER_PROPERTY)) {
144                 fSecurityManager = (SecurityManager JavaDoc)value;
145                 maxNodeLimit = (fSecurityManager != null) ? fSecurityManager.getMaxOccurNodeLimit() * MULTIPLICITY : 0 ;
146                 return;
147             }
148             if (suffixLength == Constants.ERROR_REPORTER_PROPERTY.length() &&
149                 propertyId.endsWith(Constants.ERROR_REPORTER_PROPERTY)) {
150                 fErrorReporter = (XMLErrorReporter)value;
151                 return;
152             }
153         }
154
155     } // setProperty(String,Object)
156

157 }//CMNodeFactory()
158
Popular Tags