From aec99f5c0ea6c754ab823e99fa9f0a82fd99fa4b Mon Sep 17 00:00:00 2001 From: wwang Date: Thu, 8 Dec 2011 10:11:28 +0000 Subject: [PATCH] fix Bug TDI-18747: With "tAvancedFileOutputXML" and generation mode "slow with no memory consumed" xml file generated is KO. git-svn-id: http://talendforge.org/svn/tos/trunk@74110 f6f1c999-d317-4740-80b0-e6d1abc6f99e --- .../java/routines/system/XMLHelper.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 org.talend.librariesmanager/resources/java/routines/system/XMLHelper.java diff --git a/org.talend.librariesmanager/resources/java/routines/system/XMLHelper.java b/org.talend.librariesmanager/resources/java/routines/system/XMLHelper.java new file mode 100644 index 0000000000..16cbd5c3dc --- /dev/null +++ b/org.talend.librariesmanager/resources/java/routines/system/XMLHelper.java @@ -0,0 +1,60 @@ +package routines.system; + +import java.io.StringReader; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; + +/** + * helper for xml source + * @author Administrator + * + */ +public class XMLHelper { + + private static XMLHelper instance; + + private XMLReader reader; + + private XMLHelper() { + SAXParserFactory factory = SAXParserFactory.newInstance(); + try { + reader = factory.newSAXParser().getXMLReader(); + reader.setErrorHandler(null); + } catch (SAXException e) { + e.printStackTrace(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + } + + public static XMLHelper getInstance() { + if(instance == null) { + instance = new XMLHelper(); + } + return instance; + } + + + + /** + * validate xml source + * return true if xml is well formed + * @param source + * @return + */ + public boolean isValid(String xml) { + try { + InputSource source = new InputSource(new StringReader(xml)); + reader.parse(source); + return true; + } catch(Exception e) { + return false; + } + } + +}