Skip to content

Instantly share code, notes, and snippets.

@HugoPoi
Created December 3, 2014 16:48
Show Gist options
  • Save HugoPoi/7f888ca1246297217be1 to your computer and use it in GitHub Desktop.
Save HugoPoi/7f888ca1246297217be1 to your computer and use it in GitHub Desktop.
XMLValidator JAVA no externals libraries
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/XMLValidator.iml" filepath="$PROJECT_DIR$/XMLValidator.iml" />
</modules>
</component>
</project>
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
</component>
</project>
package com.company;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
public class Main {
public static void main(String[] args) {
if(args.length >= 1){
try {
if(args.length == 1 ){
//This code work only if a DOCTYPE statement is present in the document
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setValidating(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) throws SAXException {
System.out.println(exception.getLocalizedMessage());
}
@Override
public void error(SAXParseException exception) throws SAXException {
System.out.println(exception.getLocalizedMessage());
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
System.out.println(exception.getLocalizedMessage());
}
});
docBuilder.parse(args[0]);
}else if(args.length == 2 && getExtension(args[1]).equals("xsd")){
// The following code doesn't work if the document contains a DOCTYPE statement.
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new StreamSource(args[1]));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(args[0]));
System.out.println("Document is valid.");
}else{
System.out.println("Second file must be a .dtd or xsd");
}
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}else{
System.out.println("Usage: {.xml} [.xsd]");
}
}
private static String getExtension(String filename){
return filename.substring(filename.toUpperCase().lastIndexOf(".") + 1);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment