1 package jdiff; 2 3 import java.io.*; 4 import java.util.*; 5 6 7 import org.xml.sax.Attributes ; 8 import org.xml.sax.SAXException ; 9 import org.xml.sax.SAXParseException ; 10 import org.xml.sax.XMLReader ; 11 import org.xml.sax.helpers.DefaultHandler ; 12 13 26 class CommentsHandler extends DefaultHandler { 27 28 29 public Comments comments_ = null; 30 31 32 private List currSingleComment_ = null; 34 35 private boolean inText = false; 36 37 38 private String currentText = null; 39 40 41 private LinkedList tagStack = null; 42 43 44 public CommentsHandler(Comments comments) { 45 comments_ = comments; 46 tagStack = new LinkedList(); 47 } 48 49 public void startDocument() { 50 } 51 52 public void endDocument() { 53 if (trace) 54 comments_.dump(); 55 } 56 57 public void startElement(java.lang.String uri, java.lang.String localName, 58 java.lang.String qName, Attributes attributes) { 59 if (localName.equals("")) 61 localName = qName; 62 if (localName.compareTo("comments") == 0) { 63 String commentsName = attributes.getValue("name"); 64 String version = attributes.getValue("jdversion"); if (commentsName == null) { 66 System.out.println("Error: no identifier found in the comments XML file."); 67 System.exit(3); 68 } 69 int idx1 = JDiff.oldFileName.lastIndexOf('.'); 71 int idx2 = JDiff.newFileName.lastIndexOf('.'); 72 String filename2 = JDiff.oldFileName.substring(0, idx1) + 73 "_to_" + JDiff.newFileName.substring(0, idx2); 74 if (filename2.compareTo(commentsName) != 0) { 75 System.out.println("Warning: API identifier in the comments XML file (" + filename2 + ") differs from the name of the file."); 76 } 77 } else if (localName.compareTo("comment") == 0) { 78 currSingleComment_ = new ArrayList(); } else if (localName.compareTo("identifier") == 0) { 80 String id = attributes.getValue("id"); 82 SingleComment newComment = new SingleComment(id, null); 83 currSingleComment_.add(newComment); 85 } else if (localName.compareTo("text") == 0) { 86 inText = true; 87 currentText = null; 88 } else { 89 if (inText) { 90 addStartTagToText(localName, attributes); 92 } else { 93 System.out.println("Error: unknown element type: " + localName); 94 System.exit(-1); 95 } 96 } 97 } 98 99 public void endElement(java.lang.String uri, java.lang.String localName, 100 java.lang.String qName) { 101 if (localName.equals("")) 102 localName = qName; 103 if (localName.compareTo("text") == 0) { 104 inText = false; 105 addTextToComments(); 106 } else if (inText) { 107 addEndTagToText(localName); 108 } 109 110 } 111 112 113 public void characters(char[] ch, int start, int length) { 114 if (inText) { 115 String chunk = new String (ch, start, length); 116 if (currentText == null) 117 currentText = chunk; 118 else 119 currentText += chunk; 120 } 121 } 122 123 127 public void addTextToComments() { 128 currentText = currentText.trim(); 130 if (!currentText.endsWith(".") && 132 !currentText.endsWith("?") && 133 !currentText.endsWith("!") && 134 currentText.compareTo(Comments.placeHolderText) != 0) { 135 System.out.println("Warning: text of comment does not end in a period: " + currentText); 136 } 137 Iterator iter = currSingleComment_.iterator(); 139 while (iter.hasNext()) { 140 SingleComment currComment = (SingleComment)(iter.next()); 141 if (currComment.text_ == null) 142 currComment.text_ = currentText; 143 else 144 currComment.text_ += currentText; 145 comments_.addComment(currComment); 146 } 147 } 148 149 152 public void addStartTagToText(String localName, Attributes attributes) { 153 String currentHTMLTag = localName; 155 tagStack.add(currentHTMLTag); 157 String tag = "<" + currentHTMLTag; 158 int len = attributes.getLength(); 160 for (int i = 0; i < len; i++) { 161 String name = attributes.getLocalName(i); 162 String value = attributes.getValue(i); 163 tag += " " + name + "=\"" + value+ "\""; 164 } 165 166 if (Comments.isMinimizedTag(currentHTMLTag)) { 168 tag += "/>"; 169 } else { 170 tag += ">"; 171 } 172 if (currentText == null) 174 currentText = tag; 175 else 176 currentText += tag; 177 } 178 179 182 public void addEndTagToText(String localName) { 183 String currentHTMLTag = (String )(tagStack.removeLast()); 185 if (!Comments.isMinimizedTag(currentHTMLTag)) 186 currentText += "</" + currentHTMLTag + ">"; 187 } 188 189 public void warning(SAXParseException e) { 190 System.out.println("Warning (" + e.getLineNumber() + "): parsing XML comments file:" + e); 191 e.printStackTrace(); 192 } 193 194 public void error(SAXParseException e) { 195 System.out.println("Error (" + e.getLineNumber() + "): parsing XML comments file:" + e); 196 e.printStackTrace(); 197 System.exit(1); 198 } 199 200 public void fatalError(SAXParseException e) { 201 System.out.println("Fatal Error (" + e.getLineNumber() + "): parsing XML comments file:" + e); 202 e.printStackTrace(); 203 System.exit(1); 204 } 205 206 207 private static final boolean trace = false; 208 209 } 210 211 | Popular Tags |