Search in sources :

Example 1 with DOMContentBuilderImpl

use of org.eclipse.wst.xml.core.internal.contentmodel.util.DOMContentBuilderImpl in project webtools.sourceediting by eclipse.

the class NewXMLGenerator method createXMLDocument.

public ByteArrayOutputStream createXMLDocument(String xmlFileName, String charset) throws Exception {
    if (charset == null) {
        charset = getUserPreferredCharset();
        if (charset == null) {
            // $NON-NLS-1$
            charset = "UTF-8";
        }
    }
    CMDocument cmDocument = getCMDocument();
    Assert.isNotNull(cmDocument);
    Assert.isNotNull(getRootElementName());
    // create the xml model
    CMNamedNodeMap nameNodeMap = cmDocument.getElements();
    CMElementDeclaration cmElementDeclaration = (CMElementDeclaration) nameNodeMap.getNamedItem(getRootElementName());
    Document xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    DOMContentBuilderImpl contentBuilder = new DOMContentBuilderImpl(xmlDocument);
    // this 'uglyTempHack' flag is required in order to supress the
    // creation a default encoding
    // we'll handle this later in the domWriter.print() method used below
    // 
    contentBuilder.supressCreationOfDoctypeAndXMLDeclaration = true;
    contentBuilder.setBuildPolicy(buildPolicy);
    contentBuilder.setOptionalElementDepthLimit(optionalElementDepthLimit);
    contentBuilder.setExternalCMDocumentSupport(new MyExternalCMDocumentSupport(namespaceInfoList, xmlFileName));
    contentBuilder.createDefaultRootContent(cmDocument, cmElementDeclaration, namespaceInfoList);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, charset);
    DOMWriter domWriter = new DOMWriter(outputStreamWriter);
    // TODO... instead of relying on file extensions, we need to keep
    // track of the grammar type
    // better yet we should reate an SSE document so that we can format it
    // nicely before saving
    // then we won't need the DOMWriter at all
    // 
    domWriter.print(xmlDocument, charset, cmDocument.getNodeName(), getNonWhitespaceString(getPublicId()), getNonWhitespaceString(getSystemId()));
    outputStream.flush();
    outputStream.close();
    return outputStream;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) DOMContentBuilderImpl(org.eclipse.wst.xml.core.internal.contentmodel.util.DOMContentBuilderImpl) DOMWriter(org.eclipse.wst.xml.core.internal.contentmodel.util.DOMWriter) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) Document(org.w3c.dom.Document) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)1 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)1 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)1 DOMContentBuilderImpl (org.eclipse.wst.xml.core.internal.contentmodel.util.DOMContentBuilderImpl)1 DOMWriter (org.eclipse.wst.xml.core.internal.contentmodel.util.DOMWriter)1 Document (org.w3c.dom.Document)1