All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.enterprise.deployment.node.DeploymentDescriptorNode Maven / Gradle / Ivy

There is a newer version: 10.0-b28
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.sun.enterprise.deployment.node;

import com.sun.enterprise.deployment.*;
import com.sun.enterprise.deployment.node.ejb.AroundInvokeNode;
import com.sun.enterprise.deployment.node.ejb.AroundTimeoutNode;
import com.sun.enterprise.deployment.types.EjbReference;
import com.sun.enterprise.deployment.util.DOLUtils;
import com.sun.enterprise.deployment.xml.EjbTagNames;
import com.sun.enterprise.deployment.xml.TagNames;
import com.sun.enterprise.deployment.xml.WebServicesTagNames;
import com.sun.enterprise.util.LocalStringManagerImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.logging.Level;

/**
 * Superclass of all Nodes implementation
 * XMLNode implementation represents all the DOL classes responsible for 
 * handling  the XML deployment descriptors. These nodes are called by the 
 * SAX parser when reading and are constructed to build the DOM tree for 
 * saving the XML files.
 *
 * XMLNode are orgnalized like a tree with one root XMLNode (which 
 * implement the RootXMLNode interface) and sub XMLNodes responsible
 * for handling subparts of the XML documents. Sub XMLNodes register 
 * themselfves to their parent XMLNode as handlers for a particular XML 
 * subtag of the tag handled by the parent XMLNode
 *
 * Each XMLNode is therefore associated with a xml tag (located anywhere 
 * in the tree of tags as defined by the DTD). It owns the responsibility for 
 * reading and writing the tag, its attributes and all subtags (by using 
 * delegation to sub XMLNode if necessary).
 *
 * @author  Jerome Dochez
 * @version 
 */
public abstract class DeploymentDescriptorNode implements XMLNode  {

    private static final String QNAME_SEPARATOR = ":";

    // handlers is the list of XMLNodes  registered for handling sub xml tags of the current
    // XMLNode
    protected Hashtable handlers = null;
    
    // list of add methods declared on the descriptor class to add sub descriptors extracted 
    // by the handlers registered above. The key for the table is the xml root tag for the 
    // descriptor to be added, the value is the method name to add such descriptor to the 
    // current descriptor.
    private Hashtable addMethods = null;

    // Each node is associated with a XML tag it is handling
    private XMLElement xmlTag;
    
    // Parent node in the XML Nodes implementation tree we create to map to the XML 
    // tags of the XML document
    protected XMLNode parentNode = null;
        
    // default descriptor associated with this node, some sub nodes which
    // relies on the dispatch table don't really need to know the actual
    // type of the descriptor they deal with since they are populated through
    // reflection method calls
    protected Object abstractDescriptor;
    

    // for i18N
    protected static final LocalStringManagerImpl localStrings=
	    new LocalStringManagerImpl(DeploymentDescriptorNode.class);        
    
    /** Creates new DeploymentDescriptorNode */
    public DeploymentDescriptorNode() {
        registerElementHandler(new XMLElement(TagNames.DESCRIPTION), LocalizedInfoNode.class);        
    }    
        
   /**
    * @return the descriptor instance to associate with this XMLNode
    */
   @SuppressWarnings("unchecked")
    public T getDescriptor() {
        
        if (abstractDescriptor==null) {
	    abstractDescriptor = DescriptorFactory.getDescriptor(getXMLPath());
        }
        return (T) abstractDescriptor;
    }    
    
