Changeset 2642 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2009-12-16T19:35:50+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/XmlObjectParser.java
r2017 r2642 1 1 // License: GPL. Copyright 2007 by Immanuel Scholz and others 2 2 package org.openstreetmap.josm.tools; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 3 5 4 6 import java.io.Reader; … … 18 20 import org.xml.sax.Attributes; 19 21 import org.xml.sax.InputSource; 22 import org.xml.sax.Locator; 20 23 import org.xml.sax.SAXException; 24 import org.xml.sax.SAXParseException; 21 25 import org.xml.sax.helpers.DefaultHandler; 22 26 … … 27 31 */ 28 32 public class XmlObjectParser implements Iterable<Object> { 33 public class PresetParsingException extends SAXException { 34 private int columnNumber; 35 private int lineNumber; 36 37 public PresetParsingException() { 38 super(); 39 } 40 41 public PresetParsingException(Exception e) { 42 super(e); 43 } 44 45 public PresetParsingException(String message, Exception e) { 46 super(message, e); 47 } 48 49 public PresetParsingException(String message) { 50 super(message); 51 } 52 53 public PresetParsingException rememberLocation(Locator locator) { 54 if (locator == null) return this; 55 this.columnNumber = locator.getColumnNumber(); 56 this.lineNumber = locator.getLineNumber(); 57 return this; 58 } 59 60 @Override 61 public String getMessage() { 62 String msg = super.getMessage(); 63 if (lineNumber == 0 && columnNumber == 0) 64 return msg; 65 if (msg == null) { 66 msg = getClass().getName(); 67 } 68 msg = msg + " " + tr("(at line {0}, column {1})", lineNumber, columnNumber); 69 return msg; 70 } 71 72 public int getColumnNumber() { 73 return columnNumber; 74 } 75 76 public int getLineNumber() { 77 return lineNumber; 78 } 79 } 29 80 30 81 public static final String lang = LanguageInfo.getLanguageCodeXML(); … … 53 104 Stack<Object> current = new Stack<Object>(); 54 105 String characters = ""; 106 107 private Locator locator; 108 109 @Override 110 public void setDocumentLocator(Locator locator) { 111 this.locator = locator; 112 } 113 114 protected void throwException(String msg) throws PresetParsingException{ 115 throw new PresetParsingException(msg).rememberLocation(locator); 116 } 117 118 protected void throwException(Exception e) throws PresetParsingException{ 119 throw new PresetParsingException(e).rememberLocation(locator); 120 } 121 55 122 @Override public void startElement(String ns, String lname, String qname, Attributes a) throws SAXException { 56 123 if (mapping.containsKey(qname)) { … … 59 126 current.push(klass.newInstance()); 60 127 } catch (Exception e) { 61 throw new SAXException(e);62 } 63 for (int i = 0; i < a.getLength(); ++i) 128 throwException(e); 129 } 130 for (int i = 0; i < a.getLength(); ++i) { 64 131 setValue(a.getQName(i), a.getValue(i)); 65 if (mapping.get(qname).onStart) 132 } 133 if (mapping.get(qname).onStart) { 66 134 report(); 135 } 67 136 if (mapping.get(qname).both) 68 137 { … … 75 144 } 76 145 @Override public void endElement(String ns, String lname, String qname) throws SAXException { 77 if (mapping.containsKey(qname) && !mapping.get(qname).onStart) 146 if (mapping.containsKey(qname) && !mapping.get(qname).onStart) { 78 147 report(); 79 else if (characters != null && !current.isEmpty()) {148 } else if (characters != null && !current.isEmpty()) { 80 149 setValue(qname, characters.trim()); 81 150 characters = ""; … … 106 175 107 176 private void setValue(String fieldName, String value) throws SAXException { 108 if (fieldName.equals("class") || fieldName.equals("default") || fieldName.equals("throw") || fieldName.equals("new") || fieldName.equals("null")) 177 if (fieldName.equals("class") || fieldName.equals("default") || fieldName.equals("throw") || fieldName.equals("new") || fieldName.equals("null")) { 109 178 fieldName += "_"; 179 } 110 180 try { 111 181 Object c = current.peek(); … … 124 194 } 125 195 } 126 if (f != null && Modifier.isPublic(f.getModifiers())) 196 if (f != null && Modifier.isPublic(f.getModifiers())) { 127 197 f.set(c, getValueForClass(f.getType(), value)); 128 else {198 } else { 129 199 if(fieldName.startsWith(lang)) 130 200 { … … 146 216 } catch (Exception e) { 147 217 e.printStackTrace(); // SAXException does not dump inner exceptions. 148 throw new SAXException(e); 149 } 150 } 218 throwException(e); 219 } 220 } 221 151 222 private boolean parseBoolean(String s) { 152 223 return s != null && 153 !s.equals("0") && 154 !s.startsWith("off") && 155 !s.startsWith("false") && 156 !s.startsWith("no"); 224 !s.equals("0") && 225 !s.startsWith("off") && 226 !s.startsWith("false") && 227 !s.startsWith("no"); 228 } 229 230 @Override 231 public void error(SAXParseException e) throws SAXException { 232 throwException(e); 233 } 234 235 @Override 236 public void fatalError(SAXParseException e) throws SAXException { 237 throwException(e); 157 238 } 158 239 }
Note:
See TracChangeset
for help on using the changeset viewer.