-
-
Save micheee/4fc5341c1cff57a5475b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import javax.xml.XMLConstants; | |
import javax.xml.transform.Source; | |
import javax.xml.transform.stream.StreamSource; | |
import javax.xml.validation.Schema; | |
import javax.xml.validation.SchemaFactory; | |
import javax.xml.validation.Validator; | |
import org.xml.sax.ErrorHandler; | |
import org.xml.sax.SAXException; | |
import org.xml.sax.SAXParseException; | |
public class ValidateTest { | |
private static final String URL = "https://raw.github.com/gist/29ff06c0720988d3d72a/b3f2deaa13bc2837504764b60b185267ffcaddc8/atomic.xml"; | |
private static final String SCH_URL = "https://raw.github.com/gist/29ff06c0720988d3d72a/af6ec6b9410ded5b9b25bfd3bd4c1577d373c5e1/atomic.xsd"; | |
public static void main(String[] args) throws SAXException, IOException { | |
notWorking(); | |
working(); | |
} | |
public static void notWorking() throws IOException, | |
MalformedURLException, SAXException { | |
final SchemaFactory sf = SchemaFactory | |
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); | |
final Schema schema = sf.newSchema(); | |
final Source s = new StreamSource(new URL(URL).openStream()); | |
final Validator v = schema.newValidator(); | |
v.setErrorHandler(new Eh()); | |
try { | |
v.validate(s); | |
} catch (final SAXException e) { | |
System.err.println("Implicit schema: Could not validate"); | |
System.err.println("\t" + e.getMessage()); | |
} | |
} | |
/** setting explicitly works: */ | |
public static void working() throws SAXException, IOException, | |
MalformedURLException { | |
final SchemaFactory sf = SchemaFactory | |
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); | |
final Schema schema = sf.newSchema(new URL(SCH_URL)); | |
final Source s = new StreamSource(new URL(URL).openStream()); | |
final Validator v = schema.newValidator(); | |
v.setErrorHandler(new Eh()); | |
try { | |
v.validate(s); | |
} catch (final SAXException e) { | |
// TODO Auto-generated catch block | |
System.err.println("Could not validate"); | |
e.printStackTrace(); | |
} | |
System.out.println("Validated with explicit declaration"); | |
} | |
/** ugly error handler */ | |
private static final class Eh implements ErrorHandler { | |
@Override | |
public void fatalError(SAXParseException exception) throws SAXException { | |
throw exception; | |
} | |
@Override | |
public void error(SAXParseException exception) throws SAXException { | |
throw exception; | |
} | |
@Override | |
public void warning(SAXParseException exception) throws SAXException { | |
// TODO Auto-generated method stub | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment