Changeset 3612 in josm


Ignore:
Timestamp:
2010-10-16T22:08:17+02:00 (11 years ago)
Author:
stoecker
Message:

add XML checking for mappaint files

Location:
trunk
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r2990 r3612  
    66import java.io.IOException;
    77import java.io.InputStream;
     8import java.io.InputStreamReader;
    89import java.util.Collection;
    910import java.util.Collections;
     
    1617import org.openstreetmap.josm.io.MirroredInputStream;
    1718import org.openstreetmap.josm.tools.ImageProvider;
    18 import org.xml.sax.InputSource;
     19import org.openstreetmap.josm.tools.XmlObjectParser;
    1920import org.xml.sax.SAXException;
    20 import org.xml.sax.XMLReader;
    21 import org.xml.sax.helpers.XMLReaderFactory;
     21import org.xml.sax.SAXParseException;
    2222
    2323public class MapPaintStyles {
     
    8686                    a = new String[] { null, file };
    8787                }
    88                 XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    89                 ElemStyleHandler handler = new ElemStyleHandler(a[0]);
    90                 xmlReader.setContentHandler(handler);
    91                 xmlReader.setErrorHandler(handler);
     88                XmlObjectParser parser = new XmlObjectParser(new ElemStyleHandler(a[0]));
    9289                MirroredInputStream in = new MirroredInputStream(a[1]);
    9390                InputStream zip = in.getZipEntry("xml","style");
     91                InputStreamReader ins;
    9492                if(zip != null)
    9593                {
    9694                    zipIcons = in.getFile();
    97                     xmlReader.parse(new InputSource(zip));
     95                    ins = new InputStreamReader(zip);
    9896                } else {
    99                     xmlReader.parse(new InputSource(in));
     97                    ins = new InputStreamReader(in);
     98                }
     99                parser.startWithValidation(ins, "http://josm.openstreetmap.de/mappaint-styöe-1.0",
     100                "resource://data/mappaint-style.xsd");
     101                while(parser.hasNext()) {
    100102                }
    101103            } catch(IOException e) {
    102104                System.err.println(tr("Warning: failed to load Mappaint styles from ''{0}''. Exception was: {1}", a[1], e.toString()));
    103105                e.printStackTrace();
     106            } catch(SAXParseException e) {
     107                System.err.println(tr("Warning: failed to parse Mappaint styles from ''{0}''. Error was: [{1}:{2}] {3}", a[1], e.getLineNumber(), e.getColumnNumber(), e.getMessage()));
     108                e.printStackTrace();
    104109            } catch(SAXException e) {
    105                 System.err.println(tr("Warning: failed to parse Mappaint styles from ''{0}''. Exception was: {1}", a[1], e.toString()));
     110                System.err.println(tr("Warning: failed to parse Mappaint styles from ''{0}''. Error was: {1}", a[1], e.getMessage()));
    106111                e.printStackTrace();
    107112            }
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r3582 r3612  
    4040import javax.swing.JTextField;
    4141import javax.swing.SwingUtilities;
    42 import javax.xml.transform.stream.StreamSource;
    4342
    4443import org.openstreetmap.josm.Main;
     
    749748
    750749        if (validate) {
    751             parser.startWithValidation(in, "http://josm.openstreetmap.de/tagging-preset-1.0", new StreamSource(TaggingPreset.class.getResourceAsStream("/data/tagging-preset.xsd")));
     750            parser.startWithValidation(in, "http://josm.openstreetmap.de/tagging-preset-1.0", "resource://data/tagging-preset.xsd");
    752751        } else {
    753752            parser.start(in);
  • trunk/src/org/openstreetmap/josm/tools/XmlObjectParser.java

    r3385 r3612  
    55
    66import java.io.Reader;
     7import java.io.IOException;
    78import java.lang.reflect.Field;
    89import java.lang.reflect.Method;
     
    1920import javax.xml.parsers.SAXParserFactory;
    2021import javax.xml.transform.Source;
     22import javax.xml.transform.stream.StreamSource;
    2123import javax.xml.validation.Schema;
    2224import javax.xml.validation.SchemaFactory;
    2325import javax.xml.validation.ValidatorHandler;
     26
     27import org.openstreetmap.josm.io.MirroredInputStream;
    2428
    2529import org.xml.sax.Attributes;
     
    276280
    277281    private Map<String, Entry> mapping = new HashMap<String, Entry>();
    278     private Parser parser;
     282    private DefaultHandler parser;
    279283
    280284    /**
     
    297301    public XmlObjectParser() {
    298302        parser = new Parser();
     303    }
     304
     305    public XmlObjectParser(DefaultHandler handler) {
     306        parser = handler;
    299307    }
    300308
     
    328336    }
    329337
    330     public Iterable<Object> startWithValidation(final Reader in, String namespace, Source schemaSource) throws SAXException {
    331         SchemaFactory factory =  SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    332         Schema schema = factory.newSchema(schemaSource);
    333         ValidatorHandler validator = schema.newValidatorHandler();
    334         validator.setContentHandler(parser);
    335         validator.setErrorHandler(parser);
    336 
    337         AddNamespaceFilter filter = new AddNamespaceFilter(namespace);
    338         filter.setContentHandler(validator);
    339 
    340         return start(in, filter);
     338    public Iterable<Object> startWithValidation(final Reader in, String namespace, String schemaSource) throws SAXException {
     339        try {
     340            SchemaFactory factory =  SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
     341            Schema schema = factory.newSchema(new StreamSource(new MirroredInputStream(schemaSource)));
     342            ValidatorHandler validator = schema.newValidatorHandler();
     343            validator.setContentHandler(parser);
     344            validator.setErrorHandler(parser);
     345
     346            AddNamespaceFilter filter = new AddNamespaceFilter(namespace);
     347            filter.setContentHandler(validator);
     348            return start(in, filter);
     349        } catch(IOException e) {
     350            throw new SAXException(tr("Failed to load XML schema."), e);
     351        }
    341352    }
    342353
  • trunk/styles/maxspeed/elemstyles.xml

    r2091 r3612  
    1 
    2 <rules>
     1<?xml version="1.0" encoding="UTF-8"?>
     2<rules xmlns="http://josm.openstreetmap.de/mappaint-style-1.0"
     3       name="standard">
    34
    45        <rule>
  • trunk/styles/standard/elemstyles.xml

    r3611 r3612  
     1<?xml version="1.0" encoding="UTF-8"?>
     2<rules xmlns="http://josm.openstreetmap.de/mappaint-style-1.0"
     3       name="standard">
    14<!--
    25A little help:
     
    7073-->
    7174
    72 <rules name="standard">
    73 
    7475        <rule>
    7576                <condition k="restriction" v="no_left_turn"/>
  • trunk/styles/standard/elemstyles_gb.xml

    r1745 r3612  
     1<?xml version="1.0" encoding="UTF-8"?>
     2<rules xmlns="http://josm.openstreetmap.de/mappaint-style-1.0"
     3       name="standard" author="Ulf Lamping" version="1.0" shortdescription="British map overlay"
     4       description="Special rules to take differences in the british area into account (e.g. traffic signs).">
    15<!--
    26 GB specific rules
     
    812-->
    913
    10 <rules name="standard" author="Ulf Lamping" version="1.0" shortdescription="British map overlay"
    11         description="Special rules to take differences in the british area into account (e.g. traffic signs).">
    1214        <rule>
    1315                <condition k="restriction" v="no_left_turn"/>
Note: See TracChangeset for help on using the changeset viewer.