Example usage for org.w3c.dom Document createAttributeNS

List of usage examples for org.w3c.dom Document createAttributeNS

Introduction

In this page you can find the example usage for org.w3c.dom Document createAttributeNS.

Prototype

public Attr createAttributeNS(String namespaceURI, String qualifiedName) throws DOMException;

Source Link

Document

Creates an attribute of the given qualified name and namespace URI.

Usage

From source file:org.globus.wsrf.tools.wsdl.TypesProcessor.java

public void addResourceProperties(QName portTypeName, Map resourcePropertyElements, Map schemaDocumentLocations)
        throws Exception {
    logger.debug("Starting to build resource properties element");
    PortType portType = this.getPortType(portTypeName);

    QName resourceProperties = (QName) portType.getExtensionAttribute(RP);

    addPrefix(WSRP_NS, "wsrp");

    Element schema = getSchema();
    Document doc = schema.getOwnerDocument();
    String schemaPrefix = "";
    if (schema.getPrefix() != null) {
        schemaPrefix = schema.getPrefix() + ":";
    }/*w  ww.  ja v a2  s.c  om*/

    Element properties = null;
    if (resourceProperties == null) {
        resourceProperties = new QName(portType.getQName().getLocalPart() + "GTWSDLResourceProperties");
        portType.setExtensionAttribute(RP, resourceProperties);
        properties = doc.createElementNS(XSD_NS, schemaPrefix + "element");
        properties.setAttribute("name", resourceProperties.getLocalPart());
        properties.appendChild(doc.createTextNode("\n        "));
        schema.appendChild(properties);
        schema.appendChild(doc.createTextNode("\n    "));
    } else {
        NodeList elementNodes = schema.getElementsByTagNameNS(XSD_NS, "element");
        String name;
        Element element;
        for (int i = 0; i < elementNodes.getLength(); i++) {
            element = (Element) elementNodes.item(i);
            name = element.getAttribute("name");
            if (name != null && XmlUtils.getFullQNameFromString(name, element).getLocalPart()
                    .equals(resourceProperties.getLocalPart())) {
                properties = element;
                break;
            }
        }

        if (properties == null) {
            throw new Exception(
                    "Unable to find '" + resourceProperties + "' element in WSDL schema section of '"
                            + this.definition.getDocumentBaseURI() + "' document.");
        }
    }
    String type = properties.getAttribute("type");
    NodeList complexTypeElements;
    Element complexType = null;

    if (type != null && !type.equals("")) {
        complexTypeElements = schema.getElementsByTagNameNS(XSD_NS, "complexType");
        Element currentType;
        QName currentName;
        QName typeName = XmlUtils.getFullQNameFromString(type, properties);
        for (int i = 0; i < complexTypeElements.getLength(); i++) {
            currentType = (Element) complexTypeElements.item(i);
            currentName = XmlUtils.getFullQNameFromString(currentType.getAttribute("name"), currentType);
            if (currentName.getLocalPart().equals(typeName.getLocalPart())) {
                complexType = currentType;
                break;
            }
        }
        if (complexType == null) {
            throw new Exception("Unable to find type entry for '" + resourceProperties + "' element");
        }
    } else {
        complexTypeElements = properties.getElementsByTagNameNS(XSD_NS, "complexType");
        if (complexTypeElements.getLength() > 0) {
            complexType = (Element) complexTypeElements.item(0);
        } else {
            complexType = doc.createElementNS(XSD_NS, schemaPrefix + "complexType");
            complexType.appendChild(doc.createTextNode("\n          "));
            properties.appendChild(complexType);
            properties.appendChild(doc.createTextNode("\n      "));
        }
    }

    NodeList sequenceElements = complexType.getElementsByTagNameNS(XSD_NS, "sequence");
    Element sequence;
    if (sequenceElements.getLength() > 0) {
        sequence = (Element) sequenceElements.item(0);
    } else {
        sequence = doc.createElementNS(XSD_NS, schemaPrefix + "sequence");
        complexType.appendChild(sequence);
        complexType.appendChild(doc.createTextNode("\n        "));
    }

    Collection resourcePropertiesList = resourcePropertyElements.values();
    Iterator elementIterator = resourcePropertiesList.iterator();
    int nsCounter = 0;

    while (elementIterator.hasNext()) {
        sequence.appendChild(doc.createTextNode("\n            "));
        Element resourcePropertyElement = doc.createElementNS(XSD_NS, schemaPrefix + "element");
        XSParticle particle = (XSParticle) elementIterator.next();
        XSElementDeclaration element = (XSElementDeclaration) particle.getTerm();
        String prefix = XmlUtils.getPrefix(element.getNamespace(), schema);

        addSchemaDefinitions(element, schemaDocumentLocations, schema);

        if (prefix == null) {
            prefix = "rpns" + String.valueOf(nsCounter++);
            Attr nameSpace = doc.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix);
            nameSpace.setValue(element.getNamespace());
            schema.setAttributeNode(nameSpace);
        }

        resourcePropertyElement.setAttribute("ref", prefix + ":" + element.getName());
        resourcePropertyElement.setAttribute("minOccurs", String.valueOf(particle.getMinOccurs()));
        if (particle.getMaxOccursUnbounded()) {
            resourcePropertyElement.setAttribute("maxOccurs", String.valueOf("unbounded"));
        } else {
            resourcePropertyElement.setAttribute("maxOccurs", String.valueOf(particle.getMaxOccurs()));
        }
        sequence.appendChild(resourcePropertyElement);
    }
    sequence.appendChild(doc.createTextNode("\n          "));
    logger.debug("Resource properties element: " + XmlUtils.getElementAsString(properties));
}

