Example usage for org.dom4j.io XMLWriter startDocument

List of usage examples for org.dom4j.io XMLWriter startDocument

Introduction

In this page you can find the example usage for org.dom4j.io XMLWriter startDocument.

Prototype

public void startDocument() throws SAXException 

Source Link

Usage

From source file:org.alfresco.module.vti.web.fp.PropfindMethod.java

License:Open Source License

/**
 * <p>/*from www.  ja v  a 2s.  c  o m*/
 *  Handle PROPFIND method of the MS-WDVME protocol.
 *  <ul>
 *      <li>1. If MS-Doclib header with value '1' presents in request it check for library existing and return
 *             the MS-Doclib header in response with value of the valid URL that is point to the library that 
 *             resource belongs to.
 *      </li>
 *      <li>
 *          2. If Depth header in request has a value '0' returns properties for the requested resource.
 *      </li>
 *      <li>
 *          3. If Depth header in request has a value 'infinity' (requested resources should be collection) 
 *             returns properties for the requested resource and all resources that are stored in requested 
 *             collection. 
 *      </li>
 *  </ul>
 *  In case if requested resource was not found then HTTP 404 status is sent to the client.
 * </p>
 */
@Override
protected void executeImpl() throws WebDAVServerException, Exception {
    String msDoclib = m_request.getHeader(HEADER_MS_DOCLIB);

    if (msDoclib != null && msDoclib.equals("1")) {
        try {
            getDAVHelper().getParentNodeForPath(getRootNodeRef(), m_strPath);
        } catch (FileNotFoundException e) {
            m_response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        // TODO: this shouldn't assume the requested URL is the one exposed to the outside world
        // (may be behind proxy). Change to parse properly.
        String docLibHref = URLDecoder
                .decode(m_request.getRequestURL().substring(0, m_request.getRequestURL().lastIndexOf("/")));
        m_response.setHeader(HEADER_MS_DOCLIB, docLibHref);
        return;
    }

    m_response.setStatus(WebDAV.WEBDAV_SC_MULTI_STATUS);

    if (logger.isDebugEnabled()) {
        logger.debug("processing PROPFIND request with uri: " + m_request.getRequestURI());
    }

    FileInfo pathNodeInfo = null;

    // Check that the path exists
    pathNodeInfo = pathHelper.resolvePathFileInfo(m_strPath);

    if (pathNodeInfo == null) {
        // The path is not valid - send a 404 error back to the client
        m_response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // Note the null check, as root node may be null in cloud.
    if (pathNodeInfo.getNodeRef() != null && getFileFolderService().isHidden(pathNodeInfo.getNodeRef())) {
        // ALF-17662, the path is hidden - send a 404 error back to the client
        m_response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // Set the response content type
    m_response.setContentType(WebDAV.XML_CONTENT_TYPE);

    // Create multistatus response
    XMLWriter xml = createXMLWriter();

    xml.startDocument();

    String nsdec = generateNamespaceDeclarations(namespaceMap);
    xml.startElement(WebDAV.DAV_NS, WebDAV.XML_MULTI_STATUS + nsdec, WebDAV.XML_NS_MULTI_STATUS + nsdec,
            getDAVHelper().getNullAttributes());

    if (containsCollblob) {
        xml.startElement("http://schemas.microsoft.com/repl/", "repl", "Repl:repl",
                getDAVHelper().getNullAttributes());
        xml.startElement("http://schemas.microsoft.com/repl/", "collblob", "Repl:collblob",
                getDAVHelper().getNullAttributes());
        xml.write(VtiUtils.formatPropfindDate(new Date()));
        xml.endElement("http://schemas.microsoft.com/repl/", "collblob", "Repl:collblob");
        xml.endElement("http://schemas.microsoft.com/repl/", "repl", "Repl:repl");
    }

    xml.write("\n");

    // Create the path for the current location in the tree
    StringBuilder baseBuild = new StringBuilder(256);
    baseBuild.append("");
    if (baseBuild.length() == 0 || baseBuild.charAt(baseBuild.length() - 1) != WebDAVHelper.PathSeperatorChar) {
        baseBuild.append(WebDAVHelper.PathSeperatorChar);
    }
    String basePath = baseBuild.toString();

    // Output the response for the root node, depth zero
    try {
        generateResponseForNode(xml, pathNodeInfo, basePath);
    } catch (Exception e) {
        m_response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // If additional levels are required and the root node is a folder then recurse to the required
    // level and output node details a level at a time
    if (depth != DEPTH_ZERO && pathNodeInfo.isFolder()) {
        // Create the initial list of nodes to report
        List<FileInfo> nodeInfos = new ArrayList<FileInfo>(10);
        nodeInfos.add(pathNodeInfo);

        int curDepth = WebDAV.DEPTH_1;

        // List of next level of nodes to report
        List<FileInfo> nextNodeInfos = null;
        if (depth > WebDAV.DEPTH_1) {
            nextNodeInfos = new ArrayList<FileInfo>(10);
        }

        // Loop reporting each level of nodes to the requested depth
        while (curDepth <= depth && nodeInfos != null) {
            // Clear out the next level of nodes, if required
            if (nextNodeInfos != null) {
                nextNodeInfos.clear();
            }

            // Output the current level of node(s), the node list should
            // only contain folder nodes

            for (FileInfo curNodeInfo : nodeInfos) {
                // Get the list of child nodes for the current node
                List<FileInfo> childNodeInfos = getDAVHelper().getChildren(curNodeInfo);

                // can skip the current node if it doesn't have children
                if (childNodeInfos.size() == 0) {
                    continue;
                }

                // Output the child node details
                // Generate the base path for the current parent node

                baseBuild.setLength(0);
                try {
                    String pathSnippet = null;
                    if ((pathNodeInfo.getNodeRef() == null) && (curNodeInfo.getNodeRef() == null)) {
                        pathSnippet = "/";
                    } else {
                        pathSnippet = getDAVHelper().getPathFromNode(pathNodeInfo.getNodeRef(),
                                curNodeInfo.getNodeRef());
                    }

                    baseBuild.append(pathSnippet);
                } catch (FileNotFoundException e) {
                    // move to the next node
                    continue;
                }

                int curBaseLen = baseBuild.length();

                // Output the child node details
                for (FileInfo curChildInfo : childNodeInfos) {
                    // ALF-17662, do not show link nodes and hidden documents
                    // Note the null check, as node may be null in cloud.
                    final boolean isHidden = curChildInfo.getNodeRef() != null
                            && getFileFolderService().isHidden(curChildInfo.getNodeRef());
                    if (curChildInfo.isLink() == false && !isHidden) {
                        // Build the path for the current child node
                        baseBuild.setLength(curBaseLen);

                        baseBuild.append(WebDAVHelper.PathSeperatorChar + curChildInfo.getName());

                        // Output the current child node details
                        try {
                            generateResponseForNode(xml, curChildInfo, baseBuild.toString());
                        } catch (Exception e) {
                            m_response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                            return;
                        }

                        // If the child is a folder add it to the list of next level nodes
                        if (nextNodeInfos != null && curChildInfo.isFolder()) {
                            nextNodeInfos.add(curChildInfo);
                        }
                    }
                }
            }

            // Update the current tree depth
            curDepth++;

            // Move the next level of nodes to the current node list
            nodeInfos.clear();
            if (nextNodeInfos != null) {
                nodeInfos.addAll(nextNodeInfos);
            }
        }
    }

    // Close the outer XML element
    xml.endElement(WebDAV.DAV_NS, WebDAV.XML_MULTI_STATUS, WebDAV.XML_NS_MULTI_STATUS);

    // Send remaining data
    flushXML(xml);
}

From source file:org.alfresco.repo.admin.patch.util.ImportFileUpdater.java

License:Open Source License

/**
 * Updates the passed import file into the equivalent 1.4 format.
 * //from  w  w  w. ja  v  a 2 s  .  c om
 * @param source      the source import file
 * @param destination   the destination import file
 */
public void updateImportFile(String source, String destination) {
    XmlPullParser reader = getReader(source);
    XMLWriter writer = getWriter(destination);
    this.shownWarning = false;

    try {
        // Start the documentation
        writer.startDocument();

        // Start reading the document
        int eventType = reader.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                ImportFileUpdater.this.outputCurrentElement(reader, writer, new OutputChildren());
            }
            eventType = reader.next();
        }

        // End and close the document
        writer.endDocument();
        writer.close();
    } catch (Exception exception) {
        throw new AlfrescoRuntimeException("Unable to update import file.", exception);
    }

}

From source file:org.alfresco.repo.webdav.PropFindMethod.java

License:Open Source License

/**
 * Execute the main WebDAV request processing
 * /*from  www.  j  a  v  a  2  s .  co  m*/
 * @exception WebDAVServerException
 */
protected void executeImpl() throws WebDAVServerException, Exception {
    m_response.setStatus(WebDAV.WEBDAV_SC_MULTI_STATUS);

    FileInfo pathNodeInfo = null;
    try {
        // Check that the path exists
        pathNodeInfo = getDAVHelper().getNodeForPath(getRootNodeRef(), m_strPath);
    } catch (FileNotFoundException e) {
        // The path is not valid - send a 404 error back to the client
        throw new WebDAVServerException(HttpServletResponse.SC_NOT_FOUND);
    }

    // A node hidden during a 'shuffle' operation - send a 404 error back to the client, as some Mac clients need this
    // Note the null check, as root node may be null in cloud.
    if (pathNodeInfo.getNodeRef() != null && getFileFolderService().isHidden(pathNodeInfo.getNodeRef())) {
        throw new WebDAVServerException(HttpServletResponse.SC_NOT_FOUND);
    }

    // Set the response content type

    m_response.setContentType(WebDAV.XML_CONTENT_TYPE);

    // Create multistatus response

    XMLWriter xml = createXMLWriter();

    xml.startDocument();

    String nsdec = generateNamespaceDeclarations(m_namespaces);
    xml.startElement(WebDAV.DAV_NS, WebDAV.XML_MULTI_STATUS + nsdec, WebDAV.XML_NS_MULTI_STATUS + nsdec,
            getDAVHelper().getNullAttributes());

    // Create the path for the current location in the tree
    StringBuilder baseBuild = new StringBuilder(256);
    baseBuild.append(getPath());
    if (baseBuild.length() == 0 || baseBuild.charAt(baseBuild.length() - 1) != WebDAVHelper.PathSeperatorChar) {
        baseBuild.append(WebDAVHelper.PathSeperatorChar);
    }
    String basePath = baseBuild.toString();

    // Output the response for the root node, depth zero
    generateResponseForNode(xml, pathNodeInfo, basePath);

    // If additional levels are required and the root node is a folder then recurse to the required
    // level and output node details a level at a time
    if (getDepth() != WebDAV.DEPTH_0 && pathNodeInfo.isFolder()) {
        // Create the initial list of nodes to report
        List<FileInfo> nodeInfos = new ArrayList<FileInfo>(10);
        nodeInfos.add(pathNodeInfo);

        int curDepth = WebDAV.DEPTH_1;

        // Save the base path length
        int baseLen = baseBuild.length();

        // List of next level of nodes to report
        List<FileInfo> nextNodeInfos = null;
        if (getDepth() > WebDAV.DEPTH_1) {
            nextNodeInfos = new ArrayList<FileInfo>(10);
        }

        // Loop reporting each level of nodes to the requested depth
        while (curDepth <= getDepth() && nodeInfos != null) {
            // Clear out the next level of nodes, if required
            if (nextNodeInfos != null) {
                nextNodeInfos.clear();
            }

            // Output the current level of node(s), the node list should
            // only contain folder nodes

            for (FileInfo curNodeInfo : nodeInfos) {
                // Get the list of child nodes for the current node
                List<FileInfo> childNodeInfos = getDAVHelper().getChildren(curNodeInfo);

                // can skip the current node if it doesn't have children
                if (childNodeInfos.size() == 0) {
                    continue;
                }

                // Output the child node details
                // Generate the base path for the current parent node

                baseBuild.setLength(baseLen);
                try {
                    String pathSnippet = null;
                    if ((pathNodeInfo.getNodeRef() == null) && (curNodeInfo.getNodeRef() == null)) {
                        // TODO review - note: can be null in case of Thor
                        pathSnippet = "/";
                    } else {
                        pathSnippet = getDAVHelper().getPathFromNode(pathNodeInfo.getNodeRef(),
                                curNodeInfo.getNodeRef());
                    }

                    baseBuild.append(pathSnippet);
                } catch (FileNotFoundException e) {
                    // move to the next node
                    continue;
                }

                int curBaseLen = baseBuild.length();

                // Output the child node details
                for (FileInfo curChildInfo : childNodeInfos) {
                    // Build the path for the current child node
                    baseBuild.setLength(curBaseLen);

                    baseBuild.append(curChildInfo.getName());

                    // Output the current child node details
                    generateResponseForNode(xml, curChildInfo, baseBuild.toString());

                    // If the child is a folder add it to the list of next level nodes
                    if (nextNodeInfos != null && curChildInfo.isFolder()) {
                        nextNodeInfos.add(curChildInfo);
                    }
                }
            }

            // Update the current tree depth
            curDepth++;

            // Move the next level of nodes to the current node list
            nodeInfos = nextNodeInfos;
        }
    }

    // Close the outer XML element
    xml.endElement(WebDAV.DAV_NS, WebDAV.XML_MULTI_STATUS, WebDAV.XML_NS_MULTI_STATUS);

    // Send remaining data
    flushXML(xml);
}

From source file:org.alfresco.repo.webdav.PropPatchMethod.java

License:Open Source License

@Override
protected void generateResponseImpl() throws Exception {
    m_response.setStatus(WebDAV.WEBDAV_SC_MULTI_STATUS);

    // Set the response content type
    m_response.setContentType(WebDAV.XML_CONTENT_TYPE);

    // Create multistatus response
    XMLWriter xml = createXMLWriter();

    xml.startDocument();

    String nsdec = generateNamespaceDeclarations(m_namespaces);
    xml.startElement(WebDAV.DAV_NS, WebDAV.XML_MULTI_STATUS + nsdec, WebDAV.XML_NS_MULTI_STATUS + nsdec,
            getDAVHelper().getNullAttributes());

    // Output the response block for the current node
    xml.startElement(WebDAV.DAV_NS, WebDAV.XML_RESPONSE, WebDAV.XML_NS_RESPONSE,
            getDAVHelper().getNullAttributes());

    xml.startElement(WebDAV.DAV_NS, WebDAV.XML_HREF, WebDAV.XML_NS_HREF, getDAVHelper().getNullAttributes());
    xml.write(strHRef);//from  ww  w. j  a  va 2  s.  c om
    xml.endElement(WebDAV.DAV_NS, WebDAV.XML_HREF, WebDAV.XML_NS_HREF);

    if (failedProperty != null) {
        generateError(xml);
    }

    for (PropertyAction propertyAction : m_propertyActions) {
        WebDAVProperty property = propertyAction.getProperty();
        int statusCode = propertyAction.getStatusCode();
        String statusCodeDescription = propertyAction.getStatusCodeDescription();
        generatePropertyResponse(xml, property, statusCode, statusCodeDescription);
    }

    // Close off the response element
    xml.endElement(WebDAV.DAV_NS, WebDAV.XML_RESPONSE, WebDAV.XML_NS_RESPONSE);

    // Close the outer XML element
    xml.endElement(WebDAV.DAV_NS, WebDAV.XML_MULTI_STATUS, WebDAV.XML_NS_MULTI_STATUS);

    // Send remaining data
    flushXML(xml);
}

From source file:org.itracker.web.util.ImportExportUtilities.java

License:Open Source License

public static void exportIssues(XMLWriter writer, List<Issue> issues, SystemConfiguration config)
        throws ImportExportException {
    Element elRoot = getDocumentFactory().createElement(TAG_ROOT);
    try {//w w  w .  j  av a 2s  .  com
        writer.startDocument();
        writer.writeOpen(elRoot);

        exportConfigModels(writer, config);
        exportIssuesModels(writer, issues);

        writer.writeClose(elRoot);
        writer.endDocument();
    } catch (SAXException e) {
        throw new ImportExportException(e.getMessage(), ImportExportException.TYPE_UNKNOWN);
    } catch (IOException e) {
        throw new ImportExportException("Problem writing export stream. " + e.getMessage(),
                ImportExportException.TYPE_UNKNOWN);
    }
}

From source file:org.neo4j.neoclipse.util.XMLUtils.java

License:Apache License

public static void save(Element pRoot, File pFile) {
    try {//from w  w  w. jav  a  2 s .com
        pFile.getParentFile().mkdirs();
        FileOutputStream fileOutputStream = new FileOutputStream(pFile);
        XMLWriter xmlWriter = new XMLWriter(fileOutputStream, OutputFormat.createPrettyPrint());
        xmlWriter.startDocument();
        xmlWriter.write(pRoot);
        xmlWriter.endDocument();
        xmlWriter.flush();
        xmlWriter.close();
    } catch (Exception e) {
        ErrorMessage.showDialog("Couldn't save: " + pFile.getAbsolutePath(), e);
    }

}

From source file:org.safehaus.penrose.config.PenroseConfigWriter.java

License:Open Source License

public void write(File file, PenroseConfig penroseConfig) throws Exception {

    Element element = createElement(penroseConfig);

    file.getParentFile().mkdirs();//from   ww  w .  j  a  v a 2s  .c  o  m
    Writer writer = new FileWriter(file);

    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setTrimText(false);

    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.startDocument();

    xmlWriter.startDTD("server", "-//Penrose/DTD Server " + Penrose.SPECIFICATION_VERSION + "//EN",
            "http://penrose.safehaus.org/dtd/server.dtd");

    xmlWriter.write(element);
    xmlWriter.close();

    writer.close();
}

From source file:org.safehaus.penrose.federation.FederationWriter.java

License:Open Source License

public void write(File file, FederationConfig federationConfig) throws Exception {

    log.debug("Writing " + file + ".");

    Element element = createElement(federationConfig);

    FileWriter fw = new FileWriter(file);
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setTrimText(false);// www. j a  va 2s .c  om

    XMLWriter writer = new XMLWriter(fw, format);
    writer.startDocument();

    writer.startDTD("federation",
            "-//Penrose/DTD Federation " + getClass().getPackage().getSpecificationVersion() + "//EN",
            "http://penrose.safehaus.org/dtd/federation.dtd");

    writer.write(element);
    writer.close();
}

From source file:org.safehaus.penrose.log.log4j.Log4jConfigWriter.java

License:Open Source License

public void write(File file, Log4jConfig config) throws Exception {

    Element element = createConfigElement(config);

    Writer out;//from  w  w w . j  a v a  2 s.  com
    if (file == null) {
        out = new PrintWriter(System.out, true);
    } else {
        file.getParentFile().mkdirs();
        out = new FileWriter(file);
    }

    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setTrimText(false);

    XMLWriter writer = new XMLWriter(out, format);
    writer.startDocument();

    writer.startDTD("log4j:configuration", "-//Apache//DTD Log4j 1.2//EN",
            "http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd");

    writer.write(element);
    writer.close();

    out.close();
}

From source file:org.safehaus.penrose.partition.PartitionWriter.java

License:Open Source License

public void writePartitionXml(File directory, PartitionConfig partitionConfig) throws Exception {
    File file = new File(directory, "partition.xml");

    log.debug("Writing " + file + ".");

    Element element = createElement(partitionConfig);

    FileWriter fw = new FileWriter(file);
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setTrimText(false);//from www  .  j  a va2  s.c o m

    XMLWriter writer = new XMLWriter(fw, format);
    writer.startDocument();

    writer.startDTD("partition",
            "-//Penrose/DTD Partition " + getClass().getPackage().getSpecificationVersion() + "//EN",
            "http://penrose.safehaus.org/dtd/partition.dtd");

    writer.write(element);
    writer.close();
}