Java Code Examples for org.apache.xml.serialize.OutputFormat#setLineWidth()

The following examples show how to use org.apache.xml.serialize.OutputFormat#setLineWidth() . 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: AtsProjectConfiguration.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
public void save() throws AtsConfigurationException {

        // save the XML file
        try {
            OutputFormat format = new OutputFormat(doc);
            format.setIndenting(true);
            format.setIndent(4);
            format.setLineWidth(1000);

            XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(atsConfigurationFile)),
                                                         format);
            serializer.serialize(doc);
        } catch (Exception e) {
            throw new AtsConfigurationException("Error saving ATS configuration in '" + atsConfigurationFile
                                                + "'", e);
        }
    }
 
Example 2
Source File: XMLUtils.java    From jdal with Apache License 2.0 6 votes vote down vote up
/**
 * Serialize a Document to a String using JAXP with identation (4 spaces) 
 * @param doc to serialize
 * @return xml string
 */
public static String prettyDocumentToString(Document doc) {
	StringWriter writer = new StringWriter();
	OutputFormat out = new OutputFormat();
	out.setOmitXMLDeclaration(true);
	out.setIndenting(true);
	out.setIndent(4);
	out.setLineSeparator(System.getProperty("line.separator"));
	out.setLineWidth(Integer.MAX_VALUE);
	
	XMLSerializer serializer = new XMLSerializer(writer, out);
	try {
		Element rootElement = doc.getDocumentElement();
		serializer.serialize(rootElement);
	} catch (IOException e) { 
		log.error(e);
	}
	return writer.toString();
}
 
Example 3
Source File: PolicyEditorService.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Formats a given unformatted XML string
 *
 * @param xml
 * @return A CDATA wrapped, formatted XML String
 */
public String formatXML(String xml) {

    try {
        DocumentBuilder docBuilder;
        Document xmlDoc;

        // create the factory
        DocumentBuilderFactory docFactory = IdentityUtil.getSecuredDocumentBuilderFactory();
        docFactory.setIgnoringComments(true);

        // now use the factory to create the document builder
        docBuilder = docFactory.newDocumentBuilder();
        xmlDoc = docBuilder.parse(new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8)));


        OutputFormat format = new OutputFormat(xmlDoc);
        format.setLineWidth(0);
        format.setIndenting(true);
        format.setIndent(2);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLSerializer serializer = new XMLSerializer(baos, format);
        serializer.serialize(xmlDoc);

        xml = baos.toString("UTF-8");

    } catch (ParserConfigurationException pce) {
        throw new IllegalArgumentException("Failed to parse the unformatted XML String. ", pce);
    } catch (Exception e) {
        log.error("Error occured while formtting the unformatted XML String. ", e);
    }

    return "<![CDATA[" + xml + "]]>";
}
 
Example 4
Source File: XMLHelpers.java    From SAMLRaider with MIT License 5 votes vote down vote up
public String getString(Document document, boolean indenting, int indent) throws IOException{
	OutputFormat format = new OutputFormat(document);
	format.setLineWidth(200);
	format.setIndenting(indenting);
	format.setIndent(indent);
	format.setPreserveEmptyAttributes(true);
	format.setEncoding("UTF-8");
	
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	XMLSerializer serializer = new XMLSerializer(baos, format);
	serializer.asDOMSerializer();
	serializer.serialize(document);

	return baos.toString("UTF-8");
}
 
Example 5
Source File: DocumentUtils.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a String representation for the XML Document.
 *
 * @param xmlDocument the XML Document
 * @return String representation for the XML Document
 * @throws IOException
 */
public static String documentToString(Document xmlDocument) throws IOException {
    String encoding = (xmlDocument.getXmlEncoding() == null) ? "UTF-8" : xmlDocument.getXmlEncoding();
    OutputFormat format = new OutputFormat(xmlDocument);
    format.setLineWidth(65);
    format.setIndenting(true);
    format.setIndent(2);
    format.setEncoding(encoding);
    try (Writer out = new StringWriter()) {
        XMLSerializer serializer = new XMLSerializer(out, format);
        serializer.serialize(xmlDocument);
        return out.toString();
    }
}
 
