1 8 9 import java.io.InputStream ; 10 import java.io.FileInputStream ; 11 import java.io.BufferedInputStream ; 12 import java.io.OutputStream ; 13 import java.io.FileOutputStream ; 14 import java.io.Writer ; 15 import java.io.PrintWriter ; 16 import java.io.FileWriter ; 17 import java.io.BufferedWriter ; 18 import java.io.FileWriter ; 19 import java.io.IOException ; 20 21 import java.util.ArrayList ; 22 23 import org.w3c.dom.NodeList ; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.Attr ; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.NamedNodeMap ; 29 import org.w3c.tidy.Tidy; 30 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.taskdefs.Property; 33 34 40 41 public class JTidyTask 42 extends org.apache.tools.ant.Task 43 { 44 private String src; 45 private String dest; 46 private String log; 47 private Tidy tidy; 48 private String warn = "false"; 49 private String summary = "false"; 50 PrintWriter pw; 51 52 55 56 public JTidyTask() 57 { 58 super(); 59 } 60 61 64 65 public void init() 66 { 67 super.init(); 68 69 tidy = new Tidy(); 71 tidy.setXmlOut(true); 72 tidy.setXHTML(true); 73 tidy.setDropFontTags(true); 74 tidy.setLiteralAttribs(true); 75 tidy.setMakeClean(true); 76 tidy.setShowWarnings(Boolean.getBoolean(warn)); 77 tidy.setQuiet(!Boolean.getBoolean(summary)); 78 } 79 80 84 85 public void execute() 86 throws org.apache.tools.ant.BuildException 87 { 88 try 89 { 90 PrintWriter pw = new PrintWriter (new FileWriter (log)); 91 92 tidy.setErrout(pw); 93 94 BufferedInputStream in = 96 new BufferedInputStream (new FileInputStream (src)); 97 98 PrintWriter out = 100 new PrintWriter (new FileWriter (dest)); 101 102 org.w3c.dom.Document domDoc = tidy.parseDOM(in, null); 104 105 domDoc.normalize(); 106 stripDuplicateAttributes(domDoc, null); 107 org.apache.xml.serialize.OutputFormat format = 108 new org.apache.xml.serialize.OutputFormat(); 109 110 format.setIndenting(true); 111 format.setEncoding("ISO-8859-1"); 112 format.setPreserveSpace(true); 113 format.setLineSeparator("\n"); 114 org.apache.xml.serialize.XMLSerializer serializer = 115 new org.apache.xml.serialize.XMLSerializer(out, format); 116 117 serializer.serialize(domDoc); 118 out.flush(); 119 out.close(); 120 in.close(); 121 pw.flush(); 122 pw.close(); 123 } 124 catch (IOException ioe) 125 { 126 throw new BuildException(ioe); 127 } 128 } 129 130 public void setSrc(String src) 131 { 132 this.src = src; 133 } 134 135 public void setDest(String dest) 136 { 137 this.dest = dest; 138 } 139 140 public void setLog(String log) 141 { 142 this.log = log; 143 } 144 145 public void setWarn(String warn) 146 { 147 this.warn = warn; 148 } 149 150 public void setSummary(String summary) 151 { 152 this.summary = summary; 153 } 154 155 public static void stripDuplicateAttributes(Node node, Node parent) 157 { 158 159 switch (node.getNodeType()) 161 { 162 163 case Node.DOCUMENT_NODE : 164 { 165 Document doc = ( Document ) node; 166 Node child = doc.getFirstChild(); 167 168 while (child != null) 169 { 170 stripDuplicateAttributes(child, node); 171 child = child.getNextSibling(); 172 } 173 break; 174 } 175 case Node.ELEMENT_NODE : 176 { 177 Element elt = ( Element ) node; 178 NamedNodeMap attrs = elt.getAttributes(); 179 ArrayList nodesToRemove = new ArrayList (); 180 int nodesToRemoveNum = 0; 181 182 for (int i = 0; i < attrs.getLength(); i++) 183 { 184 Node a = attrs.item(i); 185 186 for (int j = 0; j < attrs.getLength(); j++) 187 { 188 Node b = attrs.item(j); 189 190 if ((i != j) 192 && (a.getNodeName().equals(b.getNodeName()))) 193 { 194 nodesToRemove.add(b); 195 nodesToRemoveNum++; 196 } 197 } 198 } 199 for (int i = 0; i < nodesToRemoveNum; i++) 200 { 201 org.w3c.dom.Attr nodeToDelete = 202 ( org.w3c.dom.Attr ) nodesToRemove.get(i); 203 org.w3c.dom.Element nodeToDeleteParent = 204 ( org.w3c.dom 205 .Element ) node; 207 nodeToDeleteParent.removeAttributeNode(nodeToDelete); 208 } 209 nodesToRemove.clear(); 210 Node child = elt.getFirstChild(); 211 212 while (child != null) 213 { 214 stripDuplicateAttributes(child, node); 215 child = child.getNextSibling(); 216 } 217 break; 218 } 219 default : 220 221 break; 223 } 224 } 225 } 226 227 280 | Popular Tags |