KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.StringTokenizer JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
32 import com.sun.enterprise.deployment.xml.TagNames;
33 import com.sun.enterprise.deployment.node.connector.ConnectorNode;
34 import com.sun.enterprise.deployment.Descriptor;
35 import com.sun.enterprise.deployment.BundleDescriptor;
36 import com.sun.enterprise.deployment.ConnectorDescriptor;
37 import com.sun.enterprise.deployment.RootDeploymentDescriptor;
38 import com.sun.enterprise.deployment.MessageDestinationDescriptor;
39
40 /**
41  * This class defines all the common behaviour among nodes responsibles for
42  * handling bundles
43  *
44  * @author Jerome Dochez
45  */

46 public abstract class BundleNode extends DisplayableComponentNode implements RootXMLNode {
47     
48     public final static String JavaDoc W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
49     public final static String JavaDoc W3C_XML_SCHEMA_INSTANCE = "http://www.w3.org/2001/XMLSchema-instance";
50         
51     protected final static String JavaDoc SCHEMA_LOCATION_TAG = "xsi:schemaLocation";
52     
53     protected String JavaDoc docType;
54     private Map JavaDoc namespaceContext=null;
55     
56     /**
57      * set the DOCTYPE as read in the input XML File
58      * @param DOCTYPE
59      */

60     public void setDocType(String JavaDoc docType) {
61         this.docType = docType;
62         setSpecVersion();
63     }
64     
65     public static Element JavaDoc appendChildNS(Node JavaDoc parent, String JavaDoc elementName,
66         String JavaDoc nameSpace) {
67         Element JavaDoc child = getOwnerDocument(parent).createElementNS(nameSpace, elementName);
68         parent.appendChild(child);
69         return child;
70     }
71
72     /**
73      * all sub-implementation of this class can use a dispatch table to map xml element to
74      * method name on the descriptor class for setting the element value.
75      *
76      * @return the map with the element name as a key, the setter method as a value
77      */

78     protected Map JavaDoc getDispatchTable() {
79         Map JavaDoc dispatchTable = super.getDispatchTable();
80         dispatchTable.put(TagNames.NAME, "setDisplayName");
81         dispatchTable.put(TagNames.VERSION, "setSpecVersion");
82         return dispatchTable;
83     }
84     
85     /**
86      * receives notiification of the value for a particular tag
87      *
88      * @param element the xml element
89      * @param value it's associated value
90      */

91     public void setElementValue(XMLElement element, String JavaDoc value) {
92         if (SCHEMA_LOCATION_TAG.equals(element.getCompleteName())) {
93             // we need to keep all the non j2ee/javaee schemaLocation tags
94
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(value);
95             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
96             while (st.hasMoreElements()) {
97                 String JavaDoc namespace = (String JavaDoc) st.nextElement();
98         String JavaDoc schema;
99         if (st.hasMoreElements()) {
100             schema = (String JavaDoc) st.nextElement();
101         } else {
102             schema = namespace;
103             namespace = TagNames.JAVAEE_NAMESPACE;
104         }
105                 if (namespace.equals(TagNames.J2EE_NAMESPACE))
106                     continue;
107                 if (namespace.equals(TagNames.JAVAEE_NAMESPACE))
108                     continue;
109                 if (namespace.equals(W3C_XML_SCHEMA))
110                     continue;
111                 sb.append(namespace);
112                 sb.append(" ");
113                 sb.append(schema);
114             }
115             String JavaDoc clientSchemaLocation = sb.toString();
116             if (clientSchemaLocation!=null && clientSchemaLocation.length()!=0) {
117                 Object JavaDoc o = getDescriptor();
118                 if (o instanceof RootDeploymentDescriptor) {
119                     ((RootDeploymentDescriptor) o).setSchemaLocation(clientSchemaLocation);
120                 }
121             }
122         } else if (element.getQName().equals(TagNames.METADATA_COMPLETE)) {
123             Object JavaDoc o = getDescriptor();
124             if (o instanceof BundleDescriptor) {
125                 ((BundleDescriptor) o).setFullAttribute(value);
126             }
127         } else {
128             super.setElementValue(element, value);
129         }
130     }
131     
132     /**
133      * write the descriptor class to a DOM tree and return it
134      *
135      * @param parent node for the DOM tree
136      * @param the descriptor to write
137      * @return the DOM tree top node
138      */

139     public Node JavaDoc writeDescriptor(Node JavaDoc parent, RootDeploymentDescriptor descriptor) {
140         Node JavaDoc bundleNode;
141         if (getDocType()==null) {
142             // we are using schemas for this DDs
143

144             // connector schema still use j2ee name space
145
if (descriptor instanceof ConnectorDescriptor) {
146                 bundleNode = appendChildNS(parent, getXMLRootTag().getQName(),
147                     TagNames.J2EE_NAMESPACE);
148             } else {
149                 bundleNode = appendChildNS(parent, getXMLRootTag().getQName(),
150                     TagNames.JAVAEE_NAMESPACE);
151             }
152             addBundleNodeAttributes((Element JavaDoc) bundleNode, descriptor);
153         } else {
154             bundleNode = appendChild(parent, getXMLRootTag().getQName());
155         }
156         // description, display-name, icons...
157
writeDisplayableComponentInfo(bundleNode, descriptor);
158        
159         
160         return bundleNode;
161     }
162
163     protected void writeMessageDestinations(Node JavaDoc parentNode,
164                                             Iterator JavaDoc msgDestinations) {
165         if ( (msgDestinations == null) || !msgDestinations.hasNext() )
166             return;
167         
168         MessageDestinationNode subNode = new MessageDestinationNode();
169         for (;msgDestinations.hasNext();) {
170             MessageDestinationDescriptor next =
171                 (MessageDestinationDescriptor) msgDestinations.next();
172             subNode.writeDescriptor(parentNode,
173                                     TagNames.MESSAGE_DESTINATION, next);
174         }
175     }
176                                             
177     /**
178      * write the necessary attributes for the root node of this DDs document
179      */

180     protected void addBundleNodeAttributes(Element JavaDoc bundleNode, RootDeploymentDescriptor descriptor) {
181         String JavaDoc schemaLocation;
182         // the latest connector schema still use j2ee namespace
183
if (bundleNode instanceof ConnectorNode) {
184             bundleNode.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", TagNames.J2EE_NAMESPACE);
185             schemaLocation = TagNames.J2EE_NAMESPACE + " " +
186                 getSchemaURL();
187         } else {
188             bundleNode.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", TagNames.JAVAEE_NAMESPACE);
189             schemaLocation = TagNames.JAVAEE_NAMESPACE + " " +
190                 getSchemaURL();
191         }
192         bundleNode.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", W3C_XML_SCHEMA_INSTANCE);
193
194         // add all custom global namespaces
195
addNamespaceDeclaration(bundleNode, descriptor);
196         String JavaDoc clientSchemaLocation = descriptor.getSchemaLocation();
197         if (clientSchemaLocation!=null) {
198             schemaLocation = schemaLocation + " " + clientSchemaLocation;
199         }
200         bundleNode.setAttributeNS(W3C_XML_SCHEMA_INSTANCE, SCHEMA_LOCATION_TAG, schemaLocation);
201         bundleNode.setAttribute(TagNames.VERSION, getSpecVersion());
202
203         // Write out full attribute for DD which allows annotations.
204
// The full attribute should always be written out as true since
205
// when we come here to write it out, the annotation information
206
// has already been processed and saved in DD, so written out DD
207
// is always a full DD.
208
if (descriptor instanceof BundleDescriptor) {
209             BundleDescriptor bundleDesc = (BundleDescriptor)descriptor;
210             if (! bundleDesc.isDDWithNoAnnotationAllowed()) {
211                 bundleNode.setAttribute(TagNames.METADATA_COMPLETE, "true");
212             }
213         }
214     }
215     
216     /**
217      * notify of a new prefix mapping used in this document
218      */

219     public void addPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
220         // we don't care about the default ones...
221
if (uri.equals(TagNames.J2EE_NAMESPACE))
222             return;
223         if (uri.equals(TagNames.JAVAEE_NAMESPACE))
224             return;
225         if (uri.equals(W3C_XML_SCHEMA_INSTANCE))
226             return;
227         super.addPrefixMapping(prefix, uri);
228     }
229     
230     /**
231      * @return the complete URL for JAVAEE schemas
232      */

233     protected String JavaDoc getSchemaURL() {
234        // by default, it comes from our web site
235
return TagNames.JAVAEE_NAMESPACE + "/" + getSystemID();
236     }
237        
238     /**
239      * Sets the specVersion for this descriptor depending on the docType
240      */

241     protected void setSpecVersion() {
242         if (docType==null)
243             return;
244         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(docType, "//");
245         while (st.hasMoreElements()) {
246             String JavaDoc tmp = st.nextToken();
247             if (tmp.startsWith("DTD")) {
248                 // this is the string we are interested in
249
StringTokenizer JavaDoc versionST = new StringTokenizer JavaDoc(tmp);
250                 while (versionST.hasMoreElements()) {
251                     String JavaDoc versionStr = versionST.nextToken();
252                     try {
253                         Float.valueOf(versionStr);
254                         RootDeploymentDescriptor rdd = (RootDeploymentDescriptor) getDescriptor();
255                         rdd.setSpecVersion(versionStr);
256                         return;
257                     } catch(NumberFormatException JavaDoc nfe) {
258                         // ignore, this is just the other info of the publicID
259
}
260                 }
261             }
262         }
263     }
264 }
265
Popular Tags