3.10 Handling DOCTYPE Statements

The following code hides the DOCTYPE entry when searching the xtpipes processing instruction and when loading an input file.

<..saxReader := ignore DOCTYPE statement..>
 saxReader.setEntityResolver(new org.xml.sax.EntityResolver() {
    public InputSource resolveEntity(
                           String publicId, String systemId) {
      if( (new File(systemId)).exists() ){
         return new org.xml.sax.InputSource( systemId );
       }
       StringReader strReader = new StringReader("");
       return new org.xml.sax.InputSource(strReader);
    }
 });
 -_-_-

The default SAX behavior observes the DOCTYPE statement and assumes the following setup.

saxReader.setEntityResolver(new org.xml.sax.EntityResolver() {  
   public InputSource resolveEntity(  
      String publicId, String systemId) {  
      return null;  
   }  
});

3.10.1 Input

<..load xml into input source..>
 Node xmlNode = node.getAttributes().getNamedItem( "xml" );
 InputSource inputSource=null;
 String xml = null;
 if( xmlNode == null ){
    <.load xml from input.>
 } else {
    xml = xmlNode.getNodeValue();
    String doc = (String) map.get(xml);
    if( doc!=null ){
       byte [] bytes = doc.getBytes("UTF-8");
       ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
       inputSource = new InputSource( bais );
 }  }
 -_-_-

<..load xml from input..>
 if( inData == null ){
    xml = inFile;
 } else {
    byte [] bytes = inData.getBytes("UTF-8");
    ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
    inputSource = new InputSource( bais );
 }
 -_-_-

3.10.2 Output Stream

<..output stream for sax..>
 Node nameNode = node.getAttributes().getNamedItem("name");
 PrintWriter out;
 CharArrayWriter caos = null;
 if( nameNode == null ){
    out = outPrintWriter;
    returnToFile = false;
 } else {
    caos = new CharArrayWriter();
    out = new PrintWriter( caos );
 }
 -_-_-

<..store chars into map..>
 if( nameNode != null ){
    String name = nameNode.getNodeValue();
    char [] chars = caos.toCharArray() ;
    map.put( name, (Object) new String(chars) );
 }
 -_-_-

Note: Characters are prefered over bytes as they support unicode.