Java Code Examples for org.dom4j.io.XMLWriter#flush()

The following examples show how to use org.dom4j.io.XMLWriter#flush() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: olat   File: QTIExportImportTest.java    License: Apache License 2.0 6 votes vote down vote up
private static QTIDocument exportAndImportToQTIFormat(QTIDocument qtiDocOrig) throws IOException {
    Document qtiXmlDoc = qtiDocOrig.getDocument();
    OutputFormat outformat = OutputFormat.createPrettyPrint();

    String fileName = qtiDocOrig.getAssessment().getTitle() + "QTIFormat.xml";
    OutputStreamWriter qtiXmlOutput = new OutputStreamWriter(new FileOutputStream(new File(TEMP_DIR, fileName)), Charset.forName("UTF-8"));
    XMLWriter writer = new XMLWriter(qtiXmlOutput, outformat);
    writer.write(qtiXmlDoc);
    writer.flush();
    writer.close();

    XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
    Document doc = xmlParser.parse(new FileInputStream(new File(TEMP_DIR, fileName)), true);
    ParserManager parser = new ParserManager();
    QTIDocument qtiDocRestored = (QTIDocument) parser.parse(doc);
    return qtiDocRestored;
}
 
Example 2
Source Project: olat   File: QTIExportImportTest.java    License: Apache License 2.0 6 votes vote down vote up
private static QTIDocument exportAndImportToQTIFormat(QTIDocument qtiDocOrig) throws IOException {
    Document qtiXmlDoc = qtiDocOrig.getDocument();
    OutputFormat outformat = OutputFormat.createPrettyPrint();

    String fileName = qtiDocOrig.getAssessment().getTitle() + "QTIFormat.xml";
    OutputStreamWriter qtiXmlOutput = new OutputStreamWriter(new FileOutputStream(new File(TEMP_DIR, fileName)), Charset.forName("UTF-8"));
    XMLWriter writer = new XMLWriter(qtiXmlOutput, outformat);
    writer.write(qtiXmlDoc);
    writer.flush();
    writer.close();

    XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
    Document doc = xmlParser.parse(new FileInputStream(new File(TEMP_DIR, fileName)), true);
    ParserManager parser = new ParserManager();
    QTIDocument qtiDocRestored = (QTIDocument) parser.parse(doc);
    return qtiDocRestored;
}
 
Example 3
Source Project: ofdrw   File: TestTool.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取对应元素的XML文本文件的字节内容
 *
 * @param element 元素
 * @return 字节内容
 */
public static byte[] xmlByte(Element element) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        XMLWriter writerToSout = new XMLWriter(out, FORMAT);
        writerToSout.write(element);
        writerToSout.flush();
        return out.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 4
Source Project: studio   File: XmlUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * convert document to string
 * 
 * @param document
 * @return XML as String
 * @throws java.io.IOException
 */
public static String convertDocumentToString(Document document) throws IOException {
	StringWriter sw = new StringWriter();
	XMLWriter writer = new XMLWriter(sw);
	try {
		writer.write(document);
		writer.flush();
		return sw.toString();
	} finally {
		sw.close();
		writer.close();
	}
}
 
Example 5
Source Project: unitime   File: SolverInfo.java    License: Apache License 2.0 5 votes vote down vote up
public void setValue(Document document) {
	try {
		if (document == null) {
			setData(null);
		} else {
			 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
             XMLWriter writer = new XMLWriter(new GZIPOutputStream(bytes),OutputFormat.createCompactFormat());
             writer.write(document);
             writer.flush(); writer.close();
             setData(bytes.toByteArray());
		}
	} catch (IOException e) {
		throw new HibernateException(e.getMessage(),e);
	}
}
 
Example 6
Source Project: syndesis   File: XmlSchemaHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static String serialize(final Document document) {
    try (StringWriter out = new StringWriter()) {
        final OutputFormat format = new OutputFormat(null, false, "UTF-8");
        format.setExpandEmptyElements(false);
        format.setIndent(false);

        final XMLWriter writer = new XMLWriter(out, format);
        writer.write(document);
        writer.flush();

        return out.toString();
    } catch (final IOException e) {
        throw new IllegalStateException("Unable to serialize given document to XML", e);
    }
}
 
Example 7
/**
 * Flushes all XML written so far to the response
 * 
 * @param writer XMLWriter that should be flushed
 */
protected final void flushXML(XMLWriter writer) throws IOException
{
    if (shouldFlushXMLWriter())
    {
        writer.flush();
    }
    
    m_response.getWriter().write(m_xmlWriter.toCharArray());
    
    m_xmlWriter.reset();
}
 
Example 8
/**
 * 保存 XML 文件
 * @param document 文档
 * @param xmlFile xml文件
 */
private void saveXML(Document document, File xmlFile){
    try {
        XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(xmlFile), StandardCharsets.UTF_8));
        writer.write(document);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 9
/**
   Save the scenario.
   @throws ScenarioException If there is a problem saving the scenario.
*/
public void save() throws ScenarioException {
    if (saveFile == null) {
        saveAs();
    }
    if (saveFile != null) {
        LOG.debug("Saving to " + saveFile.getAbsolutePath());
        Document doc = DocumentHelper.createDocument();
        scenario.write(doc);
        try {
            if (!saveFile.exists()) {
                File parent = saveFile.getParentFile();
                if (!parent.exists()) {
                    if (!saveFile.getParentFile().mkdirs()) {
                        throw new ScenarioException("Couldn't create file " + saveFile.getPath());
                    }
                }
                if (!saveFile.createNewFile()) {
                    throw new ScenarioException("Couldn't create file " + saveFile.getPath());
                }
            }
            XMLWriter writer = new XMLWriter(new FileOutputStream(saveFile), OutputFormat.createPrettyPrint());
            writer.write(doc);
            writer.flush();
            writer.close();
        }
        catch (IOException e) {
            throw new ScenarioException(e);
        }
        baseDir = saveFile.getParentFile();
        changed = false;
    }
}
 
Example 10
@Override
public void write(Map map, File file) throws MapException {
    if (map == null) {
        throw new IllegalArgumentException("Map must not be null");
    }
    if (file == null) {
        throw new IllegalArgumentException("File must not be null");
    }
    if (!(map instanceof GMLMap)) {
        throw new IllegalArgumentException("Map is not a GMLMap: " + map.getClass().getName());
    }
    Document doc = write((GMLMap)map);
    try {
        if (!file.exists()) {
            File parent = file.getParentFile();
            if (!parent.exists()) {
                if (!file.getParentFile().mkdirs()) {
                    throw new MapException("Couldn't create file " + file.getPath());
                }
            }
            if (!file.createNewFile()) {
                throw new MapException("Couldn't create file " + file.getPath());
            }
        }
        XMLWriter writer = new XMLWriter(new FileOutputStream(file), OutputFormat.createPrettyPrint());
        Element root = doc.getRootElement();
        for (java.util.Map.Entry<String, String> next : getNamespaces().entrySet()) {
            root.addNamespace(next.getKey(), next.getValue());
        }
        writer.write(doc);
        writer.flush();
        writer.close();
    }
    catch (IOException e) {
        throw new MapException(e);
    }
}
 
Example 11
public void setStudentsDocument(Document document) {
	try {
		if (document == null) {
			setStudents(null);
		} else {
			StringWriter string = new StringWriter();
			XMLWriter writer = new XMLWriter(string, OutputFormat.createCompactFormat());
			writer.write(document);
			writer.flush(); writer.close();
			setStudents(string.toString());
		}
	} catch (Exception e) {
		sLog.warn("Failed to store cached students for " + getCurriculum().getAbbv() + " " + getName() + ": " + e.getMessage(), e);
	}
}
 
Example 12
Source Project: SpringBootUnity   File: XmlUtil.java    License: MIT License 5 votes vote down vote up
/**
 * 保存文档
 *
 * @throws Exception
 */
public static void save(Document doc, String xmlPath, String encoding) throws Exception {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(encoding);
    XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(xmlPath), encoding), format);
    writer.write(doc);
    writer.flush();
    writer.close();
}
 
Example 13
Source Project: SpringBootUnity   File: XmlUtil.java    License: MIT License 5 votes vote down vote up
/**
 * xml转换为字符串
 *
 * @param doc
 * @param encoding
 * @return
 * @throws Exception
 */
public static String toString(Document doc, String encoding) throws Exception {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(encoding);
    ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
    XMLWriter writer = new XMLWriter(new OutputStreamWriter(byteOS, encoding), format);
    writer.write(doc);
    writer.flush();
    writer.close();
    return byteOS.toString(encoding);
}
 
Example 14
public static void dump(Element element) {
	try {
		// try to "pretty print" it
		OutputFormat outformat = OutputFormat.createPrettyPrint();
		XMLWriter writer = new XMLWriter( System.out, outformat );
		writer.write( element );
		writer.flush();
		System.out.println( "" );
	}
	catch( Throwable t ) {
		// otherwise, just dump it
		System.out.println( element.asXML() );
	}

}
 
