Ignore:
Timestamp:
2016-02-18T17:39:58+01:00 (5 years ago)
Author:
bastiK
Message:

move preference writing to new class; some refactoring

Location:
trunk/src/org/openstreetmap/josm/data/preferences
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/preferences/PreferencesReader.java

    r9822 r9823  
    4141
    4242    private final SortedMap<String, Setting<?>> settings = new TreeMap<>();
     43    private XMLStreamReader parser;
    4344    private int version = 0;
    44     private XMLStreamReader parser;
     45    private Reader reader;
     46    private File file;
    4547
    4648    private final boolean defaults;
     
    4850    /**
    4951     * Constructs a new {@code PreferencesReader}.
     52     * @param file the file
    5053     * @param defaults true when reading from the cache file for default preferences,
    5154     * false for the regular preferences config file
    52      */
    53     public PreferencesReader(boolean defaults) {
     55     * @throws IOException if any I/O error occurs
     56     * @throws XMLStreamException if any XML stream error occurs
     57     */
     58    public PreferencesReader(File file, boolean defaults) throws IOException, XMLStreamException {
    5459        this.defaults = defaults;
     60        this.reader = null;
     61        this.file = file;
     62    }
     63
     64    /**
     65     * Constructs a new {@code PreferencesReader}.
     66     * @param reader the {@link Reader}
     67     * @param defaults true when reading from the cache file for default preferences,
     68     * false for the regular preferences config file
     69     * @throws XMLStreamException if any XML stream error occurs
     70     */
     71    public PreferencesReader(Reader reader, boolean defaults) throws XMLStreamException {
     72        this.defaults = defaults;
     73        this.reader = reader;
     74        this.file = null;
    5575    }
    5676
     
    82102
    83103    /**
    84      * Parse preferences XML.
    85      * @param f the file
    86      * @throws IOException if any I/O error occurs
    87      * @throws XMLStreamException if any XML stream error occurs
    88      */
    89     public void fromXML(File f) throws IOException, XMLStreamException {
    90         try (BufferedReader in = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8)) {
    91             fromXML(in);
    92         }
    93     }
    94 
    95     /**
    96      * Parse preferences XML.
    97      * @param in the {@link Reader}
    98      * @throws XMLStreamException if any XML stream error occurs
    99      */
    100     public void fromXML(Reader in) throws XMLStreamException {
    101         this.parser = XMLInputFactory.newInstance().createXMLStreamReader(in);
    102         parse();
    103     }
    104 
    105     /**
    106104     * Return the parsed preferences as a settings map
    107105     * @return the parsed preferences as a settings map
     
    120118    }
    121119
    122     private void parse() throws XMLStreamException {
     120
     121    public void parse() throws XMLStreamException, IOException {
     122        if (reader != null) {
     123            this.parser = XMLInputFactory.newInstance().createXMLStreamReader(reader);
     124            doParse();
     125        } else {
     126            try (BufferedReader in = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
     127                this.parser = XMLInputFactory.newInstance().createXMLStreamReader(in);
     128                doParse();
     129            }
     130        }
     131    }
     132
     133    private void doParse() throws XMLStreamException {
    123134        int event = parser.getEventType();
    124135        while (true) {
Note: See TracChangeset for help on using the changeset viewer.