package hterhors.editor.xml; import java.io.StringReader; import org.apache.xerces.parsers.SAXParser; import org.xml.sax.ContentHandler; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; /** * @author Hendrik */ public class XMLParser { private ErrorHandler errorHandler; private ContentHandler contentHandler; /** * @param errorHandler * @uml.property name="errorHandler" */ public void setErrorHandler(ErrorHandler errorHandler) { this.errorHandler = errorHandler; } /** * @param contentHandler * @uml.property name="contentHandler" */ public void setContentHandler(ContentHandler contentHandler) { this.contentHandler = contentHandler; } public static final String VALIDATION_FEATURE = "http://xml.org/sax/features/validation"; public void doParse(String xmlText) throws RuntimeException { InputSource inputSource = new InputSource(new StringReader(xmlText)); try { XMLReader reader = new SAXParser(); reader.setErrorHandler(errorHandler); reader.setContentHandler(contentHandler); reader.setFeature(VALIDATION_FEATURE, true); reader.parse(inputSource); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } }