Example usage for org.dom4j DocumentHelper createNamespace

List of usage examples for org.dom4j DocumentHelper createNamespace

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper createNamespace.

Prototype

public static Namespace createNamespace(String prefix, String uri) 

Source Link

Usage

From source file:nz.co.senanque.schemaparser.SchemaVisitorDom4j.java

License:Apache License

@Override
public void initialize(String xsdpackageName, String targetNamespace) {
    Element root = document.addElement(rootName);
    Namespace xsi = DocumentHelper.createNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.add(xsi);// w w w  . j  a va 2 s.c om
    if (targetNamespace != null) {
        Namespace tns = DocumentHelper.createNamespace("tns", targetNamespace);
        root.add(tns);
        QName schemaLocation = DocumentHelper.createQName("schemaLocation", xsi);
        Attribute attribute = DocumentHelper.createAttribute(root, schemaLocation,
                targetNamespace + " " + location + " " + rootName.getNamespaceURI() + " " + rootLocation);
        root.add(attribute);
    }
    elements.push(root);
}

From source file:org.codehaus.modello.plugin.jaxrs.JaxRSMappingModelloGenerator.java

License:Apache License

private void generateJaxRS(File webXml, Model model) throws IOException, ModelloException {
    OutputStreamWriter fileWriter = new OutputStreamWriter(new FileOutputStream(webXml), "UTF-8");

    org.dom4j.io.XMLWriter writer = null;

    namespace = DocumentHelper.createNamespace("", "http://java.sun.com/xml/ns/j2ee");
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("web-app");
    root.addNamespace("", "http://java.sun.com/xml/ns/j2ee");
    root.addAttribute("version", "2.4");
    root.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.addAttribute("xsi:schemaLocation",
            "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd");
    Element displayName = addElement(root, "display-name", model.getName());
    // add the declaration for the Jersey servlet
    Element servlet = addElement(root, "servlet");
    addElement(servlet, "servlet-name", JERSEY_SERVLET_NAME);
    addElement(servlet, "servlet-class", "com.sun.jersey.spi.container.servlet.ServletContainer");
    Element pkgsParam = addElement(servlet, "init-param");
    addElement(pkgsParam, "param-name", "com.sun.jersey.config.property.packages");
    addElement(servlet, "load-on-startup", "1");

    Set<String> pkgs = new HashSet<String>();

    // Processed classes to be mapped here
    for (Iterator it = model.getClasses(getGeneratedVersion()).iterator(); it.hasNext();) {
        ModelClass modelClass = (ModelClass) it.next();

        JaxRSClassLevelMetadata metadata = (JaxRSClassLevelMetadata) modelClass
                .getMetadata(JaxRSClassLevelMetadata.ID);

        // only generate servlet-mappings for classes which have a jaxrs.urls parameter
        if (!isEmpty(metadata.getUrls())) {
            String packageName = modelClass.getPackageName(isPackageWithVersion(), getGeneratedVersion());
            if (!pkgs.contains(packageName)) {
                pkgs.add(packageName);//from  w ww  .ja  v  a2s  . c  om
            }

            String[] urls = metadata.getUrls().split(",");
            for (String url : urls) {
                Element mapping = addElement(root, "servlet-mapping");
                addElement(mapping, "servlet-name", JERSEY_SERVLET_NAME);
                addElement(mapping, "url-pattern", url);
            }
        }

    }

    String pkgsString = "";
    for (String s : pkgs) {
        if (!"".equals(pkgsString))
            pkgsString += ",";
        pkgsString += s;
    }
    addElement(pkgsParam, "param-value", pkgsString);

    OutputFormat format = OutputFormat.createPrettyPrint();
    writer = new XMLWriter(fileWriter, format);
    writer.write(document);
    writer.close();
}

From source file:org.codehaus.modello.plugin.jpa.JpaOrmMappingModelloGenerator.java

License:Apache License