From source file:org.ojbc.util.xml.XmlUtils.java

/**
 * Add an attribute to the specified element
 * @param parent the element to which we add the attribute
 * @param ns the namespace of the attribute
 * @param attributeName the name of the attribute
 * @param value the value of the attribute
 * @return the attribute/*from  w w w  .  java  2 s.co m*/
 */
public static final Attr addAttribute(Element parent, String ns, String attributeName, String value) {
    Document doc = parent.getOwnerDocument();
    Attr ret = doc.createAttributeNS(ns, attributeName);
    ret.setTextContent(value);
    ret.setPrefix(OJBC_NAMESPACE_CONTEXT.getPrefix(ns));
    parent.setAttributeNode(ret);
    return ret;
}

From source file:org.osaf.cosmo.xml.DomReader.java

private static Attr readAttribute(int i, Document d, XMLStreamReader reader) throws XMLStreamException {
    Attr a = null;/*from   w  w w  .j a  v a 2s.  c o m*/

    String local = reader.getAttributeLocalName(i);
    String ns = reader.getAttributeNamespace(i);
    if (ns != null) {
        String prefix = reader.getAttributePrefix(i);
        String qualified = prefix != null ? prefix + ":" + local : local;
        a = d.createAttributeNS(ns, qualified);
    } else {
        a = d.createAttribute(reader.getAttributeLocalName(i));
    }

    //if (log.isDebugEnabled())
    //log.debug("Reading attribute " + a.getName());

    a.setValue(reader.getAttributeValue(i));

    return a;
}

From source file:org.regenstrief.util.XMLUtil.java

/**
 * Imports a Node into a Document without creating new prefixes like Document.importNode
 * //from w ww .  j a v  a2s  . c o m
 * @param doc the Document
 * @param toImport the Node to import
 * @return the imported Node
 **/
public final static Node importNode(final Document doc, final Node toImport) {
    Node imported;
    NodeList list;
    NamedNodeMap map;
    int i, size;

    if (toImport instanceof Element) {
        //imported = doc.createElement(((Element) toImport).getTagName()); // Ever want this?
        // This should copy toImport's prefix; I don't think that's what we want, but we don't have a namespace context for doc
        imported = doc.createElementNS(toImport.getNamespaceURI(), toImport.getNodeName());
        for (map = toImport.getAttributes(), size = size(map), i = 0; i < size; i++) {
            final Node n = map.item(i);
            if (n != null) {
                ((Element) imported).setAttributeNode((Attr) importNode(doc, n));
            }
        }
    } else if (toImport instanceof Attr) {
        final String uri = toImport.getNamespaceURI();
        if (Util.isEmpty(uri)) {
            imported = doc.createAttribute(
                    "xmlns".equals(getPrefix(toImport)) ? toImport.getNodeName() : getLocalName(toImport));
        } else {
            imported = doc.createAttributeNS(uri, toImport.getNodeName());
        }
        //imported.setNodeValue(toImport.getNodeValue()); // The value will be copied when we import children below
    } else {
        imported = doc.importNode(toImport, false);
    }
    for (list = toImport.getChildNodes(), size = size(list), i = 0; i < size; i++) {
        final Node n = list.item(i);
        if (n != null) {
            imported.appendChild(importNode(doc, n));
        }
    }

    return imported;
}

From source file:org.talend.designer.maven.utils.PomUtil.java

/**
 * /*from w  w  w.j  a va2  s . c  o m*/
 * Create pom without refresh eclipse resources
 * 
 * @param artifact
 * @return
 */
public static String generatePom2(MavenArtifact artifact) {
    try {
        Project project = ProjectManager.getInstance().getCurrentProject();
        IProject fsProject = ResourceUtils.getProject(project);
        SecureRandom random = new SecureRandom();
        IPath tempPath = fsProject.getLocation().append("temp").append("pom" + Math.abs(random.nextLong()));
        File tmpFolder = new File(tempPath.toPortableString());
        tmpFolder.mkdirs();
        String pomFile = tempPath.append(TalendMavenConstants.POM_FILE_NAME).toPortableString();
        Model pomModel = new Model();
        pomModel.setModelVersion(TalendMavenConstants.POM_VERSION);
        pomModel.setModelEncoding(TalendMavenConstants.DEFAULT_ENCODING);
        pomModel.setGroupId(artifact.getGroupId());
        pomModel.setArtifactId(artifact.getArtifactId());
        pomModel.setVersion(artifact.getVersion());
        String artifactType = artifact.getType();
        if (artifactType == null || "".equals(artifactType)) {
            artifactType = TalendMavenConstants.PACKAGING_JAR;
        }
        pomModel.setPackaging(artifactType);

        ByteArrayOutputStream buf = new ByteArrayOutputStream();

        MavenPlugin.getMaven().writeModel(pomModel, buf);

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        TransformerFactory tfactory = TransformerFactory.newInstance();

        Document document = documentBuilder.parse(new ByteArrayInputStream(buf.toByteArray()));
        Element documentElement = document.getDocumentElement();

        NamedNodeMap attributes = documentElement.getAttributes();

        if (attributes == null || attributes.getNamedItem("xmlns") == null) { //$NON-NLS-1$
            Attr attr = document.createAttribute("xmlns"); //$NON-NLS-1$
            attr.setTextContent("http://maven.apache.org/POM/4.0.0"); //$NON-NLS-1$
            documentElement.setAttributeNode(attr);
        }

        if (attributes == null || attributes.getNamedItem("xmlns:xsi") == null) { //$NON-NLS-1$
            Attr attr = document.createAttribute("xmlns:xsi"); //$NON-NLS-1$
            attr.setTextContent("http://www.w3.org/2001/XMLSchema-instance"); //$NON-NLS-1$
            documentElement.setAttributeNode(attr);
        }

        if (attributes == null || attributes.getNamedItem("xsi:schemaLocation") == null) { //$NON-NLS-1$
            Attr attr = document.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance", //$NON-NLS-1$
                    "xsi:schemaLocation"); //$NON-NLS-1$
            attr.setTextContent(
                    "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"); //$NON-NLS-1$
            documentElement.setAttributeNode(attr);
        }
        Transformer transformer = tfactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File(pomFile));
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);

        return pomFile;
    } catch (PersistenceException e) {
        ExceptionHandler.process(e);
    } catch (CoreException e) {
        ExceptionHandler.process(e);
    } catch (ParserConfigurationException e) {
        ExceptionHandler.process(e);
    } catch (SAXException e) {
        ExceptionHandler.process(e);
    } catch (IOException e) {
        ExceptionHandler.process(e);
    } catch (TransformerConfigurationException e) {
        ExceptionHandler.process(e);
    } catch (TransformerException e) {
        ExceptionHandler.process(e);
    }
    return null;
}

From source file:org.unitedinternet.cosmo.util.DomReader.java

private static Attr readAttribute(int i, Document d, XMLStreamReader reader) throws XMLStreamException {
    Attr a = null;//  w  ww. jav  a2 s .c  o m

    String local = reader.getAttributeLocalName(i);
    String ns = reader.getAttributeNamespace(i);
    if (ns != null && !ns.equals("")) {
        String prefix = reader.getAttributePrefix(i);
        String qualified = prefix != null ? prefix + ":" + local : local;
        a = d.createAttributeNS(ns, qualified);
    } else {
        a = d.createAttribute(reader.getAttributeLocalName(i));
    }

    //if (log.isDebugEnabled())
    //log.debug("Reading attribute " + a.getName());

    a.setValue(reader.getAttributeValue(i));

    return a;
}