Example usage for org.w3c.dom Element getLocalName

List of usage examples for org.w3c.dom Element getLocalName

Introduction

In this page you can find the example usage for org.w3c.dom Element getLocalName.

Prototype

public String getLocalName();

Source Link

Document

Returns the local part of the qualified name of this node.

Usage

From source file:org.apache.rampart.util.RampartUtil.java

public static Element insertSiblingAfter(RampartMessageData rmd, Element child, Element sibling) {
    if (child == null) {
        return appendChildToSecHeader(rmd, sibling);
    } else {//from  www  .j  av a 2 s.c o m
        if (child.getOwnerDocument().equals(sibling.getOwnerDocument())) {

            if (child.getParentNode() == null && !child.getLocalName().equals("UsernameToken")) {
                rmd.getSecHeader().getSecurityHeader().appendChild(child);
            }
            ((OMElement) child).insertSiblingAfter((OMElement) sibling);
            return sibling;
        } else {
            Element newSib = (Element) child.getOwnerDocument().importNode(sibling, true);
            ((OMElement) child).insertSiblingAfter((OMElement) newSib);
            return newSib;
        }
    }
}

From source file:org.apache.servicemix.jbi.deployer.descriptor.DescriptorFactory.java

/**
 * Build a jbi descriptor from the specified binary data.
 * The descriptor is validated against the schema, but no
 * semantic validation is performed./* ww w.  ja v a 2s.com*/
 *
 * @param bytes hold the content of the JBI descriptor xml document
 * @return the Descriptor object
 */
