10.1 Clean Xmlns

<..static void cleanXmlns( dom )..>
 static ArrayList<String> nsName, nsValue;
 static void cleanXmlns( Node root ){
    if( root.getNodeType() == Node.ELEMENT_NODE) {
       int top = nsName.size();
       <.clean xmlns attributes.>
       <.clean xmlns in children.>
        for(int i=nsName.size(); i>top; ){
          i--;
          nsName.remove(i);
          nsValue.remove(i);
        }
    } else if( root.getNodeType() == Node.DOCUMENT_NODE) {
       nsName = new ArrayList<String>();
       nsValue = new ArrayList<String>();
       <.clean xmlns in children.>
       nsName = null;
       nsValue = null;
 }  }
 -_-_-

<..clean xmlns in children..>
 NodeList children = root.getChildNodes();
 if(children.getLength() > 0) {
    for(int i = 0; i < children.getLength(); i++) {
       cleanXmlns(children.item(i));
 }  }
 -_-_-

<..clean xmlns attributes..>
 ArrayList<Attr> remove = new ArrayList<Attr>();
 NamedNodeMap attributes = root.getAttributes();
 for(int i = 0; i < attributes.getLength(); i++) {
    Attr attribute = (Attr) attributes.item(i);
    String name = attribute.getName();
    if( name.startsWith("xmlns") ){
      if( (name.length() == 5) || (name.charAt(5) == ’:’) ){
         String value = attribute.getValue();
         <.bool := xmlns active?.>
         if( bool ){ remove.add(attribute);
         } else { nsName.add(name); nsValue.add(value); }
 }  } }
 for(int i=remove.size(); i>0; ){
    i--;
   ((Element) root).removeAttributeNode( (Attr) remove.get(i) );
 }
 remove = null;
 -_-_-

<..bool := xmlns active?..>
 boolean bool = false;
 for(int k=nsName.size(); k>0; ){
   k--;
   if( ((String) nsName.get(k)) . equals(name) ){
      bool = ((String) nsValue.get(k)) . equals(value);
      break;
 } }
 -_-_-

<..xtpipes imports..>+
 import java.util.ArrayList;
 -_-_-