Example 15
public static void print(Element elt) throws Exception {
	OutputFormat outformat = OutputFormat.createPrettyPrint();
	// outformat.setEncoding(aEncodingScheme);
	XMLWriter writer = new XMLWriter( System.out, outformat );
	writer.write( elt );
	writer.flush();
	// System.out.println( elt.asXML() );
}
 
Example 16
Source Project: openmeetings   File: XmlExport.java    License: Apache License 2.0 5 votes vote down vote up
public static void toXml(Writer out, Document doc) throws IOException {
	OutputFormat outformat = OutputFormat.createPrettyPrint();
	outformat.setIndentSize(1);
	outformat.setIndent("\t");
	outformat.setEncoding(UTF_8.name());
	XMLWriter writer = new XMLWriter(out, outformat);
	writer.write(doc);
	writer.flush();
	out.flush();
	out.close();
}
 
Example 17
Source Project: unitime   File: XmlBlobType.java    License: Apache License 2.0 5 votes vote down vote up
public void nullSafeSet(PreparedStatement ps, Object value, int index, SessionImplementor session) throws SQLException, HibernateException {
    if (value == null) {
        ps.setNull(index, sqlTypes()[0]);
    } else {
        try {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            XMLWriter writer = new XMLWriter(new GZIPOutputStream(bytes),OutputFormat.createCompactFormat());
            writer.write((Document)value);
            writer.flush(); writer.close();
            ps.setBinaryStream(index, new ByteArrayInputStream(bytes.toByteArray(),0,bytes.size()), bytes.size());
        } catch (IOException e) {
            throw new HibernateException(e.getMessage(),e);
        }
    }
}
 
Example 18
Source Project: unitime   File: StudentSectioningQueue.java    License: Apache License 2.0 5 votes vote down vote up
public void setMessage(Document document) {
	try {
		if (document == null) {
			setData(null);
		} else {
			StringWriter string = new StringWriter();
			XMLWriter writer = new XMLWriter(string, OutputFormat.createCompactFormat());
			writer.write(document);
			writer.flush(); writer.close();
			setData(string.toString());
		}
	} catch (IOException e) {
		throw new HibernateException(e.getMessage(),e);
	}
}
 
Example 19
@Override
protected void writeResponse(InputStream input, OutputStream output)
    throws IOException
{
    if (response.getContentType().startsWith(MimetypeMap.MIMETYPE_ATOM) ||
        response.getContentType().startsWith(MimetypeMap.MIMETYPE_RSS))
    {
        // Only post-process ATOM and RSS feeds
        // Replace all navigation links with "proxied" versions
        SAXReader reader = new SAXReader();
        try
        {
            Document document = reader.read(input);
            Element rootElement = document.getRootElement();

            XPath xpath = rootElement.createXPath(ATOM_LINK_XPATH);
            Map<String,String> uris = new HashMap<String,String>();
            uris.put(ATOM_NS_PREFIX, ATOM_NS_URI);
            xpath.setNamespaceURIs(uris);

            List nodes = xpath.selectNodes(rootElement);
            Iterator iter = nodes.iterator();
            while (iter.hasNext())
            {
                Element element = (Element)iter.next();
                Attribute hrefAttr = element.attribute("href");
                String mimetype = element.attributeValue("type");
                if (mimetype == null || mimetype.length() == 0)
                {
                    mimetype = MimetypeMap.MIMETYPE_HTML;
                }
                String url = createUrl(engine, hrefAttr.getValue(), mimetype);
                if (url.startsWith("/"))
                {
                    url = rootPath + url;
                }
                hrefAttr.setValue(url);
            }
            
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(output, outputFormat);
            writer.write(rootElement);
            writer.flush();                
        }
        catch(DocumentException e)
        {
            throw new IOException(e.toString());
        }
    }
    else
    {
        super.writeResponse(input, output);
    }
}
 
Example 20
Source Project: mybatis-daoj   File: XmlUtil.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 将XML文档写入指定的文件.
 *
 * @param document XML文档
 * @param fileName 文件名
 * @param format   输出格式, 参考常量XmlUtil.FORMAT_GBK_PRETTY,
 *                 XmlUtil.FORMAT_UTF_PRETTY等
 * @throws java.io.IOException
 */
public static void write(Document document, String fileName, OutputFormat format) throws IOException {

    XMLWriter writer = new XMLWriter(new FileWriter(fileName), format);
    writer.write(document);
    writer.flush();
    writer.close();
}