1 5 6 package javax.xml.bind.annotation; 7 8 import org.w3c.dom.Document ; 9 import org.w3c.dom.DocumentFragment ; 10 import org.w3c.dom.Element ; 11 import org.w3c.dom.Node ; 12 13 import javax.xml.bind.ValidationEventHandler; 14 import javax.xml.parsers.DocumentBuilder ; 15 import javax.xml.transform.Source ; 16 import javax.xml.transform.dom.DOMResult ; 17 import javax.xml.transform.dom.DOMSource ; 18 19 25 public class W3CDomHandler implements DomHandler<Element ,DOMResult > { 26 27 private DocumentBuilder builder; 28 29 35 public W3CDomHandler() { 36 this.builder = null; 37 } 38 39 47 public W3CDomHandler(DocumentBuilder builder) { 48 if(builder==null) 49 throw new IllegalArgumentException (); 50 this.builder = builder; 51 } 52 53 public DocumentBuilder getBuilder() { 54 return builder; 55 } 56 57 public void setBuilder(DocumentBuilder builder) { 58 this.builder = builder; 59 } 60 61 public DOMResult createUnmarshaller(ValidationEventHandler errorHandler) { 62 if(builder==null) 63 return new DOMResult (); 64 else 65 return new DOMResult (builder.newDocument()); 66 } 67 68 public Element getElement(DOMResult r) { 69 Node n = r.getNode(); 72 if( n instanceof Document ) { 73 return ((Document )n).getDocumentElement(); 74 } 75 if( n instanceof Element ) 76 return (Element )n; 77 if( n instanceof DocumentFragment ) 78 return (Element )n.getChildNodes().item(0); 79 80 throw new IllegalStateException (n.toString()); 84 } 85 86 public Source marshal(Element element, ValidationEventHandler errorHandler) { 87 return new DOMSource (element); 88 } 89 } 90 | Popular Tags |