From: Jeff M. <je...@mk...> - 2003-05-08 09:19:53
|
Looks cool, bit busy at the moment though so it might take me a couple of days to look at this properly. Catalog support sounds good, so i'd be happy to consider anything you come up with. On Thu, 2003-05-08 at 07:56, Luca Dall'Olio wrote: > I have really appreciated your enhancement to the Validator class > including the useXMLSchema() method; now I can validate my xml using xsd > schema. > Still, I couldn't find out how to redirect the retrival of xsd files : > they are always supposed to be in the project home directory (or the ant > home, depending on the build tool I use). > After some code digging I suppose that the current way to redirect files > works only for DTDs, so I tried to extend it for xsd too. > I have not written a clean solution but it seems to work, so I thought > better to post it : perhaps it could be useful for others too. > My solution is based on an extension of the DoctypeReader class (this > way I can pass it to the Validator constructor) that parses the input, > adds a couple of attributes to the root element with the xsd info and > serializes it back to a string... very similar to the doctypeReader > itself. I have written a simple helper class to use the assertXMLValid() > interface, it can be useful as an example of the calling. > This is the way I call it : > > private String xsdName = "ordinativoFirmato.xsd"; > private String xsdPath = > this.getClass().getResource(xsdName).toString(); > > ... > > MyXMLAssert.assertXMLValid(supposedNormalizedString, xsdName, > xsdPath); > > I think that a much cleaner way to solve this problem would be to make > use of oasis catalogs : > > http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=entity > > An implementation can be downloaded at the apache site in the xml > commons package, and IMVHO it should be rather straightforward to add it > to xmlunit: > > http://xml.apache.org/commons/ > > If you are interested I can have a look at it, just let me know! > > greetings, Luca Dall'Olio (dal...@un...) > > ______________________________________________________________________ > /* > * Created on May 5, 2003 > * > * To change the template for this generated file go to > * Window>Preferences>Java>Code Generation>Code and Comments > */ > package it.unimaticaspa.unimoney.utils; > > import java.io.IOException; > import java.io.Reader; > import java.io.StringReader; > import java.io.StringWriter; > import java.util.Properties; > > import javax.xml.parsers.DocumentBuilder; > import javax.xml.parsers.DocumentBuilderFactory; > import javax.xml.parsers.ParserConfigurationException; > import javax.xml.transform.OutputKeys; > import javax.xml.transform.Transformer; > import javax.xml.transform.TransformerConfigurationException; > import javax.xml.transform.TransformerException; > import javax.xml.transform.TransformerFactory; > import javax.xml.transform.dom.DOMSource; > import javax.xml.transform.stream.StreamResult; > > import org.custommonkey.xmlunit.DoctypeReader; > import org.w3c.dom.Document; > import org.w3c.dom.Element; > import org.xml.sax.InputSource; > import org.xml.sax.SAXException; > > /** > * First written at : May 5, 2003 10:14:16 AM > * Project : unimoney > * Package : it.unimaticaspa.unimoney.utils > * File : SchemaReader.java > * Type : SchemaReader > * @author ldallolio Luca Dall'Olio lda...@un... > * > * To change the template for this generated type comment go to > * Window>Preferences>Java>Code Generation>Code and Comments > */ > public class SchemaReader extends DoctypeReader { > > private String _xsdName = null; > private String _systemID = null; > private Reader replacementReader; > > /** > * Create a Reader whose XML content is provided by the originalSource with > * the exception of the xsd schema which is provided by the xsdName > * and systemID. > * @param originalSource > * @param doctypeName > * @param systemID > */ > public SchemaReader( > Reader originalSource, > String xsdName, > String systemID) { > super(originalSource, xsdName, systemID); > this._xsdName = xsdName; > this._systemID = systemID; > } > > /* (non-Javadoc) > * @see java.io.Reader#close() > */ > public void close() throws IOException { > this.replacementReader.close(); > } > > /** > * @return the content of the original source, without amendments or > * substitutions. Safe to call multiple times. > * @throws IOException if thrown while reading from the original source > */ > protected String getContent() throws IOException { > return super.getContent(); > } > > /** > * Read DOCTYPE-replaced content from the wrapped Reader > * @param cbuf > * @param off > * @param len > * @return The number of characters read, or -1 if the end of the > * stream has been reached > * @throws IOException > */ > public int read(char cbuf[], int off, int len) throws IOException { > if (replacementReader == null) { > replacementReader = getReplacementReader(); > } > return replacementReader.read(cbuf, off, len); > } > > /** > * Perform DOCTYPE amendment / addition within some marked-up content > * @param withinContent > * @param doctypeName > * @param systemId > * @return the content, after DOCTYPE amendment / addition > */ > public String replaceDoctype( > StringBuffer withinContent, > String xsdName, > String systemId) { > String content = withinContent.toString(); > > Document domContent = null; > try { > DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); > dbf.setNamespaceAware(true); > > DocumentBuilder db = dbf.newDocumentBuilder(); > domContent = db.parse(new InputSource(new StringReader(content))); > } catch (IOException e) { > // TODO Auto-generated catch block > e.printStackTrace(); > } catch (ParserConfigurationException e) { > // TODO Auto-generated catch block > e.printStackTrace(); > } catch (SAXException e) { > // TODO Auto-generated catch block > e.printStackTrace(); > } > > Element root = domContent.getDocumentElement(); > root.setAttributeNS( > "http://www.w3.org/2000/xmlns/", > "xmlns:xsi", > "http://www.w3.org/2001/XMLSchema-instance"); > root.setAttribute("xsi:noNamespaceSchemaLocation", xsdName); > > String modifiedString = null; > try { > StringWriter writer = new StringWriter(); > StreamResult result = new StreamResult(writer); > TransformerFactory factory = TransformerFactory.newInstance(); > Transformer transformer = factory.newTransformer(); > Properties properties = transformer.getOutputProperties(); > > properties.put(OutputKeys.ENCODING, "ISO-8859-1"); > properties.put(OutputKeys.INDENT, "yes"); > transformer.setOutputProperties(properties); > > DOMSource src = new DOMSource(domContent); > transformer.transform(src, result); > modifiedString = writer.toString(); > } catch (TransformerConfigurationException e1) { > // TODO Auto-generated catch block > e1.printStackTrace(); > } catch (TransformerException e1) { > // TODO Auto-generated catch block > e1.printStackTrace(); > } > > return modifiedString; > } > > /** > * Wrap the DOCTYPE-replaced content in a StringReader > * @return a StringReader from which the DOCTYPE-replaced content can be read > * @throws IOException > */ > private Reader getReplacementReader() throws IOException { > String modified = > replaceDoctype( > new StringBuffer(super.getContent()), > this._xsdName, > this._systemID); > return new StringReader(modified); > } > > } > > ______________________________________________________________________ > /* > * Created on May 5, 2003 > * > * To change the template for this generated file go to > * Window>Preferences>Java>Code Generation>Code and Comments > */ > package it.unimaticaspa.unimoney.utils; > > import java.io.StringReader; > > import javax.xml.parsers.ParserConfigurationException; > > import org.custommonkey.xmlunit.DoctypeReader; > import org.custommonkey.xmlunit.Validator; > import org.custommonkey.xmlunit.XMLAssert; > import org.xml.sax.SAXException; > > /** > * First written at : May 5, 2003 11:45:25 AM > * Project : unimoney > * Package : it.unimaticaspa.unimoney.utils > * File : MyXMLAssert.java > * Type : MyXMLAssert > * @author ldallolio Luca Dall'Olio lda...@un... > * > * To change the template for this generated type comment go to > * Window>Preferences>Java>Code Generation>Code and Comments > */ > public class MyXMLAssert { > > /** > * > */ > protected MyXMLAssert() { > super(); > // TODO Auto-generated constructor stub > } > > /** > * Assert that a String containing XML contains valid XML: the String will > * be given a DOCTYPE to be validated with the name and systemId specified > * regardless of whether it already contains a doctype declaration. > * @param xmlString > * @param systemId > * @param doctype > * @throws SAXException > * @throws ParserConfigurationException > * @see Validator > */ > public static void assertXMLValid(String xmlString, String systemId, String doctype) > throws SAXException, ParserConfigurationException { > DoctypeReader schemaReader = > new SchemaReader( > new StringReader(xmlString), > doctype, > systemId); > Validator myValidator = new Validator(schemaReader); > myValidator.useXMLSchema(true); > XMLAssert.assertXMLValid(myValidator); > } > } |