Example usage for org.dom4j Document normalize

List of usage examples for org.dom4j Document normalize

Introduction

In this page you can find the example usage for org.dom4j Document normalize.

Prototype

void normalize();

Source Link

Document

Puts all Text nodes in the full depth of the sub-tree underneath this Node, including attribute nodes, into a "normal" form where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are neither adjacent Text nodes nor empty Text nodes.

Usage

From source file:com.digiaplus.lms.core.xml.XMLParser.java

License:Apache License

/**
 * @param in/*  w ww. ja va2 s.  c o  m*/
 * @param validateXML
 * @return parsed document
 */
public Document parse(InputStream in, boolean validateXML) {
    Document document;
    try {
        SAXReader reader = new SAXReader();
        reader.setEntityResolver(er);
        reader.setValidation(validateXML);
        document = reader.read(in, "");
        document.normalize();
    } catch (Exception e) {
        throw new DAPRuntimeException(XMLParser.class, "Exception reading XML", e);
    }
    return document;
}

From source file:com.secuotp.model.xml.XMLCreate.java

public static Document createResponseXML(int error, String service, String message) {
    Document d = new DefaultDocument();

    Element root = d.addElement("secuotp").addAttribute("status", "" + error);
    root.addElement("service").addText(service);
    root.addElement("message").addText(message);

    d.normalize();
    return d;//  w  w w  .  jav  a2 s.c o m
}

From source file:com.secuotp.model.xml.XMLCreate.java

public static Document createResponseXMLWithData(String service, String message, XMLParameter param) {
    Document d = new DefaultDocument();

    Element root = d.addElement("secuotp").addAttribute("status", "101");
    root.addElement("service").addText(service);
    root.addElement("message").addText(message);
    Element responseNode = root.addElement("response");
    while (param.hasNext()) {
        String[] parameter = param.pop();
        responseNode.addElement(parameter[0]).setText(parameter[1]);
    }/*from  www.j a  va  2 s.com*/

    d.normalize();
    return d;
}

From source file:org.apache.poi.openxml4j.opc.internal.ContentTypeManager.java

License:Apache License

/**
 * Save the contents type part./*from   w w w  . ja va 2  s .co m*/
 *
 * @param outStream
 *            The output stream use to save the XML content of the content
 *            types part.
 * @return <b>true</b> if the operation success, else <b>false</b>.
 */
public boolean save(OutputStream outStream) {
    Document xmlOutDoc = DocumentHelper.createDocument();

    // Building namespace
    Namespace dfNs = Namespace.get("", TYPES_NAMESPACE_URI);
    Element typesElem = xmlOutDoc.addElement(new QName(TYPES_TAG_NAME, dfNs));

    // Adding default types
    for (Entry<String, String> entry : defaultContentType.entrySet()) {
        appendDefaultType(typesElem, entry);
    }

    // Adding specific types if any exist
    if (overrideContentType != null) {
        for (Entry<PackagePartName, String> entry : overrideContentType.entrySet()) {
            appendSpecificTypes(typesElem, entry);
        }
    }
    xmlOutDoc.normalize();

    // Save content in the specified output stream
    return this.saveImpl(xmlOutDoc, outStream);
}

From source file:org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller.java

License:Apache License

/**
 * Save relationships into the part./* w  w w  . j  a  v  a2  s . c  o m*/
 *
 * @param rels
 *            The relationships collection to marshall.
 * @param relPartName
 *            Part name of the relationship part to marshall.
 * @param zos
 *            Zip output stream in which to save the XML content of the
 *            relationships serialization.
 */
public static boolean marshallRelationshipPart(PackageRelationshipCollection rels, PackagePartName relPartName,
        ZipOutputStream zos) {
    // Building xml
    Document xmlOutDoc = DocumentHelper.createDocument();
    // make something like <Relationships
    // xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    Namespace dfNs = Namespace.get("", PackageNamespaces.RELATIONSHIPS);
    Element root = xmlOutDoc.addElement(new QName(PackageRelationship.RELATIONSHIPS_TAG_NAME, dfNs));

    // <Relationship
    // TargetMode="External"
    // Id="rIdx"
    // Target="http://www.custom.com/images/pic1.jpg"
    // Type="http://www.custom.com/external-resource"/>

    URI sourcePartURI = PackagingURIHelper.getSourcePartUriFromRelationshipPartUri(relPartName.getURI());

    for (PackageRelationship rel : rels) {
        // the relationship element
        Element relElem = root.addElement(PackageRelationship.RELATIONSHIP_TAG_NAME);

        // the relationship ID
        relElem.addAttribute(PackageRelationship.ID_ATTRIBUTE_NAME, rel.getId());

        // the relationship Type
        relElem.addAttribute(PackageRelationship.TYPE_ATTRIBUTE_NAME, rel.getRelationshipType());

        // the relationship Target
        String targetValue;
        URI uri = rel.getTargetURI();
        if (rel.getTargetMode() == TargetMode.EXTERNAL) {
            // Save the target as-is - we don't need to validate it,
            //  alter it etc
            targetValue = uri.toString();

            // add TargetMode attribute (as it is external link external)
            relElem.addAttribute(PackageRelationship.TARGET_MODE_ATTRIBUTE_NAME, "External");
        } else {
            URI targetURI = rel.getTargetURI();
            targetValue = PackagingURIHelper.relativizeURI(sourcePartURI, targetURI, true).toString();
        }
        relElem.addAttribute(PackageRelationship.TARGET_ATTRIBUTE_NAME, targetValue);
    }

    xmlOutDoc.normalize();

    // String schemaFilename = Configuration.getPathForXmlSchema()+
    // File.separator + "opc-relationships.xsd";

    // Save part in zip
    ZipEntry ctEntry = new ZipEntry(
            ZipHelper.getZipURIFromOPCName(relPartName.getURI().toASCIIString()).getPath());
    try {
        zos.putNextEntry(ctEntry);
        if (!StreamHelper.saveXmlInStream(xmlOutDoc, zos)) {
            return false;
        }
        zos.closeEntry();
    } catch (IOException e) {
        logger.log(POILogger.ERROR, "Cannot create zip entry " + relPartName, e);
        return false;
    }
    return true; // success
}

From source file:org.openxml4j.opc.internal.marshallers.ZipPartMarshaller.java

License:Apache License

/**
 * Save relationships into the part./*w w  w. j  av  a2s  .  c  o  m*/
 * 
 * @param rels
 *            The relationships collection to marshall.
 * @param relPartURI
 *            Part name of the relationship part to marshall.
 * @param zos
 *            Zip output stream in which to save the XML content of the
 *            relationships serialization.
 */
public static boolean marshallRelationshipPart(PackageRelationshipCollection rels, PackagePartName relPartName,
        ZipOutputStream zos) {
    // Building xml
    Document xmlOutDoc = DocumentHelper.createDocument();
    // make something like <Relationships
    // xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    Namespace dfNs = Namespace.get("", PackageNamespaces.RELATIONSHIPS);
    Element root = xmlOutDoc.addElement(new QName(PackageRelationship.RELATIONSHIPS_TAG_NAME, dfNs));

    // <Relationship
    // TargetMode="External"
    // Id="rIdx"
    // Target="http://www.custom.com/images/pic1.jpg"
    // Type="http://www.custom.com/external-resource"/>

    URI sourcePartURI = PackagingURIHelper.getSourcePartUriFromRelationshipPartUri(relPartName.getURI());

    for (PackageRelationship rel : rels) {
        // L'lment de la relation
        Element relElem = root.addElement(PackageRelationship.RELATIONSHIP_TAG_NAME);

        // L'attribut ID
        relElem.addAttribute(PackageRelationship.ID_ATTRIBUTE_NAME, rel.getId());

        // L'attribut Type
        relElem.addAttribute(PackageRelationship.TYPE_ATTRIBUTE_NAME, rel.getRelationshipType());

        // L'attribut Target
        String targetValue;
        URI uri = rel.getTargetURI();
        if (rel.getTargetMode() == TargetMode.EXTERNAL) {
            // Save the target as-is - we don't need to validate it,
            //  alter it etc
            try {
                targetValue = URLEncoder.encode(uri.toString(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                targetValue = uri.toString();
            }

            // add TargetMode attribut (as it is external link external)
            relElem.addAttribute(PackageRelationship.TARGET_MODE_ATTRIBUTE_NAME, "External");
        } else {
            targetValue = PackagingURIHelper.relativizeURI(sourcePartURI, rel.getTargetURI()).getPath();
        }
        relElem.addAttribute(PackageRelationship.TARGET_ATTRIBUTE_NAME, targetValue);
    }

    xmlOutDoc.normalize();

    // String schemaFilename = Configuration.getPathForXmlSchema()+
    // File.separator + "opc-relationships.xsd";

    // Save part in zip
    ZipEntry ctEntry = new ZipEntry(
            ZipHelper.getZipURIFromOPCName(relPartName.getURI().toASCIIString()).getPath());
    try {
        // Cration de l'entre dans le fichier ZIP
        zos.putNextEntry(ctEntry);
        if (!StreamHelper.saveXmlInStream(xmlOutDoc, zos)) {
            return false;
        }
        zos.closeEntry();
    } catch (IOException e) {
        logger.error("Cannot create zip entry " + relPartName, e);
        return false;
    }
    return true; // success
}