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

The following examples show how to use org.dom4j.io.XMLWriter#flush() . 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 File: QTIExportImportTest.java    From olat with 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 File: QTIExportImportTest.java    From olat with 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 File: CurriculumClassification.java    From unitime with Apache License 2.0 5 votes vote down vote up
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 4
Source File: StudentSectioningQueue.java    From unitime with 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 5
Source File: XmlBlobType.java    From unitime with 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 6
Source File: XmlExport.java    From openmeetings with 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 7
Source File: Dom4jManyToOneTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 8
Source File: XMLHelper.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 9
Source File: XmlUtil.java    From SpringBootUnity with 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 10
Source File: XmlUtil.java    From SpringBootUnity with 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 11
Source File: TestTool.java    From ofdrw with 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 12
Source File: GMLMapFormat.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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 13
Source File: ScenarioEditor.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
   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 14
Source File: WindowsServiceMojo.java    From joylau-springboot-daemon-windows with MIT License 5 votes vote down vote up
/**
 * 保存 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 15
Source File: WebDAVMethod.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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 16
Source File: XmlSchemaHelper.java    From syndesis with 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 17
Source File: SolverInfo.java    From unitime with 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 18
Source File: XmlUtils.java    From studio with 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 19
Source File: SearchProxy.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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 File: XmlUtil.java    From mybatis-daoj with 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();
}