KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > node > runtime > web > ConstraintFieldNode


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment.node.runtime.web;
25
26 import java.util.Map JavaDoc;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30
31 import com.sun.enterprise.deployment.runtime.RuntimeDescriptor;
32 import com.sun.enterprise.deployment.runtime.web.ConstraintField;
33 import com.sun.enterprise.deployment.xml.RuntimeTagNames;
34 import com.sun.enterprise.deployment.node.XMLElement;
35
36 /**
37 * node for cache-mapping tag
38 *
39 * @author Jerome Dochez
40 */

41 public class ConstraintFieldNode extends WebRuntimeNode {
42         
43     /**
44      * all sub-implementation of this class can use a dispatch table to map xml element to
45      * method name on the descriptor class for setting the element value.
46      *
47      * @return the map with the element name as a key, the setter method as a value
48      */

49     protected Map JavaDoc getDispatchTable() {
50     Map JavaDoc dispatchTable = super.getDispatchTable();
51         // for backward compatibility with S1AS 7 dtd
52
dispatchTable.put(RuntimeTagNames.VALUE, "addValue");
53         dispatchTable.put(RuntimeTagNames.CONSTRAINT_FIELD_VALUE, "addValue");
54     return dispatchTable;
55     }
56
57     public void startElement(XMLElement element, Attributes JavaDoc attributes) {
58         if (element.getQName().equals(RuntimeTagNames.CONSTRAINT_FIELD)) {
59             ConstraintField descriptor =
60                 (ConstraintField) getRuntimeDescriptor();
61             for (int i=0; i<attributes.getLength();i++) {
62                 if (RuntimeTagNames.NAME.equals(attributes.getQName(i))) {
63                     descriptor.setAttributeValue(ConstraintField.NAME,
64                         attributes.getValue(i));
65                 } else
66                 if (RuntimeTagNames.SCOPE.equals(attributes.getQName(i))) {
67                     descriptor.setAttributeValue(ConstraintField.SCOPE,
68                         attributes.getValue(i));
69                 } else
70                 if (RuntimeTagNames.CACHE_ON_MATCH.equals(
71                     attributes.getQName(i))) {
72                     descriptor.setAttributeValue(
73                         ConstraintField.CACHE_ON_MATCH,
74                         attributes.getValue(i));
75                 } else
76                 if (RuntimeTagNames.CACHE_ON_MATCH_FAILURE.equals(
77                     attributes.getQName(i))) {
78                     descriptor.setAttributeValue(
79                         ConstraintField.CACHE_ON_MATCH_FAILURE,
80                         attributes.getValue(i));
81                 }
82             }
83         // From sun-web-app_2_3-0.dtd to sun-web-app_2_4-0.dtd,
84
// the element name "value" is changed to "constraint-field-value",
85
// need to make sure both will work
86
} else if (element.getQName().equals(RuntimeTagNames.VALUE) ||
87             element.getQName().equals(RuntimeTagNames.CONSTRAINT_FIELD_VALUE)) {
88             ConstraintField descriptor =
89                 (ConstraintField) getRuntimeDescriptor();
90             int index = descriptor.sizeValue();
91             for (int i=0; i<attributes.getLength();i++) {
92                 if (RuntimeTagNames.MATCH_EXPR.equals(
93                     attributes.getQName(i))) {
94                     descriptor.setAttributeValue(ConstraintField.VALUE,
95                         index, ConstraintField.MATCH_EXPR,
96                         attributes.getValue(i));
97                 } else
98                 if (RuntimeTagNames.CACHE_ON_MATCH.equals(
99                     attributes.getQName(i))) {
100                     descriptor.setAttributeValue(ConstraintField.VALUE,
101                         index, ConstraintField.CACHE_ON_MATCH,
102                         attributes.getValue(i));
103                 } else
104                 if (RuntimeTagNames.CACHE_ON_MATCH_FAILURE.equals(
105                     attributes.getQName(i))) {
106                     descriptor.setAttributeValue(ConstraintField.VALUE,
107                         index, ConstraintField.CACHE_ON_MATCH_FAILURE,
108                         attributes.getValue(i));
109                 }
110             }
111         } else super.startElement(element, attributes);
112     }
113     
114     /**
115      * write the descriptor class to a DOM tree and return it
116      *
117      * @param parent node for the DOM tree
118      * @param node name
119      * @param the array of descriptor to write
120      * @return the DOM tree top node
121      */

122     public void writeDescriptor(Node JavaDoc parent, String JavaDoc nodeName, ConstraintField[] descriptors) {
123     for (int i=0;i<descriptors.length;i++) {
124         writeDescriptor(parent, nodeName, descriptors[i]);
125     }
126     }
127     
128     /**
129      * write the descriptor class to a DOM tree and return it
130      *
131      * @param parent node for the DOM tree
132      * @param node name
133      * @param the descriptor to write
134      * @return the DOM tree top node
135      */

136     public Node JavaDoc writeDescriptor(Node JavaDoc parent, String JavaDoc nodeName, ConstraintField descriptor) {
137     
138     Element JavaDoc constraintField = (Element JavaDoc) super.writeDescriptor(parent, nodeName, descriptor);
139     
140     // value*
141
String JavaDoc[] values = descriptor.getValue();
142     for (int i=0;i<values.length;i++) {
143         Element JavaDoc value = (Element JavaDoc) appendTextChild(constraintField, RuntimeTagNames.CONSTRAINT_FIELD_VALUE, values[i]);
144         setAttribute(value, RuntimeTagNames.MATCH_EXPR, (String JavaDoc) descriptor.getAttributeValue(ConstraintField.VALUE, i, ConstraintField.MATCH_EXPR));
145         setAttribute(value, RuntimeTagNames.CACHE_ON_MATCH, (String JavaDoc) descriptor.getAttributeValue(ConstraintField.VALUE, i, ConstraintField.CACHE_ON_MATCH));
146         setAttribute(value, RuntimeTagNames.CACHE_ON_MATCH_FAILURE, (String JavaDoc) descriptor.getAttributeValue(ConstraintField.VALUE, i, ConstraintField.CACHE_ON_MATCH_FAILURE));
147         
148     }
149     // name, scope, cache-on-match, cache-on-match-failure attributes
150
setAttribute(constraintField, RuntimeTagNames.NAME, (String JavaDoc) descriptor.getAttributeValue(ConstraintField.NAME));
151     setAttribute(constraintField, RuntimeTagNames.SCOPE, (String JavaDoc) descriptor.getAttributeValue(ConstraintField.SCOPE));
152     setAttribute(constraintField, RuntimeTagNames.CACHE_ON_MATCH, (String JavaDoc) descriptor.getAttributeValue(ConstraintField.CACHE_ON_MATCH));
153     setAttribute(constraintField, RuntimeTagNames.CACHE_ON_MATCH_FAILURE, (String JavaDoc) descriptor.getAttributeValue(ConstraintField.CACHE_ON_MATCH_FAILURE));
154     
155     return constraintField;
156     }
157 }
158
Popular Tags