Example 6
Source File: XMLUtil.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public static String toXML(XMLObject xmlObject, boolean inlineEntity, boolean indent, XMLSerializer serializer) {
	Document document = xmlObject.toDocument(serializer);

	if (document == null)
		return "";

	OutputFormat format = new OutputFormat(document);

	/**
	 * PROBLEM WITH THE WRONG ENCODING SET TO THE XML STRING REPRESENTATION OF THE CHART TEMPLATE INSIDE THE xmlObject INPUT PARAMETER. FOR THIS
	 * APPLICATION, IT IS ALWAYS SET TO UTF-8, EVEN IF WE APPLY THE XML TEMPLATE WITH SOME DIFFERENT ENCODING (e.g. ISO-8859-1).
	 * 
	 * This line (encoding of the format) is commented due to the problem stated in the KNOWAGE-775 JIRA issue (title
	 * "COCKPIT-Chart: letters with accent are not correctly decoded").
	 * 
	 * It is found that the XML template of a chart has the UTF-8 encoding set in its header, no matter what is the encoding set on saving of the chart
	 * document (or uploading of the template with some different encoding, e.g. ISO-8859-1). It is not clear why this happens.
	 * 
	 * THE WORKAROUND: the encoding setting of the format will be skipped in order to avoid misbehavior and wrong encoding of the XML string representation
	 * (inside the xmlObject input parameter). This way we are keeping all the details provided in the template, such as specific Italian letters (such as
	 * à, è, ò etc.). Otherwise, this line would encode that XML string with the default encoding type (XML_HEADER_DEFAULT_ENCODING), that is ISO-8859-1. If
	 * we let this happen, we would lose mentioned specific letters and we will start getting weird ones on the chart (such as è).
	 * 
	 * @author Danilo Ristovski (danristo, [email protected])
	 */
	// format.setEncoding(XML_HEADER_DEFAULT_ENCODING);

	format.setIndenting(indent);
	format.setIndent(indent ? 2 : 0);
	format.setLineWidth(0);
	format.setLineSeparator(ConfigSingleton.LINE_SEPARATOR);
	format.setPreserveEmptyAttributes(true);
	StringWriter stringOut = new StringWriter();
	org.apache.xml.serialize.XMLSerializer serial = new org.apache.xml.serialize.XMLSerializer(stringOut, format);
	try {
		serial.asDOMSerializer();
		if (inlineEntity)
			serial.serialize(document);
		else
			serial.serialize(document.getDocumentElement());
	} // try
	catch (IOException ex) {
		TracerSingleton.log(Constants.NOME_MODULO, TracerSingleton.CRITICAL, "XMLUtil::toXML: ", ex);
	} // catch (IOException ex)
	return stringOut.toString();
}
 
Example 7
Source File: PolicyEditorService.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Formats a given unformatted XML string
 *
 * @param xml
 * @return A CDATA wrapped, formatted XML String
 */
public String formatXML(String xml) {

    try {
        // create the factory
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setIgnoringComments(true);
        docFactory.setNamespaceAware(true);
        docFactory.setExpandEntityReferences(false);
        SecurityManager securityManager = new SecurityManager();
        securityManager.setEntityExpansionLimit(ENTITY_EXPANSION_LIMIT);
        docFactory.setAttribute(SECURITY_MANAGER_PROPERTY, securityManager);
        DocumentBuilder docBuilder;
        Document xmlDoc;

        // now use the factory to create the document builder
        docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        docFactory.setFeature(EXTERNAL_GENERAL_ENTITIES_URI, false);
        docBuilder = docFactory.newDocumentBuilder();
        docBuilder.setEntityResolver(new CarbonEntityResolver());
        xmlDoc = docBuilder.parse(new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8)));


        OutputFormat format = new OutputFormat(xmlDoc);
        format.setLineWidth(0);
        format.setIndenting(true);
        format.setIndent(2);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLSerializer serializer = new XMLSerializer(baos, format);
        serializer.serialize(xmlDoc);

        xml = baos.toString("UTF-8");

    } catch (ParserConfigurationException pce) {
        throw new IllegalArgumentException("Failed to parse the unformatted XML String. ", pce);
    } catch (Exception e) {
        log.error("Error occured while formtting the unformatted XML String. ", e);
    }

    return "<![CDATA[" + xml + "]]>";
}