// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class DefAttrib {
    public static void main(String[] args) throws Exception {
        //String xml = "<test xmlns='http://notexist/test' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://notexist/test http://notexist/test' a='123'/>";
        String xml = "<test xmlns='http://notexist/test' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://notexist/test http://notexist/test'/>";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(true);
        dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setErrorHandler(new MyErrorHandler());
        db.setEntityResolver(new SchemaHandler());
        Document doc = db.parse(new InputSource(new StringReader(xml )));
        System.out.println(doc.getDocumentElement().getAttribute("a"));
    }
}

class SchemaHandler implements EntityResolver {
    @Override
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
        if(systemId.equals("http://notexist/test")) {
            //String xsd = "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:test='http://notexist/test' targetNamespace='http://notexist/test'><xsd:complexType name='testType'><xsd:simpleContent><xsd:extension base='xsd:string'><xsd:attribute name='a' type='xsd:integer'/></xsd:extension></xsd:simpleContent></xsd:complexType><xsd:element name='test' type='test:testType'/></xsd:schema>";
            String xsd = "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:test='http://notexist/test' targetNamespace='http://notexist/test'><xsd:complexType name='testType'><xsd:simpleContent><xsd:extension base='xsd:string'><xsd:attribute name='a' type='xsd:integer' default='123'/></xsd:extension></xsd:simpleContent></xsd:complexType><xsd:element name='test' type='test:testType'/></xsd:schema>";
            return new InputSource(new StringReader(xsd));
        } else {
            return null;
        }
    }
}

class MyErrorHandler implements ErrorHandler {
    @Override
    public void error(SAXParseException exception) throws SAXException {
        System.out.println(exception.getMessage());
    }
    @Override
    public void fatalError(SAXParseException exception) throws SAXException {
        System.out.println(exception.getMessage());
    }
    @Override
    public void warning(SAXParseException exception) throws SAXException {
        System.out.println(exception.getMessage());
    }
}
