001/*
002 *  Copyright 2013 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.core.userpref;
017
018import java.util.Map;
019
020import org.xml.sax.Attributes;
021import org.xml.sax.SAXException;
022import org.xml.sax.helpers.DefaultHandler;
023
024/**
025 * This class is intended to be use as a simple helper to construct Maps from
026 * SAX events. <br>
027 * The incoming SAX document must follow this structure :<br>
028 * 
029 * &lt;UserPreferences&gt; <br>
030 * &nbsp;&nbsp;&lt;Name1&gt;value1&lt;/Name1&gt; <br>
031 * &nbsp;&nbsp;&lt;Name2&gt;value2&lt;/Name2&gt; <br>
032 * &nbsp;&nbsp;... <br>
033 * &lt;/UserPreferences&gt; <br>
034 * <br>
035 * 
036 * or<br>
037 * &lt;UserPreferences&gt; <br>
038 * &nbsp;&nbsp;&lt;preference id="Name1"&gt;value1&lt;/preference&gt; <br>
039 * &nbsp;&nbsp;&lt;preference id="Name1"&gt;value2&lt;/preference&gt; <br>
040 * &nbsp;&nbsp;... <br>
041 * &lt;/UserPreferences&gt; <br>
042 * <br>
043 * Each pair Name/Value will be put (as Strings) in the constructed Map <br>
044 */
045public class UserPrefsHandler extends DefaultHandler
046{
047    // The object being constructed
048    private Map<String, String> _map;
049
050    // level in the XML tree. Root is at level 1, and data at level 2
051    private int _level;
052
053    // current characters from SAX events
054    private StringBuffer _currentString;
055
056    private int _version;
057    
058    private String _userPrefId;
059    
060    /**
061     * Create a map handler
062     * @param map Map to fill
063     */
064    public UserPrefsHandler(Map<String, String> map)
065    {
066        _map = map;
067    }
068
069    @Override
070    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
071    {
072        if (_level == 0)
073        {
074            _version = attributes.getValue("version") != null ? Integer.valueOf(attributes.getValue("version")) : 1;
075        }
076            
077        _level++;
078        _currentString = new StringBuffer();
079        
080        if (_level == 2 && _version == 2)
081        {
082            _userPrefId = attributes.getValue("id");
083        }
084        
085    }
086
087    @Override
088    public void characters(char[] ch, int start, int length) throws SAXException
089    {
090        _currentString.append(ch, start, length);
091    }
092
093    @Override
094    public void endElement(String uri, String localName, String qName) throws SAXException
095    {
096        if (_level == 2)
097        {
098            // If we are at the "data" level
099            String strValue = _currentString.toString();
100            
101            if (_version == 2)
102            {
103                _map.put(_userPrefId, strValue);
104                _userPrefId = null;
105            }
106            else
107            {
108                _map.put(qName, strValue);
109            }
110            
111        }
112
113        _level--;
114    }
115}