Changeset 2642 in josm


Ignore:
Timestamp:
2009-12-16T19:35:50+01:00 (14 years ago)
Author:
Gubaer
Message:

fixed #4151: Please provide line number in presets parsing error

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/XmlObjectParser.java

    r2017 r2642  
    11// License: GPL. Copyright 2007 by Immanuel Scholz and others
    22package org.openstreetmap.josm.tools;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
    35
    46import java.io.Reader;
     
    1820import org.xml.sax.Attributes;
    1921import org.xml.sax.InputSource;
     22import org.xml.sax.Locator;
    2023import org.xml.sax.SAXException;
     24import org.xml.sax.SAXParseException;
    2125import org.xml.sax.helpers.DefaultHandler;
    2226
     
    2731 */
    2832public 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    }
    2980
    3081    public static final String lang = LanguageInfo.getLanguageCodeXML();
     
    53104        Stack<Object> current = new Stack<Object>();
    54105        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
    55122        @Override public void startElement(String ns, String lname, String qname, Attributes a) throws SAXException {
    56123            if (mapping.containsKey(qname)) {
     
    59126                    current.push(klass.newInstance());
    60127                } 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) {
    64131                    setValue(a.getQName(i), a.getValue(i));
    65                 if (mapping.get(qname).onStart)
     132                }
     133                if (mapping.get(qname).onStart) {
    66134                    report();
     135                }
    67136                if (mapping.get(qname).both)
    68137                {
     
    75144        }
    76145        @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) {
    78147                report();
    79             else if (characters != null && !current.isEmpty()) {
     148            } else if (characters != null && !current.isEmpty()) {
    80149                setValue(qname, characters.trim());
    81150                characters = "";
     
    106175
    107176        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")) {
    109178                fieldName += "_";
     179            }
    110180            try {
    111181                Object c = current.peek();
     
    124194                    }
    125195                }
    126                 if (f != null && Modifier.isPublic(f.getModifiers()))
     196                if (f != null && Modifier.isPublic(f.getModifiers())) {
    127197                    f.set(c, getValueForClass(f.getType(), value));
    128                 else {
     198                } else {
    129199                    if(fieldName.startsWith(lang))
    130200                    {
     
    146216            } catch (Exception e) {
    147217                e.printStackTrace(); // SAXException does not dump inner exceptions.
    148                 throw new SAXException(e);
    149             }
    150         }
     218                throwException(e);
     219            }
     220        }
     221
    151222        private boolean parseBoolean(String s) {
    152223            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);
    157238        }
    158239    }
Note: See TracChangeset for help on using the changeset viewer.