Java Code Examples for org.jdom.output.XMLOutputter#setFormat()

The following examples show how to use org.jdom.output.XMLOutputter#setFormat() . 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: OvalFileAggregator.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Finalizes processing and builds the aggregated document
 * @param prettyPrint pretty print XML or not
 * @return XML in string form
 * @throws IOException document output failed
 */
public String finish(boolean prettyPrint) throws IOException {
    if (!isFinished && !isEmpty()) {
        buildDocument();
        isFinished = true;
    }
    if (isEmpty()) {
        return "";
    }
    XMLOutputter out = new XMLOutputter();
    if (prettyPrint) {
        out.setFormat(Format.getPrettyFormat());
    }
    else {
        out.setFormat(Format.getCompactFormat());
    }
    StringWriter buffer = new StringWriter();
    out.output(aggregate, buffer);
    String retval = buffer.toString();
    retval = retval.replaceAll(" xmlns:oval=\"removeme\"", "");
    return retval.replaceAll(" xmlns:redhat=\"removeme\"", "");
}
 
Example 2
Source File: StepConfigsDOM.java    From entando-components with GNU Lesser General Public License v3.0 6 votes vote down vote up
public String createConfigXml(StepsConfig stepsConfig) throws ApsSystemException {
	String xml = null;
	try {
		Element root =  this.createConfigElement(stepsConfig);
		XMLOutputter out = new XMLOutputter();
		Format format = Format.getPrettyFormat();
		format.setIndent("\t");
		out.setFormat(format);
		Document doc = new Document(root);
		xml = out.outputString(doc);
	} catch (Throwable t) {
		_logger.error("Error creating xml config", t);
		throw new ApsSystemException("Error creating xml config", t);
	}
	return xml;
}
 
Example 3
Source File: UserShortcutConfigDOM.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected static String createUserConfigXml(String[] config) throws ApsSystemException {
	XMLOutputter out = new XMLOutputter();
	Document document = new Document();
	try {
		Element rootElement = new Element(ROOT_ELEMENT_NAME);
		document.setRootElement(rootElement);
		for (int i = 0; i < config.length; i++) {
			String shortcut = config[i];
			if (null != shortcut) {
				Element element = new Element(BOX_ELEMENT_NAME);
				element.setAttribute(POS_ATTRIBUTE_NAME, String.valueOf(i));
				element.setText(shortcut);
				rootElement.addContent(element);
			}
		}
	} catch (Throwable t) {
		_logger.error("Error parsing user config", t);
		//ApsSystemUtils.logThrowable(t, UserShortcutConfigDOM.class, "extractUserShortcutConfig");
		throw new ApsSystemException("Error parsing user config", t);
	}
	Format format = Format.getPrettyFormat();
	format.setIndent("\t");
	out.setFormat(format);
	return out.outputString(document);
}
 
Example 4
Source File: StepConfigsDOM.java    From entando-components with GNU Lesser General Public License v3.0 6 votes vote down vote up
public String createConfigXml(Map<String, StepsConfig> config) throws ApsSystemException {
	String xml = null;
	try {
		Element root = new Element(ROOT);
		Collection<StepsConfig> stepsConfigs = config.values();
		if (null != stepsConfigs) {
			Iterator<StepsConfig> iter = stepsConfigs.iterator();
			while (iter.hasNext()) {
				StepsConfig stepsConfig = iter.next();
				Element stepsConfigElements = this.createConfigElement(stepsConfig);
				root.addContent(stepsConfigElements);
			}
		}
		XMLOutputter out = new XMLOutputter();
		Format format = Format.getPrettyFormat();
		format.setIndent("\t");
		out.setFormat(format);
		Document doc = new Document(root);
		xml = out.outputString(doc);
	} catch (Throwable t) {
		_logger.error("Error creating xml config", t);
		throw new ApsSystemException("Error creating xml config", t);
	}
	return xml;
}
 
Example 5
Source File: EntityTypeDOM.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public String getXml(IApsEntity entityType) throws ApsSystemException {
	XMLOutputter out = new XMLOutputter();
	Document document = new Document();
	try {
		Element entityTypeElement = this.createTypeElement(entityType);
		document.setRootElement(entityTypeElement);
		Format format = Format.getPrettyFormat();
		format.setIndent("\t");
		out.setFormat(format);
	} catch (Throwable t) {
		_logger.error("Error building xml", t);
		throw new ApsSystemException("Error building xml", t);
	}
	return out.outputString(document);
}
 
Example 6
Source File: NewsletterConfigDOM.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Create an xml containing the newsletter configuration.
 * @param config The newsletter configuration.
 * @return The xml containing the configuration.
 * @throws ApsSystemException In case of errors.
 */
public String createConfigXml(NewsletterConfig config) throws ApsSystemException {
	Element root = this.createConfigElement(config);
	Document doc = new Document(root);
	XMLOutputter out = new XMLOutputter();
	Format format = Format.getPrettyFormat();
	format.setIndent("\t");
	out.setFormat(format);
	String xml = out.outputString(doc);
	return xml;
}
 
Example 7
Source File: MailConfigDOM.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Create an xml containing the jpmail configuration.
 * @param config The jpmail configuration.
 * @return The xml containing the configuration.
 * @throws ApsSystemException In case of errors.
 */
public String createConfigXml(MailConfig config) throws ApsSystemException {
	Element root = this.createConfigElement(config);
	Document doc = new Document(root);
	XMLOutputter out = new XMLOutputter();
	Format format = Format.getPrettyFormat();
	format.setIndent("\t");
	out.setFormat(format);
	return out.outputString(doc);
}
 
Example 8
Source File: ShortcutDefDOM.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public String getXMLDocument(){
	XMLOutputter out = new XMLOutputter();
	Format format = Format.getPrettyFormat();
	format.setIndent("\t");
	out.setFormat(format);
	return out.outputString(_doc);
}
 
Example 9
Source File: ModifyXMLFile.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
	try {

		SAXBuilder builder = new SAXBuilder();

		Document doc = (Document) builder.build(ClassLoader
				.getSystemResource("file.xml"));
		Element rootNode = doc.getRootElement();

		// update staff id attribute
		Element staff = rootNode.getChild("staff");
		staff.getAttribute("id").setValue("2");

		// add new age element
		Element age = new Element("age").setText("28");
		staff.addContent(age);

		// update salary value
		staff.getChild("salary").setText("7000");

		// remove firstname element
		staff.removeChild("firstname");

		XMLOutputter xmlOutput = new XMLOutputter();

		// display nice nice
		xmlOutput.setFormat(Format.getPrettyFormat());
		xmlOutput.output(doc, new FileWriter("target/file.xml"));

		// xmlOutput.output(doc, System.out);

		System.out.println("File updated!");
	} catch (IOException io) {
		io.printStackTrace();
	} catch (JDOMException e) {
		e.printStackTrace();
	}
}
 
Example 10
Source File: LangDOM.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Restutuisce l'xml del documento.
 * @return L'xml del documento.
 */
public String getXMLDocument(){
	XMLOutputter out = new XMLOutputter();
	Format format = Format.getPrettyFormat();
	out.setFormat(format);
	String xml = out.outputString(this.doc);
	return xml;
}
 
Example 11
Source File: AvatarConfigDOM.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Create an xml containing the jpavatar configuration.
 * @param config The jpavatar configuration.
 * @return The xml containing the configuration.
 * @throws ApsSystemException In case of errors.
 */
public String createConfigXml(AvatarConfig config) throws ApsSystemException {
	Element root = this.createConfigElement(config);
	Document doc = new Document(root);
	XMLOutputter out = new XMLOutputter();
	Format format = Format.getPrettyFormat();
	format.setIndent("\t");
	out.setFormat(format);
	return out.outputString(doc);
}
 
Example 12
Source File: WriteXMLFile.java    From maven-framework-project with MIT License 5 votes vote down vote up
public static void main(String[] args) {
 
  try {

	Element company = new Element("company");
	Document doc = new Document(company);
	doc.setRootElement(company);

	Element staff = new Element("staff");
	staff.setAttribute(new Attribute("id", "1"));
	staff.addContent(new Element("firstname").setText("yong"));
	staff.addContent(new Element("lastname").setText("mook kim"));
	staff.addContent(new Element("nickname").setText("mkyong"));
	staff.addContent(new Element("salary").setText("199999"));

	doc.getRootElement().addContent(staff);

	Element staff2 = new Element("staff");
	staff2.setAttribute(new Attribute("id", "2"));
	staff2.addContent(new Element("firstname").setText("low"));
	staff2.addContent(new Element("lastname").setText("yin fong"));
	staff2.addContent(new Element("nickname").setText("fong fong"));
	staff2.addContent(new Element("salary").setText("188888"));

	doc.getRootElement().addContent(staff2);

	// new XMLOutputter().output(doc, System.out);
	XMLOutputter xmlOutput = new XMLOutputter();

	// display nice nice
	xmlOutput.setFormat(Format.getPrettyFormat());
	xmlOutput.output(doc, new FileWriter("target/file.xml"));

	System.out.println("File Saved!");
  } catch (IOException io) {
	System.out.println(io.getMessage());
  }
}
 
Example 13
Source File: ApsPropertiesDOM.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Restituisce il formato xml delle Properties.
 * @return String Formato xml delle Properties.
 */
public String getXMLDocument(){
	XMLOutputter out = new XMLOutputter();
	Format format = Format.getPrettyFormat();
	format.setIndent("");
	out.setFormat(format);
	return out.outputString(_doc);
}
 
Example 14
Source File: WidgetTypeDOM.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public String getXMLDocument(){
	XMLOutputter out = new XMLOutputter();
	Format format = Format.getPrettyFormat();
	format.setIndent("");
	out.setFormat(format);
	return out.outputString(this.getDoc());
}
 
Example 15
Source File: NetbeansBuildActionJDOMWriterTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testInsertAtPreferredLocation() throws Exception {
    NetbeansBuildActionJDOMWriter writer = new NetbeansBuildActionJDOMWriter();
    XMLOutputter xmlout = new XMLOutputter();
    xmlout.setFormat(Format.getRawFormat().setLineSeparator("\n"));
    Element p = new Element("p");
    NetbeansBuildActionJDOMWriter.Counter c = writer.new Counter(1);
    writer.insertAtPreferredLocation(p, new Element("one"), c);
    assertEquals("<p>\n    <one />\n</p>", xmlout.outputString(p));
    c.increaseCount();
    writer.insertAtPreferredLocation(p, new Element("two"), c);
    assertEquals("<p>\n    <one />\n    <two />\n</p>", xmlout.outputString(p));
    c = writer.new Counter(1);
    writer.insertAtPreferredLocation(p, new Element("zero"), c);
    assertEquals("<p>\n    <zero />\n    <one />\n    <two />\n</p>", xmlout.outputString(p));
    c.increaseCount();
    writer.insertAtPreferredLocation(p, new Element("hemi"), c);
    assertEquals("<p>\n    <zero />\n    <hemi />\n    <one />\n    <two />\n</p>", xmlout.outputString(p));
    c.increaseCount();
    c.increaseCount();
    writer.insertAtPreferredLocation(p, new Element("sesqui"), c);
    assertEquals("<p>\n    <zero />\n    <hemi />\n    <one />\n    <sesqui />\n    <two />\n</p>", xmlout.outputString(p));
    c.increaseCount();
    c.increaseCount();
    writer.insertAtPreferredLocation(p, new Element("ultimate"), c);
    assertEquals("<p>\n    <zero />\n    <hemi />\n    <one />\n    <sesqui />\n    <two />\n    <ultimate />\n</p>", xmlout.outputString(p));
    c = writer.new Counter(1);
    writer.insertAtPreferredLocation(p, new Element("initial"), c);
    assertEquals("<p>\n    <initial />\n    <zero />\n    <hemi />\n    <one />\n    <sesqui />\n    <two />\n    <ultimate />\n</p>", xmlout.outputString(p));
    // XXX indentation still not right; better to black-box test write(ActionToGoalMapping...)
}
 
Example 16
Source File: PageExtraConfigDOM.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected String getXMLDocument(Document doc) {
	XMLOutputter out = new XMLOutputter();
	Format format = Format.getPrettyFormat();
	out.setFormat(format);
	return out.outputString(doc);
}
 
Example 17
Source File: JDOMSettingsConverter.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
public final void write( final Settings source, final File target, final String intro, String outtro ) throws IOException, JDOMException
{
    final SAXBuilder builder = new SAXBuilder();
    final Document document;

    // TODO: Improve this.
    // If we are building from an existing file then use that as a base otherwise we need to construct the Document
    // and root Element.
    if ( target.length() > 0 )
    {
        document = builder.build( target );
    }
    else
    {
        document = builder.build( new StringReader( "<settings xmlns=\"http://maven.apache.org/SETTINGS/1.0.0\" "
                                                                    + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                                                                    + "xsi:schemaLocation=\"http://maven.apache.org/SETTINGS/1.0.0 "
                                                                    + "http://maven.apache.org/xsd/settings-1.0.0.xsd\"></settings>" ) );
    }

    format.setTextMode( Format.TextMode.PRESERVE );
    format.setOmitEncoding( false );
    format.setOmitDeclaration( false );
    format.setExpandEmptyElements( false );
    format.setEncoding( StringUtils.isEmpty( source.getModelEncoding() ) ? StandardCharsets.UTF_8.toString() : source.getModelEncoding() );

    update( source, new IndentationCounter( 0 ), document.getRootElement() );

    final XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat( format );

    try ( Writer settingsWriter = WriterFactory.newWriter( target, format.getEncoding() ) )
    {
        if ( isNotBlank( intro ) )
        {
            settingsWriter.write( intro );
            outputter.output( document.getRootElement(), settingsWriter );
            settingsWriter.write( outtro );
        }
        else
        {
            outputter.output( document, settingsWriter );
        }
        settingsWriter.flush();
    }
}
 
Example 18
Source File: DataSourceDumpReport.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
public String toXml() {
	try {
		Document doc = new Document();
		Element rootElement = new Element(ROOT_ELEMENT);
		this.addElement(DATE_ELEMENT, DateConverter.getFormattedDate(this.getDate(), DATE_FORMAT), rootElement);
		this.addElement(REQUIRED_TIME_ELEMENT, String.valueOf(this.getRequiredTime()), rootElement);
		this.addElement(SUBFOLDER_NAME_ELEMENT, this.getSubFolderName(), rootElement);
		Element components = new Element(COMPONENTS_HISTORY_ELEMENT);
		rootElement.addContent(components);
		List<ComponentInstallationReport> componentsHistory = this.getComponentsHistory();
		for (int i = 0; i < componentsHistory.size(); i++) {
			ComponentInstallationReport componentHistory = componentsHistory.get(i);
			Element element = componentHistory.toJdomElement();
			components.addContent(element);
		}
		List<String> dataSourceNames = new ArrayList<String>();
		dataSourceNames.addAll(this.getDataSourcesReports().keySet());
		for (int i = 0; i < dataSourceNames.size(); i++) {
			String dataSourceName = dataSourceNames.get(i);
			Element dataSourceElement = new Element(DATASOURCE_ELEMENT);
			rootElement.addContent(dataSourceElement);
			dataSourceElement.setAttribute(NAME_ATTRIBUTE, dataSourceName);
			List<TableDumpReport> tableReports = this.getDataSourcesReports().get(dataSourceName);
			BeanComparator comparator = new BeanComparator("tableName");
			Collections.sort(tableReports, comparator);
			for (int j = 0; j < tableReports.size(); j++) {
				TableDumpReport tableDumpReport = tableReports.get(j);
				dataSourceElement.addContent(tableDumpReport.toJdomElement());
			}
		}
		doc.setRootElement(rootElement);
		XMLOutputter out = new XMLOutputter();
		Format format = Format.getPrettyFormat();
		format.setIndent("\t");
		out.setFormat(format);
		return out.outputString(doc);
	} catch (Throwable t) {
		_logger.error("Error creating XML", t);
		throw new RuntimeException("Error creating XML", t);
	}
}
 
Example 19
Source File: NetbeansBuildActionJDOMWriter.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/**
 * Method write.
 * 
 * @param actions
 * @param jdomFormat
 * @param writer
 * @param document
 * @throws java.io.IOException
 */
public void write(ActionToGoalMapping actions, Document document, Writer writer, Format jdomFormat)
    throws java.io.IOException
{
    updateActionToGoalMapping(actions, "actions", new Counter(0), document.getRootElement());
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(jdomFormat);
    outputter.output(document, writer);
}
 
Example 20
Source File: XmlUtilities.java    From ghidra with Apache License 2.0 3 votes vote down vote up
/**
 * Writes a JDOM XML {@link Document} to a {@link File}, with a prettier
 * format than {@link #writeDocToFile(Document, File)}.
 * <p>
 * 
 * @param doc JDOM XML {@link Document} to write.
 * @param dest {@link File} to write to.
 * @throws IOException if error when writing file.
 */
public static void writePrettyDocToFile(Document doc, File dest) throws IOException {
	XMLOutputter outputter = new XMLOutputter();
	outputter.setFormat(Format.getPrettyFormat());
	try (FileWriter fw = new FileWriter(dest)) {
		outputter.output(doc, fw);
	}
}