    /**
     * Adds  a new DOL descriptor instance to the descriptor instance associated with 
     * this XMLNode
     *
     * @param descriptor the new descriptor
     */
    public void addDescriptor(Object descriptor) {
        if (getParentNode()==null) {
            DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure",
                new Object[] {descriptor , toString()});
            throw new RuntimeException("Cannot add " + descriptor + " to " + toString());
        } else {
            getParentNode().addDescriptor(descriptor);
        }
    }
    
    /**
     * Adds a new DOL descriptor instance to the descriptor associated with this 
     * XMLNode
     * 
     * @param node the sub-node adding the descriptor;
     */
    protected void addNodeDescriptor(DeploymentDescriptorNode node) {
        
        // if there is no descriptor associated with this class, the addDescriptor should implement 
        // the fate of this new descriptor.
        if (getDescriptor()==null) {
            addDescriptor(node.getDescriptor());
            return;
        }
        String xmlRootTag = node.getXMLRootTag().getQName();
        if (addMethods!=null && addMethods.containsKey(xmlRootTag)) {
            try {
                Method toInvoke = getDescriptor().getClass().getMethod(
                                                                (String) addMethods.get(xmlRootTag), 
                                                                new Class[] { node.getDescriptor().getClass() });
                toInvoke.invoke(getDescriptor(), new Object[] {node.getDescriptor()});
            } catch (InvocationTargetException e) {
		    Throwable t = e.getTargetException();
                    if (t instanceof IllegalArgumentException) {
                        // We report the error but we continue loading, this will allow the verifier to catch these errors or to register
                        // an error handler for notification
                        DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure",
                            new Object[]{node.getDescriptor().getClass() , getDescriptor().getClass()});
                    } else {
                        DOLUtils.getDefaultLogger().log(Level.WARNING, "Error occurred", t);
                        DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure",
                                new Object[]{t.toString(), null});
                    }
            } catch(Throwable t) {
                DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure",
                    new Object[] {node.getDescriptor().getClass() , getDescriptor().getClass() });
                DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure",
                        new Object[]{t.toString(), null});
                DOLUtils.getDefaultLogger().log(Level.WARNING, "Error occurred", t);
            }
        } else {
            addDescriptor(node.getDescriptor());
        }
    }

    /** 
     * set the parent node for the current instance. 
     */
    public void setParentNode(XMLNode parentNode) {
        this.parentNode = parentNode;
    }
    
    /** 
     * @return the parent node of the current instance
     */
    public XMLNode getParentNode() {
        return parentNode;
    }
    
    /**
     * register a new XMLNode handler for a particular XML tag.
     * 
     * @param element XMLElement is the XML tag this XMLNode will handle
     * @param handler the class implemenenting the XMLNode interface
     */
    protected void registerElementHandler(XMLElement element, Class  handler) {
        if (handlers==null) {
            handlers = new Hashtable();
        }
        handlers.put(element.getQName(), handler);        
    }
    
    /**
     * register a new XMLNode handler for a particular XML tag.
     * 
     * @param element XMLElement is the XML tag this XMLNode will handle
     * @param handler the class implemenenting the XMLNode interface
     * @param addMethodName is the method name for adding the descriptor
     * extracted by the handler node to the current descriptor
     */
    protected void registerElementHandler(XMLElement element, Class  handler, 
                                            String addMethodName) {
                                                                                             
       registerElementHandler(element, handler);       
       if (addMethods==null) {
           addMethods = new Hashtable();
       }
       addMethods.put(element.getQName(), addMethodName);       
    }    

    /**
     * @return the XML tag associated with this XMLNode
     */
    protected XMLElement getXMLRootTag() {
        return xmlTag;
    }
        
    /** 
     * sets the XML tag associated with this XMLNode
     */ 
    protected void setXMLRootTag(XMLElement element) {
        xmlTag = element;
    }
    
    /**
     * @return the handler registered for the subtag element of the curent  XMLNode
     */
    public  XMLNode getHandlerFor(XMLElement element) {
        if (handlers==null) {
            DOLUtils.getDefaultLogger().log(Level.WARNING, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
                    new Object[] {this , "No handler registered"});            
            return null;
        } else {
            Class c = (Class)  handlers.get(element.getQName());
            if (c==null) {
                DOLUtils.getDefaultLogger().log(Level.WARNING, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
                    new Object[] {element.getQName(), "No handler registered"});
                return null;
            } 
            if (DOLUtils.getDefaultLogger().isLoggable(Level.FINER)) {        
                DOLUtils.getDefaultLogger().finer("New Handler requested for " + c);
            }
            DeploymentDescriptorNode node;
            try {
                node = (DeploymentDescriptorNode) c.newInstance(); 
                node.setParentNode(this);
		node.setXMLRootTag(element);
                node.getDescriptor();
            } catch(Exception e) {
                DOLUtils.getDefaultLogger().log(Level.WARNING, "Error occurred", e); 
                return null;
            }
	    return node;
        }
    }
    
    /**
     * SAX Parser API implementation, we don't really care for now.
     */
    public void startElement(XMLElement element, Attributes attributes) {
        //DOLUtils.getDefaultLogger().finer("STARTELEMENT : " + "in " + getXMLRootTag() + "  Node, startElement " + element.getQName());
        if (!this.getXMLRootTag().equals(element))
            return;
        
        if (attributes.getLength()>0) {
            for (int i=0;i
     * @return the Document for the given node
     * 

*/ static protected Document getOwnerDocument(Node node) { if (node instanceof Document) { return (Document) node; } return node.getOwnerDocument(); } /** *

* Append a new element child to the current node *

* @param parent is the parent node for the new child element * @param elementName is new element tag name * @return the newly created child node */ public static Element appendChild(Node parent, String elementName) { Element child = getOwnerDocument(parent).createElement(elementName); parent.appendChild(child); return child; } /** *

* Append a new text child *

* @param parent for the new child element * @param elementName is the new element tag name * @param text the text for the new element * @result the newly create child node */ public static Node appendTextChild(Node parent, String elementName, String text) { if (text == null || text.length()==0) return null; Node child = appendChild(parent, elementName); child.appendChild(getOwnerDocument(child).createTextNode(text)); return child; } /** *

* Append a new text child *

* @param parent for the new child element * @param elementName is the new element tag name * @param value the int value for the new element * @result the newly create child node */ public static Node appendTextChild(Node parent, String elementName, int value) { return appendTextChild(parent, elementName, String.valueOf(value)); } /** *

* Append a new text child even if text is empty *

* @param parent for the new child element * @param elementName is the new element tag name * @param text the text for the new element * @result the newly create child node */ public static Node forceAppendTextChild(Node parent, String elementName, String text) { Node child = appendChild(parent, elementName); if (text != null && text.length()!=0) { child.appendChild(getOwnerDocument(child).createTextNode(text)); } return child; } /** *

* Append a new attribute to an element *

* @param parent for the new child element * @param elementName is the new element tag name * @param text the text for the new element * @result the newly create child node */ public static void setAttribute(Element parent, String elementName, String text) { if (text == null || text.length()==0) return; parent.setAttribute(elementName, text); } /** * Set a namespace attribute on an element. * @param element on which to set attribute * @param prefix raw prefix (without "xmlns:") * @param namespaceURI namespace URI to which prefix is mapped. */ public static void setAttributeNS(Element element, String prefix, String namespaceURI) { String nsPrefix = prefix.equals("") ? "xmlns" : "xmlns" + QNAME_SEPARATOR + prefix; element.setAttributeNS("http://www.w3.org/2000/xmlns/", nsPrefix, namespaceURI); } /** * write a list of env entry descriptors to a DOM Tree * * @param parentNode parent node for the DOM tree * @param envEntries the iterator over the descriptors to write */ protected void writeEnvEntryDescriptors(Node parentNode, Iterator envEntries) { if (envEntries==null || !envEntries.hasNext()) return; EnvEntryNode subNode = new EnvEntryNode(); for (;envEntries.hasNext();) { EnvironmentProperty envProp = (EnvironmentProperty) envEntries.next(); subNode.writeDescriptor(parentNode, TagNames.ENVIRONMENT_PROPERTY, envProp); } } /** * write the ejb references (local or remote) to the DOM tree * * @param parentNode parent node for the DOM tree * @param refs the set of EjbReferenceDescriptor to write */ protected void writeEjbReferenceDescriptors(Node parentNode, Iterator refs) { if (refs==null || !refs.hasNext()) return; EjbReferenceNode subNode = new EjbReferenceNode(); // ejb-ref* Set localRefDescs = new HashSet(); for (;refs.hasNext();) { EjbReference ejbRef = (EjbReference) refs.next(); if (ejbRef.isLocal()) { localRefDescs.add(ejbRef); } else { subNode.writeDescriptor(parentNode, TagNames.EJB_REFERENCE, ejbRef); } } // ejb-local-ref* for (Iterator e=localRefDescs.iterator(); e.hasNext();) { EjbReference ejbRef = (EjbReference) e.next(); subNode.writeDescriptor(parentNode, TagNames.EJB_LOCAL_REFERENCE,ejbRef); } } protected void writeServiceReferenceDescriptors(Node parentNode, Iterator refs) { if( ( refs == null ) || !refs.hasNext() ) { return; } ServiceReferenceNode serviceRefNode = new ServiceReferenceNode(); while(refs.hasNext()) { ServiceReferenceDescriptor next = (ServiceReferenceDescriptor) refs.next(); serviceRefNode.writeDescriptor (parentNode, WebServicesTagNames.SERVICE_REF, next); } } /** * write a list of resource reference descriptors to a DOM Tree * * @param parentNode parent node for the DOM tree * @param resRefs the iterator over the descriptors to write */ protected void writeResourceRefDescriptors(Node parentNode, Iterator resRefs) { if (resRefs==null || !resRefs.hasNext()) return; ResourceRefNode subNode = new ResourceRefNode(); for (;resRefs.hasNext();) { ResourceReferenceDescriptor aResRef = (ResourceReferenceDescriptor) resRefs.next(); subNode.writeDescriptor(parentNode, TagNames.RESOURCE_REFERENCE, aResRef); } } /** * write a list of resource env reference descriptors to a DOM Tree * * @param parentNode parent node for the DOM tree * @param resRefs the iterator over the descriptors to write */ protected void writeResourceEnvRefDescriptors(Node parentNode, Iterator resRefs) { if (resRefs==null || !resRefs.hasNext()) return; ResourceEnvRefNode subNode = new ResourceEnvRefNode(); for (;resRefs.hasNext();) { JmsDestinationReferenceDescriptor aResRef = (JmsDestinationReferenceDescriptor) resRefs.next(); subNode.writeDescriptor(parentNode, TagNames.RESOURCE_ENV_REFERENCE, aResRef); } } /** * write a list of message destination reference descriptors to a DOM Tree * * @param parentNode parent node for the DOM tree * @param msgDestRefs the iterator over the descriptors to write */ protected void writeMessageDestinationRefDescriptors (Node parentNode, Iterator msgDestRefs) { if (msgDestRefs==null || !msgDestRefs.hasNext()) return; MessageDestinationRefNode subNode = new MessageDestinationRefNode(); for (;msgDestRefs.hasNext();) { MessageDestinationReferenceDescriptor next = (MessageDestinationReferenceDescriptor) msgDestRefs.next(); subNode.writeDescriptor(parentNode, TagNames.MESSAGE_DESTINATION_REFERENCE, next); } } /** * write a list of entity manager reference descriptors to a DOM Tree * * @param parentNode parent node for the DOM tree * @param entityMgrRefs the iterator over the descriptors to write */ protected void writeEntityManagerReferenceDescriptors(Node parentNode, Iterator entityMgrRefs) { if (entityMgrRefs==null || !entityMgrRefs.hasNext()) return; EntityManagerReferenceNode subNode = new EntityManagerReferenceNode(); for (;entityMgrRefs.hasNext();) { EntityManagerReferenceDescriptor aEntityMgrRef = (EntityManagerReferenceDescriptor)entityMgrRefs.next(); subNode.writeDescriptor(parentNode, TagNames.PERSISTENCE_CONTEXT_REF, aEntityMgrRef); } } /** * write a list of entity manager factory reference descriptors to * a DOM Tree * * @param parentNode parent node for the DOM tree * @param entityMgrFactoryRefs the iterator over the descriptors to write */ protected void writeEntityManagerFactoryReferenceDescriptors(Node parentNode, Iterator entityMgrFactoryRefs) { if (entityMgrFactoryRefs==null || !entityMgrFactoryRefs.hasNext()) return; EntityManagerFactoryReferenceNode subNode = new EntityManagerFactoryReferenceNode(); for (;entityMgrFactoryRefs.hasNext();) { EntityManagerFactoryReferenceDescriptor aEntityMgrFactoryRef = (EntityManagerFactoryReferenceDescriptor)entityMgrFactoryRefs.next(); subNode.writeDescriptor(parentNode, TagNames.PERSISTENCE_UNIT_REF, aEntityMgrFactoryRef); } } protected void writeAroundInvokeDescriptors (Node parentNode, Iterator aroundInvokeDescs) { if (aroundInvokeDescs == null || !aroundInvokeDescs.hasNext()) return; AroundInvokeNode subNode = new AroundInvokeNode(); for(; aroundInvokeDescs.hasNext();) { LifecycleCallbackDescriptor next = (LifecycleCallbackDescriptor) aroundInvokeDescs.next(); subNode.writeDescriptor(parentNode, EjbTagNames.AROUND_INVOKE_METHOD, next); } } protected void writeAroundTimeoutDescriptors (Node parentNode, Iterator aroundTimeoutDescs) { if (aroundTimeoutDescs == null || !aroundTimeoutDescs.hasNext()) return; AroundTimeoutNode subNode = new AroundTimeoutNode(); for(; aroundTimeoutDescs.hasNext();) { LifecycleCallbackDescriptor next = (LifecycleCallbackDescriptor) aroundTimeoutDescs.next(); subNode.writeDescriptor(parentNode, EjbTagNames.AROUND_TIMEOUT_METHOD, next); } } protected void writePostActivateDescriptors (Node parentNode, Iterator postActivateDescs) { if (postActivateDescs == null || !postActivateDescs.hasNext()) return; LifecycleCallbackNode subNode = new LifecycleCallbackNode(); for(; postActivateDescs.hasNext();) { LifecycleCallbackDescriptor next = (LifecycleCallbackDescriptor) postActivateDescs.next(); subNode.writeDescriptor(parentNode, EjbTagNames.POST_ACTIVATE_METHOD, next); } } protected void writePrePassivateDescriptors (Node parentNode, Iterator prePassivateDescs) { if (prePassivateDescs == null || !prePassivateDescs.hasNext()) return; LifecycleCallbackNode subNode = new LifecycleCallbackNode(); for(; prePassivateDescs.hasNext();) { LifecycleCallbackDescriptor next = (LifecycleCallbackDescriptor) prePassivateDescs.next(); subNode.writeDescriptor(parentNode, EjbTagNames.PRE_PASSIVATE_METHOD, next); } } /** * write a list of post-construct descriptors to a DOM Tree * * @param parentNode parent node for the DOM tree * @param postConstructDescs the iterator over the descriptors to write */ protected void writePostConstructDescriptors (Node parentNode, Iterator postConstructDescs) { if (postConstructDescs == null || !postConstructDescs.hasNext()) return; LifecycleCallbackNode subNode = new LifecycleCallbackNode(); for (; postConstructDescs.hasNext();) { LifecycleCallbackDescriptor next = postConstructDescs.next(); subNode.writeDescriptor(parentNode, TagNames.POST_CONSTRUCT, next); } } /** * write a list of pre-destroy descriptors to a DOM Tree * * @param parentNode parent node for the DOM tree * @param preDestroyDescs the iterator over the descriptors to write */ protected void writePreDestroyDescriptors (Node parentNode, Iterator preDestroyDescs) { if (preDestroyDescs == null || !preDestroyDescs.hasNext()) return; LifecycleCallbackNode subNode = new LifecycleCallbackNode(); for (; preDestroyDescs.hasNext();) { LifecycleCallbackDescriptor next = preDestroyDescs.next(); subNode.writeDescriptor(parentNode, TagNames.PRE_DESTROY, next); } } /** * write a list of datasource-definition descriptors to a DOM Tree * * @param parentNode parent node for the DOM tree * @param dsDefinitionDescIterator the iterator over the descriptors to write */ protected void writeDataSourceDefinitionDescriptors(Node parentNode, Iterator dsDefinitionDescIterator) { if(dsDefinitionDescIterator == null || !dsDefinitionDescIterator.hasNext()){ return; } DataSourceDefinitionNode subNode = new DataSourceDefinitionNode(); for(;dsDefinitionDescIterator.hasNext();){ DataSourceDefinitionDescriptor next = dsDefinitionDescIterator.next(); subNode.writeDescriptor(parentNode, TagNames.DATA_SOURCE, next); } } /** * writes iocalized descriptions (if any) to the DOM node */ protected void writeLocalizedDescriptions(Node node, Descriptor desc) { LocalizedInfoNode localizedNode = new LocalizedInfoNode(); localizedNode.writeLocalizedMap(node, TagNames.DESCRIPTION, desc.getLocalizedDescriptions()); } /** * writes jndi environment references group nodes */ protected void writeJNDIEnvironmentRefs(Node node, JndiNameEnvironment descriptor) { /* */ writeEnvEntryDescriptors(node, descriptor.getEnvironmentProperties().iterator()); /* */ /* */ writeEjbReferenceDescriptors(node, descriptor.getEjbReferenceDescriptors().iterator()); /* */ writeServiceReferenceDescriptors(node, descriptor.getServiceReferenceDescriptors().iterator()); /* */ writeResourceRefDescriptors(node, descriptor.getResourceReferenceDescriptors().iterator()); /* */ writeResourceEnvRefDescriptors(node, descriptor.getJmsDestinationReferenceDescriptors().iterator()); /* */ writeMessageDestinationRefDescriptors(node, descriptor.getMessageDestinationReferenceDescriptors().iterator()); /* */ writeEntityManagerReferenceDescriptors(node, descriptor.getEntityManagerReferenceDescriptors().iterator()); /* */ writeEntityManagerFactoryReferenceDescriptors(node, descriptor.getEntityManagerFactoryReferenceDescriptors().iterator()); /* */ writePostConstructDescriptors(node, descriptor.getPostConstructDescriptors().iterator()); /* */ writePreDestroyDescriptors(node, descriptor.getPreDestroyDescriptors().iterator()); } /** * write the deployment extension nodes associated with this node * * @param parentNode parent node for the DOM tree * @param descriptor the deployment extension descriptor * * @note this was an iterim feature of the J2EE 1.4 platform, I leave it * here for now because it may reappear in a later platform release, I put * it private so that it is not misused in the meantime. */ private void writeDeploymentExtensionDescriptor( Node parentNode, Descriptor descriptor) { Iterator itr = descriptor.getDeploymentExtensions(); if (itr==null) { return; } DeploymentExtensionNode subNode = new DeploymentExtensionNode(); subNode.writeDescriptor(parentNode, itr); } /** * Any node can now declare its own namespace. this apply to DDs only * when dealing with deployment extensions. Write any declared * namespace declaration * * @param node from which this namespace is declared * @param descriptor containing the namespace declaration if any */ protected void addNamespaceDeclaration(Element node, Descriptor descriptor) { // declare now all remaining namepace... Map prefixMapping = (descriptor != null ) ? descriptor.getPrefixMapping() : null; if (prefixMapping!=null) { for (Iterator itr =prefixMapping.keySet().iterator();itr.hasNext();) { String prefix = (String) itr.next(); String namespaceURI = (String) prefixMapping.get(prefix); setAttributeNS(node, prefix, namespaceURI); } } } /** * notify of a new prefix mapping used in this document */ public void addPrefixMapping(String prefix, String uri) { Object o = getDescriptor(); if (o instanceof Descriptor) { Descriptor descriptor = (Descriptor) o; descriptor.addPrefixMapping(prefix, uri); } } /** * Resolve a QName prefix to its corresponding Namespace URI by * searching up node chain starting with child. */ public String resolvePrefix(XMLElement element, String prefix) { // If prefix is empty string, returned namespace URI // is the default namespace. return element.getPrefixURIMapping(prefix); } /** * @return namespace URI prefix from qname, where qname is * an xsd:QName, or the empty string if there is no prefix. * * QName ::= (Prefix ':')? LocalPart */ public String getPrefixFromQName(String qname) { StringTokenizer tokenizer = new StringTokenizer(qname, QNAME_SEPARATOR); return (tokenizer.countTokens() == 2) ? tokenizer.nextToken() : ""; } /** * Return local part from qname, where qname is an xsd:QName. * * QName ::= (Prefix ':')? LocalPart */ public String getLocalPartFromQName(String qname) { StringTokenizer tokenizer = new StringTokenizer(qname, QNAME_SEPARATOR); String localPart = qname; if( tokenizer.countTokens() == 2 ) { // skip namespace prefix. tokenizer.nextToken(); localPart = tokenizer.nextToken(); } return localPart; } public String composeQNameValue(String prefix, String localPart) { return ( (prefix != null) && !(prefix.equals("")) ) ? prefix + QNAME_SEPARATOR + localPart : localPart; } public void appendQNameChild(String elementName, Node parent, String namespaceUri, String localPart, String prefix) { if( prefix == null ) { // @@@ make configurable?? prefix = elementName + "_ns__"; } String elementValue = composeQNameValue(prefix, localPart); Element element = (Element) appendTextChild (parent, elementName, elementValue); // Always set prefix mapping on leaf node. If the DOL was // populated from an existing deployment descriptor it does // not preserve the original node structure of the XML document, // so we can't reliably know what level to place mapping. // Alternatively, if we're writing out a descriptor that was created // by the deploytool, there is no prefix->namespace information in // the first place. setAttributeNS(element, prefix, namespaceUri); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy