Search in sources :

Example 51 with OutputFormat

use of org.dom4j.io.OutputFormat in project sagacity-sqltoy by chenrenfei.

the class XMLUtil method updateXML.

/**
 * @todo 编辑xml文件
 * @param xmlFile
 * @param charset
 * @param isValidator
 * @param handler
 * @return
 * @throws Exception
 */
public static boolean updateXML(File xmlFile, String charset, boolean isValidator, XMLCallbackHandler handler) throws Exception {
    if (handler == null || xmlFile == null || !xmlFile.exists())
        return false;
    InputStream is = null;
    FileOutputStream fos = null;
    try {
        SAXReader saxReader = new SAXReader();
        if (!isValidator)
            saxReader.setFeature(NO_VALIDATOR_FEATURE, false);
        if (charset != null)
            saxReader.setEncoding(charset);
        is = new FileInputStream(xmlFile);
        if (null != is) {
            org.dom4j.Document doc = saxReader.read(is);
            if (null != doc) {
                handler.process(doc, doc.getRootElement());
                OutputFormat format = OutputFormat.createPrettyPrint();
                if (charset != null)
                    format.setEncoding(charset);
                fos = new FileOutputStream(xmlFile);
                XMLWriter output = new XMLWriter(fos, format);
                output.write(doc);
            }
            is.close();
        }
    } catch (Exception e) {
        logger.error("修改XML文件:{}错误:{}!", xmlFile, e.getMessage());
        throw e;
    } finally {
        if (fos != null)
            fos.close();
        if (is != null)
            is.close();
    }
    return true;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 52 with OutputFormat

use of org.dom4j.io.OutputFormat in project tmdm-studio-se by Talend.

the class Util method formatXsdSource.

public static String formatXsdSource(String xsdSource, boolean suppressDeclaration) {
    try {
        SAXReader reader = new SAXReader();
        org.dom4j.Document document = reader.read(new StringReader(xsdSource));
        StringWriter writer = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        // $NON-NLS-1$
        format.setEncoding("UTF-8");
        format.setSuppressDeclaration(suppressDeclaration);
        XMLWriter xmlwriter = new XMLWriter(writer, format);
        xmlwriter.write(document);
        String str = writer.toString();
        writer.close();
        xmlwriter.close();
        return str;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return xsdSource;
}
Also used : StringWriter(java.io.StringWriter) SAXReader(org.dom4j.io.SAXReader) StringReader(java.io.StringReader) OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter) ConnectException(java.net.ConnectException) IOException(java.io.IOException) WebServiceException(javax.xml.ws.WebServiceException) MissingJarsException(com.amalto.workbench.service.MissingJarsException) MalformedURLException(java.net.MalformedURLException) SAXParseException(org.xml.sax.SAXParseException) InaccessibleWSDLException(com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException)

Example 53 with OutputFormat

use of org.dom4j.io.OutputFormat in project OpenOLAT by OpenOLAT.

the class QTIExportProcessor method assembleTest.

/**
 * <li>List all items
 * <li>Rewrite path
 * <li>Assemble qti.xml
 * <li>Write files at new path
 * @param fullItems
 * @param zout
 */
public void assembleTest(List<QuestionItemFull> fullItems, ZipOutputStream zout) {
    ItemsAndMaterials itemAndMaterials = new ItemsAndMaterials();
    for (QuestionItemFull fullItem : fullItems) {
        collectMaterials(fullItem, itemAndMaterials);
    }
    try {
        byte[] buffer = new byte[FileUtils.BSIZE];
        // write qti.xml
        Element sectionEl = createSectionBasedAssessment("Assessment");
        for (Element itemEl : itemAndMaterials.getItemEls()) {
            // generate new ident per item
            String ident = getAttributeValue(itemEl, "ident");
            String exportIdent = QTIEditHelper.generateNewIdent(ident);
            itemEl.addAttribute("ident", exportIdent);
            sectionEl.add(itemEl);
        }
        zout.putNextEntry(new ZipEntry("qti.xml"));
        XMLWriter xw = new XMLWriter(zout, new OutputFormat("  ", true));
        xw.write(sectionEl.getDocument());
        zout.closeEntry();
        // write materials
        for (ItemMaterial material : itemAndMaterials.getMaterials()) {
            String exportPath = material.getExportUri();
            zout.putNextEntry(new ZipEntry(exportPath));
            InputStream in = material.getLeaf().getInputStream();
            int c;
            while ((c = in.read(buffer, 0, buffer.length)) != -1) {
                zout.write(buffer, 0, c);
            }
            IOUtils.closeQuietly(in);
            zout.closeEntry();
        }
    } catch (IOException e) {
        log.error("", e);
    }
}
Also used : QuestionItemFull(org.olat.modules.qpool.QuestionItemFull) InputStream(java.io.InputStream) Element(org.dom4j.Element) ZipEntry(java.util.zip.ZipEntry) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) XMLWriter(org.dom4j.io.XMLWriter)

Example 54 with OutputFormat

use of org.dom4j.io.OutputFormat in project OpenOLAT by OpenOLAT.

the class FilePersister method createResultsReporting.

/**
 * Persist results for this user/aiid as an XML document. dlPointer is aiid in
 * this case.
 *
 * @param doc
 * @param type
 * @param info
 */
public static void createResultsReporting(Document doc, Identity subj, String type, long aiid) {
    File fUserdataRoot = new File(WebappHelper.getUserDataRoot());
    String path = RES_REPORTING + File.separator + subj.getName() + File.separator + type;
    File fReportingDir = new File(fUserdataRoot, path);
    try {
        fReportingDir.mkdirs();
        OutputStream os = new FileOutputStream(new File(fReportingDir, aiid + ".xml"));
        Element element = doc.getRootElement();
        XMLWriter xw = new XMLWriter(os, new OutputFormat("  ", true));
        xw.write(element);
        // closing steams
        xw.close();
        os.close();
    } catch (Exception e) {
        throw new OLATRuntimeException(FilePersister.class, "Error persisting results reporting for subject: '" + subj.getName() + "'; assessment id: '" + aiid + "'", e);
    }
}
Also used : OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) FileOutputStream(java.io.FileOutputStream) Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) File(java.io.File) XMLWriter(org.dom4j.io.XMLWriter) IOException(java.io.IOException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException)

Example 55 with OutputFormat

use of org.dom4j.io.OutputFormat in project xwiki-platform by xwiki.

the class XWikiDocument method toXMLDocument.

/**
 * Serialize the document to an XML {@link DOMDocument}. You should prefer
 * {@link #toXML(OutputStream, boolean, boolean, boolean, boolean, XWikiContext)} or
 * {@link #toXML(com.xpn.xwiki.internal.xml.XMLWriter, boolean, boolean, boolean, boolean, XWikiContext)} when
 * possible to reduce memory load.
 *
 * @param bWithObjects include XObjects
 * @param bWithRendering include the rendered content
 * @param bWithAttachmentContent include attachments content
 * @param bWithVersions include archived versions
 * @param context current XWikiContext
 * @return a {@link DOMDocument} containing the serialized document.
 * @throws XWikiException when an errors occurs during wiki operations
 * @deprecated since 9.0RC1, use {@link #toXML(OutputTarget, boolean, boolean, boolean, boolean, boolean, String)}
 *             instead
 */
@Deprecated
public Document toXMLDocument(boolean bWithObjects, boolean bWithRendering, boolean bWithAttachmentContent, boolean bWithVersions, XWikiContext context) throws XWikiException {
    Document doc = new DOMDocument();
    DOMXMLWriter wr = new DOMXMLWriter(doc, new OutputFormat("", true, context.getWiki().getEncoding()));
    try {
        toXML(wr, bWithObjects, bWithRendering, bWithAttachmentContent, bWithVersions, context);
        return doc;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : DOMDocument(org.dom4j.dom.DOMDocument) OutputFormat(org.dom4j.io.OutputFormat) DOMXMLWriter(com.xpn.xwiki.internal.xml.DOMXMLWriter) IOException(java.io.IOException) Document(org.dom4j.Document) DOMDocument(org.dom4j.dom.DOMDocument)

Aggregations

OutputFormat (org.dom4j.io.OutputFormat)89 XMLWriter (org.dom4j.io.XMLWriter)75 IOException (java.io.IOException)39 Document (org.dom4j.Document)31 Element (org.dom4j.Element)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 StringWriter (java.io.StringWriter)18 SAXReader (org.dom4j.io.SAXReader)16 File (java.io.File)15 FileOutputStream (java.io.FileOutputStream)14 FileWriter (java.io.FileWriter)10 OutputStreamWriter (java.io.OutputStreamWriter)8 StringReader (java.io.StringReader)6 DocumentException (org.dom4j.DocumentException)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4