public static Descriptor buildDescriptor(final byte[] bytes) {
    try {
        // Validate descriptor
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XSD_SCHEMA_LANGUAGE);
        Schema schema = schemaFactory.newSchema(DescriptorFactory.class.getResource(JBI_DESCRIPTOR_XSD));
        Validator validator = schema.newValidator();
        validator.setErrorHandler(new ErrorHandler() {
            public void warning(SAXParseException exception) throws SAXException {
                //log.debug("Validation warning on " + url + ": " + exception);
            }

            public void error(SAXParseException exception) throws SAXException {
                //log.info("Validation error on " + url + ": " + exception);
            }

            public void fatalError(SAXParseException exception) throws SAXException {
                throw exception;
            }
        });
        validator.validate(new StreamSource(new ByteArrayInputStream(bytes)));
        // Parse descriptor
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document doc = docBuilder.parse(new ByteArrayInputStream(bytes));
        Element jbi = doc.getDocumentElement();
        Descriptor desc = new Descriptor();
        desc.setVersion(Double.parseDouble(getAttribute(jbi, VERSION)));
        Element child = getFirstChildElement(jbi);
        if (COMPONENT.equals(child.getLocalName())) {
            ComponentDesc component = new ComponentDesc();
            component.setType(child.getAttribute(TYPE));
            component.setComponentClassLoaderDelegation(getAttribute(child, COMPONENT_CLASS_LOADER_DELEGATION));
            component.setBootstrapClassLoaderDelegation(getAttribute(child, BOOTSTRAP_CLASS_LOADER_DELEGATION));
            List<SharedLibraryList> sls = new ArrayList<SharedLibraryList>();
            DocumentFragment ext = null;
            for (Element e = getFirstChildElement(child); e != null; e = getNextSiblingElement(e)) {
                if (IDENTIFICATION.equals(e.getLocalName())) {
                    component.setIdentification(readIdentification(e));
                } else if (COMPONENT_CLASS_NAME.equals(e.getLocalName())) {
                    component.setComponentClassName(getText(e));
                    component.setDescription(getAttribute(e, DESCRIPTION));
                } else if (COMPONENT_CLASS_PATH.equals(e.getLocalName())) {
                    ClassPath componentClassPath = new ClassPath();
                    ArrayList<String> l = new ArrayList<String>();
                    for (Element e2 = getFirstChildElement(e); e2 != null; e2 = getNextSiblingElement(e2)) {
                        if (PATH_ELEMENT.equals(e2.getLocalName())) {
                            l.add(getText(e2));
                        }
                    }
                    componentClassPath.setPathList(l);
                    component.setComponentClassPath(componentClassPath);
                } else if (BOOTSTRAP_CLASS_NAME.equals(e.getLocalName())) {
                    component.setBootstrapClassName(getText(e));
                } else if (BOOTSTRAP_CLASS_PATH.equals(e.getLocalName())) {
                    ClassPath bootstrapClassPath = new ClassPath();
                    ArrayList<String> l = new ArrayList<String>();
                    for (Element e2 = getFirstChildElement(e); e2 != null; e2 = getNextSiblingElement(e2)) {
                        if (PATH_ELEMENT.equals(e2.getLocalName())) {
                            l.add(getText(e2));
                        }
                    }
                    bootstrapClassPath.setPathList(l);
                    component.setBootstrapClassPath(bootstrapClassPath);
                } else if (SHARED_LIBRARY.equals(e.getLocalName())) {
                    SharedLibraryList sl = new SharedLibraryList();
                    sl.setName(getText(e));
                    sl.setVersion(getAttribute(e, VERSION));
                    sls.add(sl);
                } else {
                    if (ext == null) {
                        ext = doc.createDocumentFragment();
                    }
                    ext.appendChild(e);
                }
            }
            component.setSharedLibraries(sls.toArray(new SharedLibraryList[sls.size()]));
            if (ext != null) {
                InstallationDescriptorExtension descriptorExtension = new InstallationDescriptorExtension();
                descriptorExtension.setDescriptorExtension(ext);
                component.setDescriptorExtension(descriptorExtension);
            }
            desc.setComponent(component);
        } else if (SHARED_LIBRARY.equals(child.getLocalName())) {
            SharedLibraryDesc sharedLibrary = new SharedLibraryDesc();
            sharedLibrary.setClassLoaderDelegation(getAttribute(child, CLASS_LOADER_DELEGATION));
            sharedLibrary.setVersion(getAttribute(child, VERSION));
            for (Element e = getFirstChildElement(child); e != null; e = getNextSiblingElement(e)) {
                if (IDENTIFICATION.equals(e.getLocalName())) {
                    sharedLibrary.setIdentification(readIdentification(e));
                } else if (SHARED_LIBRARY_CLASS_PATH.equals(e.getLocalName())) {
                    ClassPath sharedLibraryClassPath = new ClassPath();
                    ArrayList<String> l = new ArrayList<String>();
                    for (Element e2 = getFirstChildElement(e); e2 != null; e2 = getNextSiblingElement(e2)) {
                        if (PATH_ELEMENT.equals(e2.getLocalName())) {
                            l.add(getText(e2));
                        }
                    }
                    sharedLibraryClassPath.setPathList(l);
                    sharedLibrary.setSharedLibraryClassPath(sharedLibraryClassPath);
                }
            }
            desc.setSharedLibrary(sharedLibrary);
        } else if (SERVICE_ASSEMBLY.equals(child.getLocalName())) {
            ServiceAssemblyDesc serviceAssembly = new ServiceAssemblyDesc();
            ArrayList<ServiceUnitDesc> sus = new ArrayList<ServiceUnitDesc>();
            for (Element e = getFirstChildElement(child); e != null; e = getNextSiblingElement(e)) {
                if (IDENTIFICATION.equals(e.getLocalName())) {
                    serviceAssembly.setIdentification(readIdentification(e));
                } else if (SERVICE_UNIT.equals(e.getLocalName())) {
                    ServiceUnitDesc su = new ServiceUnitDesc();
                    for (Element e2 = getFirstChildElement(e); e2 != null; e2 = getNextSiblingElement(e2)) {
                        if (IDENTIFICATION.equals(e2.getLocalName())) {
                            su.setIdentification(readIdentification(e2));
                        } else if (TARGET.equals(e2.getLocalName())) {
                            Target target = new Target();
                            for (Element e3 = getFirstChildElement(e2); e3 != null; e3 = getNextSiblingElement(
                                    e3)) {
                                if (ARTIFACTS_ZIP.equals(e3.getLocalName())) {
                                    target.setArtifactsZip(getText(e3));
                                } else if (COMPONENT_NAME.equals(e3.getLocalName())) {
                                    target.setComponentName(getText(e3));
                                }
                            }
                            su.setTarget(target);
                        }
                    }
                    sus.add(su);
                } else if (CONNECTIONS.equals(e.getLocalName())) {
                    Connections connections = new Connections();
                    ArrayList<Connection> cns = new ArrayList<Connection>();
                    for (Element e2 = getFirstChildElement(e); e2 != null; e2 = getNextSiblingElement(e2)) {
                        if (CONNECTION.equals(e2.getLocalName())) {
                            Connection cn = new Connection();
                            for (Element e3 = getFirstChildElement(e2); e3 != null; e3 = getNextSiblingElement(
                                    e3)) {
                                if (CONSUMER.equals(e3.getLocalName())) {
                                    Consumer consumer = new Consumer();
                                    consumer.setInterfaceName(readAttributeQName(e3, INTERFACE_NAME));
                                    consumer.setServiceName(readAttributeQName(e3, SERVICE_NAME));
                                    consumer.setEndpointName(getAttribute(e3, ENDPOINT_NAME));
                                    cn.setConsumer(consumer);
                                } else if (PROVIDER.equals(e3.getLocalName())) {
                                    Provider provider = new Provider();
                                    provider.setServiceName(readAttributeQName(e3, SERVICE_NAME));
                                    provider.setEndpointName(getAttribute(e3, ENDPOINT_NAME));
                                    cn.setProvider(provider);
                                }
                            }
                            cns.add(cn);
                        }
                    }
                    connections.setConnections(cns.toArray(new Connection[cns.size()]));
                    serviceAssembly.setConnections(connections);
                }
            }
            serviceAssembly.setServiceUnits(sus.toArray(new ServiceUnitDesc[sus.size()]));
            desc.setServiceAssembly(serviceAssembly);
        } else if (SERVICES.equals(child.getLocalName())) {
            Services services = new Services();
            services.setBindingComponent(
                    Boolean.valueOf(getAttribute(child, BINDING_COMPONENT)).booleanValue());
            ArrayList<Provides> provides = new ArrayList<Provides>();
            ArrayList<Consumes> consumes = new ArrayList<Consumes>();
            for (Element e = getFirstChildElement(child); e != null; e = getNextSiblingElement(e)) {
                if (PROVIDES.equals(e.getLocalName())) {
                    Provides p = new Provides();
                    p.setInterfaceName(readAttributeQName(e, INTERFACE_NAME));
                    p.setServiceName(readAttributeQName(e, SERVICE_NAME));
                    p.setEndpointName(getAttribute(e, ENDPOINT_NAME));
                    provides.add(p);
                } else if (CONSUMES.equals(e.getLocalName())) {
                    Consumes c = new Consumes();
                    c.setInterfaceName(readAttributeQName(e, INTERFACE_NAME));
                    c.setServiceName(readAttributeQName(e, SERVICE_NAME));
                    c.setEndpointName(getAttribute(e, ENDPOINT_NAME));
                    c.setLinkType(getAttribute(e, LINK_TYPE));
                    consumes.add(c);
                }
            }
            services.setProvides(provides.toArray(new Provides[provides.size()]));
            services.setConsumes(consumes.toArray(new Consumes[consumes.size()]));
            desc.setServices(services);
        }
        checkDescriptor(desc);
        return desc;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.servicemix.jbi.runtime.impl.AbstractComponentContext.java

/**
 * <p>/* w ww .  j a  v a  2 s  . co m*/
 * Resolve an internal JBI EPR conforming to the format defined in the JBI specification.
 * </p>
 *
 * <p>The EPR would look like:
 * <pre>
 * <jbi:end-point-reference xmlns:jbi="http://java.sun.com/xml/ns/jbi/end-point-reference"
 *      jbi:end-point-name="endpointName"
 *      jbi:service-name="foo:serviceName"
 *      xmlns:foo="urn:FooNamespace"/>
 * </pre>
 * </p>
 *
 * @author Maciej Szefler m s z e f l e r @ g m a i l . c o m
 * @param epr EPR fragment
 * @return internal service endpoint corresponding to the EPR, or <code>null</code>
 *         if the EPR is not an internal EPR or if the EPR cannot be resolved
 */
public ServiceEndpoint resolveInternalEPR(DocumentFragment epr) {
    if (epr == null) {
        throw new NullPointerException("resolveInternalEPR(epr) called with null epr.");
    }
    NodeList nl = epr.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i) {
        Node n = nl.item(i);
        if (n.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        Element el = (Element) n;
        // Namespace should be "http://java.sun.com/jbi/end-point-reference"
        if (el.getNamespaceURI() == null || !el.getNamespaceURI().equals(ServiceEndpointImpl.JBI_NAMESPACE)) {
            continue;
        }
        if (el.getLocalName() == null
                || !el.getLocalName().equals(ServiceEndpointImpl.JBI_ENDPOINT_REFERENCE)) {
            continue;
        }
        String serviceName = el.getAttributeNS(el.getNamespaceURI(), ServiceEndpointImpl.JBI_SERVICE_NAME);
        // Now the DOM pain-in-the-you-know-what: we need to come up with QName for this;
        // fortunately, there is only one place where the xmlns:xxx attribute could be, on
        // the end-point-reference element!
        QName serviceQName = DOMUtil.createQName(el, serviceName);
        String endpointName = el.getAttributeNS(el.getNamespaceURI(), ServiceEndpointImpl.JBI_ENDPOINT_NAME);
        return getEndpoint(serviceQName, endpointName);
    }
    return null;
}

From source file:org.apache.servicemix.jbi.runtime.impl.utils.DOMUtil.java

/**
 * Build a QName from the element name/*from w w w. jav  a2 s .c  om*/
 * @param el
 * @return
 */
public static QName getQName(Element el) {
    if (el == null) {
        return null;
    } else if (el.getNamespaceURI() == null) {
        return new QName(el.getNodeName());
    } else if (el.getPrefix() != null) {
        return new QName(el.getNamespaceURI(), el.getLocalName(), el.getPrefix());
    } else {
        return new QName(el.getNamespaceURI(), el.getLocalName());
    }
}

From source file:org.apache.servicemix.wsn.AbstractCreatePullPoint.java

public CreatePullPointResponse handleCreatePullPoint(
        org.oasis_open.docs.wsn.b_2.CreatePullPoint createPullPointRequest, EndpointManager manager)
        throws UnableToCreatePullPointFault {
    AbstractPullPoint pullPoint = null;//from  w  w w  .  j ava2s. co  m
    boolean success = false;
    try {
        pullPoint = createPullPoint(createPullPointName(createPullPointRequest));
        for (Iterator it = createPullPointRequest.getAny().iterator(); it.hasNext();) {
            Element el = (Element) it.next();

            if ("address".equals(el.getLocalName())
                    && "http://servicemix.apache.org/wsn2005/1.0".equals(el.getNamespaceURI())) {
                String address = DOMUtil.getElementText(el).trim();
                pullPoint.setAddress(address);
            }
        }
        pullPoint.setCreatePullPoint(this);
        //       System.out.println("pullPoint.getAddress()----------------------"+pullPoint.getAddress());

        pullPoints.put(pullPoint.getAddress(), pullPoint);
        pullPoint.create(createPullPointRequest);
        if (manager != null) {
            pullPoint.setManager(manager);
        }
        //pullPoint.register();
        CreatePullPointResponse response = new CreatePullPointResponse();
        response.setPullPoint(AbstractWSAClient.createWSA(pullPoint.getAddress()));
        System.out.println(response.getPullPoint().toString());
        success = true;
        return response;
    } catch (Exception e) {
        log.warn("Unable to register new endpoint", e);
        UnableToCreatePullPointFaultType fault = new UnableToCreatePullPointFaultType();
        throw new UnableToCreatePullPointFault("Unable to register new endpoint", fault, e);
    } finally {
        if (!success && pullPoint != null) {
            pullPoints.remove(pullPoint.getAddress());
            try {
                pullPoint.destroy();
            } catch (UnableToDestroyPullPointFault e) {
                log.info("Error destroying pullPoint", e);
            }
        }
    }
}

From source file:org.apache.servicemix.wsn.AbstractCreatePullPoint.java

protected String createPullPointName(org.oasis_open.docs.wsn.b_2.CreatePullPoint createPullPointRequest) {
    // Let the creator decide which pull point name to use
    String name = null;/*from   ww w  . ja va 2  s  . co m*/
    for (Iterator it = createPullPointRequest.getAny().iterator(); it.hasNext();) {
        Element el = (Element) it.next();
        // System.out.println("el()----------------------"+el);
        if ("name".equals(el.getLocalName())
                && "http://servicemix.apache.org/wsn2005/1.0".equals(el.getNamespaceURI())) {
            name = DOMUtil.getElementText(el).trim();
        }
        // System.out.println("name1()----------------------"+name);
    }
    if (name == null) {
        // If no name is given, just generate one
        name = idGenerator.generateSanitizedId();
        // System.out.println("name2()----------------------"+name);
    }
    return name;
}

From source file:org.apache.shindig.gadgets.templates.XmlTemplateLibrary.java

private Set<TagHandler> parseLibraryDocument(Element root) throws GadgetException {
    ImmutableSet.Builder<TagHandler> handlers = ImmutableSet.builder();

    NodeList nodes = root.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (!(node instanceof Element)) {
            continue;
        }//  www.  j  a  v  a 2s  . com

        Element element = (Element) node;
        if (NAMESPACE_TAG.equals(element.getLocalName())) {
            processNamespace(element);
        } else if (STYLE_TAG.equals(element.getLocalName())) {
            processStyle(element);
        } else if (JAVASCRIPT_TAG.equals(element.getLocalName())) {
            processJavaScript(element);
        } else if (TEMPLATE_TAG.equals(element.getLocalName())) {
            processTemplate(handlers, element);
        } else if (TEMPLATEDEF_TAG.equals(element.getLocalName())) {
            processTemplateDef(handlers, element);
        }
    }

    return handlers.build();
}

