import java.io.File; import java.io.IOException; import java.io.Writer; import java.util.List; import org.apache.xerces.xs.AttributePSVI; import org.apache.xerces.xs.PSVIProvider; import org.apache.xerces.xs.XSConstants; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.Namespace; import org.jdom.input.DefaultJDOMFactory; import org.jdom.input.JDOMFactory; import org.jdom.input.SAXBuilder; import org.jdom.input.SAXHandler; import org.jdom.output.XMLOutputter; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; /** * @author Per Norrman * */ public class TestSchema { public static void main(String[] args) throws Exception { SAXBuilder builder = new MySAXBuilder(true); builder.setFeature( "http://apache.org/xml/features/validation/schema", true); builder.setFeature( "http://apache.org/xml/features/validation/schema/augment-psvi", true); builder.setProperty( "http://apache.org/xml/properties/schema/external-schemaLocation", TMUtil.createSchemaLocation()); Document doc = builder.build(new File("data/schema/standard.xml")); XMLOutputter out = new MyXMLOutputter(); out.output(doc, System.out); } } class MySAXBuilder extends SAXBuilder { private PSVIProvider psvi; public MySAXBuilder(boolean validate) { super(validate); } protected XMLReader createParser() throws JDOMException { XMLReader reader = super.createParser(); try { psvi = (PSVIProvider)reader; } catch (ClassCastException cce) { // tough luck } return reader; } /** override */ protected SAXHandler createContentHandler() { return new MySAXHandler(new MyFactory()); } private class MySAXHandler extends SAXHandler { public MySAXHandler(JDOMFactory factory) { super(factory); } /** override */ public void startElement( String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); markSchemaDefinedAttributes(getCurrentElement(), attributes); } private void markSchemaDefinedAttributes( Element element, Attributes attributes) { if (psvi == null) { return; } for (int i = 0; i < attributes.getLength(); i++) { AttributePSVI apsvi = psvi.getAttributePSVI(i); if (apsvi != null && apsvi.getIsSchemaSpecified()) { String uri = attributes.getURI(i); Namespace ns = Namespace.getNamespace(uri); AttributeEx attribute = (AttributeEx)element.getAttribute( attributes.getLocalName(i), ns); attribute.setSchemaDefined(true); } } } } } class AttributeEx extends Attribute { private boolean _schemaDefined = false; public AttributeEx() { super(); } public AttributeEx(String name, String value, Namespace namespace) { super(name, value, namespace); } public AttributeEx( String name, String value, int type, Namespace namespace) { super(name, value, type, namespace); } public AttributeEx(String name, String value) { super(name, value); } public AttributeEx(String name, String value, int type) { super(name, value, type); } public boolean isSchemaDefined() { return _schemaDefined; } public void setSchemaDefined(boolean b) { _schemaDefined = b; } } class MyFactory extends DefaultJDOMFactory { public Attribute attribute(String name, String value) { return new AttributeEx(name, value); } public Attribute attribute(String name, String value, int type) { return new AttributeEx(name, value, type); } public Attribute attribute( String name, String value, int type, Namespace namespace) { return new AttributeEx(name, value, type, namespace); } public Attribute attribute( String name, String value, Namespace namespace) { return new AttributeEx(name, value, namespace); } } class MyXMLOutputter extends XMLOutputter { protected void printAttributes( Writer out, List attributes, Element parent, NamespaceStack namespaces) throws IOException { for (int i = 0; i < attributes.size(); i++) { AttributeEx attribute = (AttributeEx)attributes.get(i); if (!attribute.isSchemaDefined()) { printAttribute(out, attribute, namespaces); } } } }