KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > node > ExtensionElementNode


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;
25
26 import java.util.Map JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.xml.sax.Attributes JavaDoc;
31 import com.sun.enterprise.deployment.Descriptor;
32 import com.sun.enterprise.deployment.ExtensionElementDescriptor;
33 import com.sun.enterprise.deployment.DynamicAttributesDescriptor;
34 import com.sun.enterprise.deployment.xml.TagNames;
35
36 /**
37  * This node is responsible for loading and saving any type of
38  * xml node and store them in a DeploymentExtensionElement
39  *
40  * @author Jerome Dochez
41  */

42 public class ExtensionElementNode extends DeploymentDescriptorNode {
43     
44     ExtensionElementDescriptor descriptor;
45     
46     /** Creates a new instance of DynamicDescriptorNode */
47     public ExtensionElementNode() {
48         descriptor = new ExtensionElementDescriptor();
49     }
50
51    /**
52      * all sub-implementation of this class can use a dispatch table to map xml element to
53      * method name on the descriptor class for setting the element value.
54      *
55      * @return the map with the element name as a key, the setter method as a value
56      */

57     protected Map JavaDoc getDispatchTable() {
58         return null;
59     }
60     
61     /**
62      * Adds a new DOL descriptor instance to the descriptor associated with this
63      * XMLNode
64      *
65      * @param XMLNode the sub-node adding the descriptor;
66      * @param descriptor the new descriptor
67      */

68     protected void addNodeDescriptor(DeploymentDescriptorNode node) {
69         // nodes are added upon creation
70
ExtensionElementDescriptor dad = (ExtensionElementDescriptor) node.getDescriptor();
71         Iterator JavaDoc itr = dad.getElementNames();
72         // jump over first element;
73
if (itr.hasNext()) itr.next();
74         
75         if (itr.hasNext() && !dad.hasAttributes()) {
76             descriptor.addElement(node.getXMLRootTag().getCompleteName(), dad);
77         } else {
78             descriptor.addElement(node.getXMLRootTag().getCompleteName(), dad.getElement(node.getXMLRootTag().getCompleteName()));
79         }
80     }
81     
82    /**
83     * @return the descriptor instance to associate with this XMLNode
84     */

85     public Object JavaDoc getDescriptor() {
86         return descriptor;
87     }
88     
89     /**
90      * @return true if the element tag can be handled by any registered sub nodes of the
91      * current XMLNode
92      */

93     public boolean handlesElement(XMLElement element) {
94         // we are never handling xml fragment only leaf nodes, we create subnodes for that.
95
return false;
96     }
97     
98     /**
99      * @return the handler registered for the subtag element of the curent XMLNode
100      */

101     public XMLNode getHandlerFor(XMLElement element) {
102         ExtensionElementNode subNode = new ExtensionElementNode();
103         subNode.setParentNode(this);
104         subNode.setXMLRootTag(new XMLElement(element.getCompleteName()));
105         return subNode;
106     }
107     
108     /**
109      * SAX Parser API implementation, we don't really care for now.
110      */

111     public void startElement(XMLElement element, Attributes JavaDoc attributes) {
112         if (attributes.getLength()>0) {
113             for (int i=0;i<attributes.getLength();i++) {
114                 if (attributes.getLocalName(i).equals("type")) {
115                     // type declaration... replace the standard xml declaration with ours...
116
descriptor.getAttributes().addExtraAttribute("xsi:type", attributes.getValue(i));
117                 } else {
118                     descriptor.getAttributes().addExtraAttribute(attributes.getQName(i), attributes.getValue(i));
119                 }
120             }
121         }
122     }
123     
124     /**
125      * receives notification of the value for a particular tag
126      *
127      * @param element the xml element
128      * @param value it's associated value
129      */

130     public void setElementValue(XMLElement element, String JavaDoc value) {
131         descriptor.addElement(element.getCompleteName(), value);
132     }
133     
134     /**
135      * receives notification of the end of an XML element by the Parser
136      *
137      * @param element the xml tag identification
138      * @return true if this node is done processing the XML sub tree
139      */

140     public boolean endElement(XMLElement element) {
141         boolean allDone = element.getCompleteName().equals(getXMLRootTag().getCompleteName()) || element.getQName().equals(TagNames.EXTENSION_ELEMENT);
142         if (allDone) {
143             postParsing();
144             ((DeploymentDescriptorNode) getParentNode()).addNodeDescriptor(this);
145         }
146         return allDone;
147     }
148     
149     /**
150      * write the deployment extensions nodes associated with this node
151      *
152      * @param parent node for the DOM tree
153      * @param deployment extension element
154      */

155     protected void writeDescriptor(Node JavaDoc parentNode, String JavaDoc tagName, ExtensionElementDescriptor descriptor) {
156         Element JavaDoc node = appendChildNS(parentNode, tagName, descriptor);
157         if (descriptor.hasAttributes()) {
158             Map JavaDoc attributes = descriptor.getAttributes().getExtraAttributes();
159             for (Iterator JavaDoc itr = attributes.keySet().iterator();itr.hasNext();) {
160                 String JavaDoc key = (String JavaDoc) itr.next();
161                 String JavaDoc value = (String JavaDoc) attributes.get(key);
162                 String JavaDoc namespace = "";
163                 if (key.indexOf(':')!=-1) {
164                     String JavaDoc prefix = key.substring(0, key.indexOf(':'));
165                     namespace = getNamespaceFor(descriptor, parentNode, prefix);
166                 }
167                 node.setAttributeNS(namespace, key, value);
168             }
169         }
170         addNamespaceDeclaration(node, descriptor);
171         for (Iterator JavaDoc itr = descriptor.getElementNames();itr.hasNext();) {
172             String JavaDoc elementName = (String JavaDoc) itr.next();
173             Object JavaDoc value = descriptor.getElement(elementName);
174             if (value instanceof ExtensionElementDescriptor) {
175                 writeDescriptor(node, elementName, (ExtensionElementDescriptor) value);
176             } else
177             if (value instanceof String JavaDoc) {
178                 appendTextChildNS(node, elementName, (String JavaDoc) value, descriptor);
179             }
180         }
181     }
182     
183     /**
184      * <p>
185      * Append a new element child to the current node
186      * </p>
187      * @param parentNode is the parent node for the new child element
188      * @param elementName is new element tag name
189      * @return the newly created child node
190      */

191     public Element JavaDoc appendChildNS(Node JavaDoc parent, String JavaDoc elementName, Descriptor descriptor) {
192         if (elementName.indexOf(':')!=-1) {
193             String JavaDoc prefix = elementName.substring(0, elementName.indexOf(':'));
194             elementName = elementName.substring(elementName.indexOf(':')+1);
195             
196             String JavaDoc namespace = getNamespaceFor(descriptor, parent, prefix);
197             
198             Element JavaDoc child = getOwnerDocument(parent).createElementNS(namespace, elementName);
199             child.setPrefix(prefix);
200             parent.appendChild(child);
201             return child;
202         }
203         return super.appendChild(parent, elementName);
204     }
205
206     /**
207      * look in the mapping defined in this descriptor and in all parent nodes
208      * for the right namespace for the passed prefix
209      */

210     private String JavaDoc getNamespaceFor(Descriptor descriptor, Node JavaDoc parent, String JavaDoc prefix) {
211         
212         Map JavaDoc prefixMapping = descriptor.getPrefixMapping();
213         String JavaDoc namespace = null;
214         if (prefixMapping!=null) {
215             namespace = (String JavaDoc) prefixMapping.get(prefix);
216         }
217         if (namespace==null) {
218             Element JavaDoc currentNode = (Element JavaDoc) parent;
219             namespace="";
220             while (currentNode!=null && namespace.length()==0) {
221                 namespace = currentNode.getAttributeNS("http://www.w3.org/2000/xmlns/", prefix);
222                 if (namespace.length()==0)
223                     currentNode = (Element JavaDoc) currentNode.getParentNode();
224             }
225         }
226         return namespace;
227     }
228
229     
230     /**
231      * <p>
232      * Append a new text child
233      * </p>
234      * @param parent for the new child element
235      * @param elementName is the new element tag name
236      * @param text the text for the new element
237      * @result the newly create child node
238      */

239     public Node JavaDoc appendTextChildNS(Node JavaDoc parent, String JavaDoc elementName, String JavaDoc text, Descriptor descriptor) {
240         
241         if (text == null || text.length()==0)
242             return null;
243         
244         Node JavaDoc child = appendChildNS(parent, elementName, descriptor);
245         child.appendChild(getOwnerDocument(child).createTextNode(text));
246         return child;
247     }
248     
249 }
250
Popular Tags