From source file:org.apache.sling.its.servlets.ItsImportServlet.java

/**
 * If element has child elements, don't process them and skip those nodes.
 *
 * @param element/*  w ww.jav  a  2 s  .  c  om*/
 *         current element
 * @param itsEng
 *         the ITSEngine
 */
private void skipChildren(final Element element, final ITraversal itsEng) {
    if (element.hasChildNodes()) {
        Node node;
        while ((node = itsEng.nextNode()) != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                if (itsEng.backTracking() && StringUtils.equals(node.getLocalName(), element.getLocalName())) {
                    break;
                }
            }
        }
    }
}

From source file:org.apache.sling.its.servlets.ItsImportServlet.java

/**
 * Store the global rule.//from  w  w w .j a  v a2  s.  co  m
 *
 * @param element
 *         an Element from the Document object.
 * @param resourceType
 *         resource type
 * @param itsEng
 *         the ITSEngine
 */
private void storeGlobalRule(final Element element, final String resourceType, final ITraversal itsEng) {
    if (StringUtils.isNotBlank(resourceType)) {
        String globalPath = SlingItsConstants.getGlobalRules().get(element.getLocalName()) + resourceType;
        if (element.getPrefix() != null) {
            globalPath += String.format("/%s(%d)", element.getLocalName(),
                    getCounter(globalPath + "/" + element.getLocalName()));
            element.setAttribute(SlingItsConstants.NODE_PREFIX, element.getPrefix());
        } else {
            globalPath += String.format("/%s(%d)", element.getNodeName(),
                    getCounter(globalPath + "/" + element.getNodeName()));
        }

        output(globalPath, null, null);
        if (element.getLocalName().equals("param")) {
            output(globalPath, null, element.getTextContent());
        } else if (element.getLocalName().equals(SlingItsConstants.ITS_LOCNOTE_RULE)
                && element.hasChildNodes()) {
            final Element locNoteElement = (Element) XmlNodeUtils.getChildNodeByLocalName(element,
                    SlingItsConstants.ITS_LOCNOTE);
            if (locNoteElement != null) {
                element.setAttribute(SlingItsConstants.ITS_NOTE, locNoteElement.getTextContent());
            }
        }
        setAttributes(element, globalPath);
    }
    skipChildren(element, itsEng);
}

