All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mxgraph.util.mxXmlUtils Maven / Gradle / Ivy

/**
 * $Id: mxXmlUtils.java,v 1.1 2012-01-13 12:26:15 david Exp $
 * Copyright (c) 2007-2012, JGraph Ltd
 */
package com.mxgraph.util;

import java.io.StringReader;
import java.io.StringWriter;

import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import org.xml.sax.InputSource;

/**
 * Contains various XML helper methods for use with mxGraph.
 */
public class mxXmlUtils
{
	/**
	 * Returns a new document for the given XML string.
	 * 
	 * @param xml
	 *            String that represents the XML data.
	 * @return Returns a new XML document.
	 */
	public static Document parseXml(String xml)
	{
		try
		{
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			docBuilderFactory.setNamespaceAware(true);
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

			return docBuilder.parse(new InputSource(new StringReader(xml)));
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(null, "Not a valid XML file!", "Error", JOptionPane.ERROR_MESSAGE);
			return null;
		}
	}
	/**
	 * Returns a string that represents the given node.
	 * 
	 * @param node
	 *            Node to return the XML for.
	 * @return Returns an XML string.
	 */
	public static String getXml(Node node)
	{
		try
		{
			Transformer tf = TransformerFactory.newInstance().newTransformer();

			//tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			String encoding = System.getProperty("file.encoding");

			tf.setOutputProperty(OutputKeys.ENCODING, encoding);
			tf.setOutputProperty(OutputKeys.STANDALONE, "yes");
			tf.setOutputProperty(OutputKeys.INDENT, "yes");
			tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 
			
			StreamResult dest = new StreamResult(new StringWriter());
			tf.transform(new DOMSource(node), dest);

			return new String(dest.getWriter().toString().getBytes(encoding));
		}
		catch (Exception e)
		{
			// ignore
		}

		return "";
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy