3.8 DOM

3.8.1 Outline

Associate a name to the result of applying the named method on the DOM of the named XML code. The method should have an interface similar to public static void m( Node dom ).

<dom name="..." xml="..." method="..." class="..." />

<..xtpipes entries..>+
 | dom
 -_-_-

<..xtpipes.dtd..>+
 <!ELEMENT dom EMPTY >
 <!ATTLIST dom
           name   CDATA #REQUIRED
           xml    CDATA #REQUIRED
           method CDATA #REQUIRED
           class  CDATA #REQUIRED
           dcl    CDATA #IMPLIED DEFAULT (yes | no) "no"
 >
 -_-_-

<..execute dom..>
 try{
    <.load xml into dom.>
    <.transform dom.>
    <.store dom into map.>
 } catch ( NoSuchMethodException e ){
    instructionErr( node,
        "could not find method: " + e.getMessage(), 18 );
 } catch ( java.lang.reflect.InvocationTargetException e ){
    if( Xtpipes.trace ){ e.printStackTrace(); }
    instructionErr( node, e.getCause().toString(), 36);
 } catch ( Exception e ){
    if( Xtpipes.trace ){ e.printStackTrace(); }
    instructionErr( node, e.toString(), 20 );
 }
 -_-_-

3.8.2 Transform

<..transform dom..>
 String className = node.getAttributes()
              .getNamedItem( "class" ).getNodeValue();
 String methodName = node.getAttributes()
              .getNamedItem( "method" ).getNodeValue();
 Class <?> cls = Class.forName( className );
 Class<?>  [] argTypes = { Node.class };
 Method m = cls.getMethod( methodName, argTypes );
 Object parmValues[] = new Object[1];
 parmValues[0] = dom;
 m.invoke( null, parmValues );
 -_-_-

<..xtpipes initialization..>+
 domFactory.setValidating(false);
 domBuilder = domFactory.newDocumentBuilder();
 -_-_-

<..xtpipes fields..>+
 private static DocumentBuilder domBuilder;
 -_-_-

3.8.3 Input Stream

<..load xml into dom..>
 Node xmlNode = node.getAttributes().getNamedItem( "xml" );
 Document dom;
 if( xmlNode == null ){
    <.dom = parse input.>
 } else {
    String xml = xmlNode.getNodeValue();
    String doc = (String) map.get(xml);
    if( doc == null ){
       instructionErr( node, "improper xml attribute value", 18 );
    }
    byte [] bytes = doc.getBytes("UTF-8");
    InputStream is = new ByteArrayInputStream( bytes );
    dom = domBuilder.parse (is);
 }
 -_-_-

<..dom = parse input..>
 if( inData == null ){
    dom = domBuilder.parse( new File(inFile) );
 } else {
    byte [] bytes = inData.getBytes("UTF-8");
    InputStream is =  new ByteArrayInputStream( bytes );
    dom = domBuilder.parse (is);}
 -_-_-

3.8.4 Output Stream

<..store dom into map..>
 <.StreamResult outDoc = ....>
 cleanXmlns(dom);
 DOMSource domSource = new DOMSource(dom);
 try{
    identityTransformer.transform( domSource, outDoc );
 } catch ( javax.xml.transform.TransformerException e ){
   String s = Xtpipes.trace?
       (
         "\n------------------------ xml code ------------------------\n"
       + serialize( dom )
       + "\n----------------------------------------------------------\n"
       )
       : "";
    instructionErr( node, e.getMessage() + s, 35 );
 }
 if( nameNode != null ){
   String name = nameNode.getNodeValue();
   char [] chars = caos.toCharArray() ;
   String domString = new String(chars);
   <.remove dom dcl.>
   map.put( name, (Object) domString );
 }
 -_-_-

<..remove dom dcl..>
 Node dcl = node.getAttributes().getNamedItem( "dcl" );
 if(  ((dcl == null) || (dcl.getNodeValue().equals("no") ))
      &&
        (domString.length() > 7)
      &&
        domString.startsWith("<?xml")
      &&
        !Character.isLetterOrDigit( domString.charAt(5) )
 ){
     domString = domString.substring( domString.indexOf("?>") + 2 );
 }
 -_-_-

<..NO..>+
 String name = node.getAttributes()
              .getNamedItem( "name" ).getNodeValue();
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 identityTransformer.transform( new DOMSource(dom),
                              new StreamResult(baos) );
 bytes = baos.toByteArray();
 map.put( name, (Object) new String(bytes) );
 -_-_-