From source file:org.apache.sling.its.servlets.ItsImportServlet.java

/**
 * Store the element and its attribute. The child node of global rules are
 * specially handled so they will not be traversed.
 *
 * @param path//from w ww. java  2 s  . co m
 *         the target path
 * @param resourceType
 *         the resourceType
 * @param doc
 *         the document
 * @param file
 *        the file.
 * @param isExternalDoc
 *         true if this is for storing global rules for external documents
 */
private void store(String path, final String resourceType, final Document doc, final File file,
        final boolean isExternalDoc) {
    final ITraversal itsEng = applyITSRules(doc, file, null, false);
    itsEng.startTraversal();
    Node node;
    while ((node = itsEng.nextNode()) != null) {
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            final Element element = (Element) node;
            // Use !backTracking() to get to the elements only once
            // and to include the empty elements (for attributes).
            if (itsEng.backTracking()) {
                if (!SlingItsConstants.getGlobalRules().containsKey(element.getLocalName())) {
                    path = backTrack(path);
                }
            } else {
                if (element.isSameNode(doc.getDocumentElement()) && !isExternalDoc) {
                    path += "/" + element.getNodeName();
                    output(path, null, null);
                    setAttributes(element, path);
                } else if (SlingItsConstants.getGlobalRules().containsKey(element.getLocalName())) {
                    storeGlobalRule(element, resourceType, itsEng);
                } else if (!isExternalDoc
                        && !SlingItsConstants.getGlobalRules().containsKey(element.getLocalName())
                        && !(element.getParentNode().getLocalName().equals(SlingItsConstants.ITS_RULES)
                                && element.getParentNode().getPrefix() != null)) {
                    if (element.getLocalName().equals(SlingItsConstants.ITS_RULES)
                            && element.getPrefix() != null) {
                        this.hasGlobalRules = true;
                    }
                    if (element.getPrefix() != null) {
                        path += String.format("/%s(%d)", element.getLocalName(),
                                getCounter(path + "/" + element.getLocalName()));
                        element.setAttribute(SlingItsConstants.NODE_PREFIX, element.getPrefix());
                    } else if (element.getNodeName().equals("link")
                            && StringUtils.endsWith(element.getAttribute("rel"), "-rules")) {
                        path += String.format("/%s(%d)", SlingItsConstants.ITS_RULES,
                                getCounter(path + "/" + SlingItsConstants.ITS_RULES));
                        final String prefix = StringUtils.substringBefore(element.getAttribute("rel"),
                                "-rules");
                        element.setAttribute(SlingItsConstants.NODE_PREFIX, prefix);
                        element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                                SlingItsConstants.XMLNS + prefix, Namespaces.ITS_NS_URI);
                        element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:h",
                                Namespaces.HTML_NS_URI);
                        element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:jcr",
                                NamespaceRegistry.NAMESPACE_JCR);
                        this.hasGlobalRules = true;
                    } else {
                        path += String.format("/%s(%d)", element.getNodeName(),
                                getCounter(path + "/" + element.getNodeName()));
                    }
                    output(path, null, null);
                    setAttributes(element, path);
                    if (!element.hasChildNodes()) // Empty elements:
                    {
                        path = backTrack(path);
                    }
                }
            }
            break;
        case Node.TEXT_NODE:
            if (StringUtils.isNotBlank(node.getNodeValue()) && !isExternalDoc) {
                path += String.format("/%s(%d)", SlingItsConstants.TEXT_CONTENT_NODE,
                        getCounter(path + "/" + SlingItsConstants.TEXT_CONTENT_NODE));
                output(path, null, node.getNodeValue());
                path = backTrack(path);
            }
            break;
        default:
            break;
        }
    }
}