package ca.umontreal.iro.rali; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; public class Debug { // debugging methods /** * Prints a message followed by the compact form of a document Node * Only used for debugging purpose * * @param mess String giving the message to show * @param elem Node to print in compact form */ public static void dumpElem(String mess,Node elem){ if(mess!=null && mess.length()>0) System.out.println(mess+":"); if(elem==null) System.out.println("!null!"); else { // compact(elem,null); // for non indented output compact(elem,""); // for an indented output System.out.println(); } } /** * print a compact form of a Node (used here only for debugging) * * @param node starting node to compact * @param indent string added at the start of each new line */ public static void compact(Node node,String indent) { if (node == null)return; short type = node.getNodeType(); switch (type) { case Node.ELEMENT_NODE: String nodeName=node.getNodeName(); if(node.getPrefix()==null){ if(node.getNamespaceURI()!=null) nodeName=Options.nsContext.getOrGenPrefix(node.getNamespaceURI())+":"+node.getLocalName(); } else nodeName=node.getPrefix()+":"+node.getLocalName(); System.out.print(nodeName+'['); if(indent!=null)indent += blanks(nodeName.length()+1); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { if(indent!=null && i>0)System.out.print('\n'+indent); compact(attrs.item(i),indent); } Node child = node.getFirstChild(); while(child != null){ if(indent!=null && (attrs.getLength()>0||child!=node.getFirstChild())) System.out.print('\n'+indent); compact(child,indent); child = child.getNextSibling(); } System.out.print(']'); break; case Node.ATTRIBUTE_NODE: System.out.print('@'+node.getNodeName()+'['+node.getNodeValue()+']'); break; case Node.TEXT_NODE: System.out.print(node.getNodeValue().trim()); break; case Node.DOCUMENT_NODE: compact(((Document)node).getDocumentElement(),indent); break; case Node.COMMENT_NODE: System.out.print(""); break; case Node.PROCESSING_INSTRUCTION_NODE: System.out.println(""); break; } } /** * Lazy buffer string containing a given number of spaces */ private static StringBuffer blanks = new StringBuffer(" "); /** * returns a string of spaces with a lazy StringBuffer * * @param n number of spaces to return * @return string of n spaces */ private static String blanks(int n){ for(int i=blanks.length();i