private void generateOrm(File orm, Model model) throws IOException, ModelloException {
    OutputStreamWriter fileWriter = new OutputStreamWriter(new FileOutputStream(orm), "UTF-8");

    org.dom4j.io.XMLWriter writer = null;

    JpaModelMetadata modelMetadata = (JpaModelMetadata) model.getMetadata(JpaModelMetadata.ID);

    String jpaVersion = modelMetadata.getVersion();

    String jpaUnit = modelMetadata.getUnit();

    Document document = DocumentHelper.createDocument();

    Element root = null;//  w ww .  java  2 s  .c om

    if (JpaMetadataPlugin.JPA_VERSION_1.equals(jpaVersion) || (jpaVersion == null)) {
        namespace = DocumentHelper.createNamespace("", "http://java.sun.com/xml/ns/persistence/orm");
        root = document.addElement("entity-mappings");
        root.addNamespace("", "http://java.sun.com/xml/ns/persistence/orm");
        root.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.addAttribute("xsi:schemaLocation",
                "http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd");
        root.addAttribute("version", "1.0");
    } else if (JpaMetadataPlugin.JPA_VERSION_2.equals(jpaVersion)) {
        namespace = DocumentHelper.createNamespace("", "http://java.sun.com/xml/ns/persistence");
        root = document.addElement("persistence");
        root.addNamespace("", "http://java.sun.com/xml/ns/persistence");
        root.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.addAttribute("version", "2.0");
        root.addAttribute("xsi:schemaLocation",
                "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd");
        Element persistenceUnit = addElement(root, "persistence-unit");
        persistenceUnit.addAttribute("name", jpaUnit);
        addElement(persistenceUnit, "provider", "org.apache.openjpa.persistence.PersistenceProviderImpl");
    }

    // Processed classes to be mapped here
    for (Iterator it = model.getClasses(getGeneratedVersion()).iterator(); it.hasNext();) {
        ModelClass modelClass = (ModelClass) it.next();

        String packageName = modelClass.getPackageName(isPackageWithVersion(), getGeneratedVersion());

        JpaClassLevelMetadata metadata = (JpaClassLevelMetadata) modelClass
                .getMetadata(JpaClassLevelMetadata.ID);

        List processorMetadataList = metadata.getProcessorMetadata();
        for (Iterator it2 = processorMetadataList.iterator(); it2.hasNext();) {
            ProcessorMetadata procMetadata = (ProcessorMetadata) it2.next();
            try {
                ((ClassMetadataProcessorMetadata) procMetadata).setModelClass(modelClass);

                ((ClassMetadataProcessorMetadata) procMetadata).setPackageName(modelClass.getPackageName());

                MetadataProcessor metadataProcessor = processorFactory.createMetadataProcessor(procMetadata);

                // set up Processor Context.
                MetadataProcessorContext processorContext = new MetadataProcessorContext();
                processorContext.setDocument(document);
                processorContext.setModel(model);

                boolean valid = metadataProcessor.validate(processorContext, procMetadata);

                if (valid)
                    metadataProcessor.process(processorContext, procMetadata);
                else
                    throw new ModelloException(
                            "Processor Metadata validate failed for '" + procMetadata.getKey() + "' in class "
                                    + modelClass.getPackageName() + '.' + modelClass.getName());
            } catch (MetadataProcessorInstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MetadataProcessorException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    if (JpaMetadataPlugin.JPA_VERSION_2.equals(jpaVersion)) {

        Element persistenceUnit = root.element("persistence-unit");
        addElement(persistenceUnit, "validation-mode", "NONE");
        File propsFile = new File("src/main/resources/" + jpaUnit + ".properties");
        if (propsFile.exists()) {
            Element properties = addElement(persistenceUnit, "properties");
            Properties persistenceProviderProps = new Properties();
            persistenceProviderProps.load(new FileInputStream(propsFile));
            Iterator it = persistenceProviderProps.keySet().iterator();
            while (it.hasNext()) {
                String k = it.next().toString();
                String v = persistenceProviderProps.getProperty(k);
                Element property = addElement(properties, "property");
                property.addAttribute("name", k);
                property.addAttribute("value", v);
            }
        }
    }

    OutputFormat format = OutputFormat.createPrettyPrint();
    writer = new XMLWriter(fileWriter, format);
    writer.write(document);
    writer.close();
}

From source file:org.hudsonci.xpath.impl.XNamespaceAttribute.java

License:Open Source License

public XNamespaceAttribute(String prefix, String value) {
    this.name = "".equals(prefix) ? "xmlns" : "xmlns:" + prefix;
    this.value = value;
    this.ns = DocumentHelper.createNamespace("xmlns", "");
}

From source file:org.jivesoftware.openfire.fastpath.providers.ChatMetadataProvider.java

License:Open Source License

public Element getMetaDataElement(Map<String, String> metaData) {
    QName qName = DocumentHelper.createQName("metadata",
            DocumentHelper.createNamespace("", "http://jivesoftware.com/protocol/workgroup"));
    Element metaDataElement = DocumentHelper.createElement(qName);

    for (Map.Entry<String, String> entry : metaData.entrySet()) {
        String name = entry.getKey();
        String value = entry.getValue();
        Element elem = metaDataElement.addElement("value");
        elem.addAttribute("name", name).setText(value);
    }//from   w w  w.  j  av a2s.co  m
    return metaDataElement;
}

From source file:org.jivesoftware.xmpp.workgroup.request.InvitationRequest.java

License:Open Source License

@Override
public Element getSessionElement() {
    // Add the workgroup of the original user request
    QName qName = DocumentHelper.createQName("session",
            DocumentHelper.createNamespace("", "http://jivesoftware.com/protocol/workgroup"));
    Element sessionElement = DocumentHelper.createElement(qName);
    sessionElement.addAttribute("id", sessionID);
    sessionElement.addAttribute("workgroup", userRequest.getWorkgroup().getJID().toString());
    return sessionElement;
}

From source file:org.jivesoftware.xmpp.workgroup.request.Request.java

License:Open Source License

public Element getMetaDataElement() {
    QName qName = DocumentHelper.createQName("metadata",
            DocumentHelper.createNamespace("", "http://jivesoftware.com/protocol/workgroup"));
    Element metaDataElement = DocumentHelper.createElement(qName);

    for (String name : metaData.keySet()) {
        List<String> values = metaData.get(name);

        for (String value : values) {
            Element elem = metaDataElement.addElement("value");
            elem.addAttribute("name", name).setText(value);
        }/*from ww  w  .j  a v  a  2  s  .c  o m*/
    }
    return metaDataElement;
}

From source file:org.jivesoftware.xmpp.workgroup.request.UserRequest.java

License:Open Source License

@Override
public Element getSessionElement() {
    QName qName = DocumentHelper.createQName("session",
            DocumentHelper.createNamespace("", "http://jivesoftware.com/protocol/workgroup"));
    Element sessionElement = DocumentHelper.createElement(qName);
    sessionElement.addAttribute("id", requestID);
    sessionElement.addAttribute("workgroup", getWorkgroup().getJID().toString());
    return sessionElement;
}

From source file:org.neuclear.asset.contracts.AssetGlobals.java

License:Open Source License

private static Namespace createNameSpace() {
    return DocumentHelper.createNamespace(ASSET_NSPREFIX, XFER_ASSETS);
}

From source file:org.neuclear.asset.orders.TransferGlobals.java

License:Open Source License

public static Namespace createNameSpace() {
    return DocumentHelper.createNamespace(XFER_NSPREFIX, XFER_NSURI);
}