001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2014 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.generator.gsp.token;
023
024import org.granite.generator.gsp.ParseException;
025
026import java.io.StringReader;
027import java.io.IOException;
028
029import java.util.Map;
030import java.util.HashMap;
031
032import org.xml.sax.Attributes;
033import org.xml.sax.SAXException;
034import org.xml.sax.InputSource;
035import org.xml.sax.helpers.DefaultHandler;
036
037import javax.xml.parsers.SAXParserFactory;
038import javax.xml.parsers.ParserConfigurationException;
039
040/**
041 * @author Franck WOLFF
042 */
043public class Directive extends Token {
044
045    String name = null;
046    final Map<String, String> attributes = new HashMap<String, String>();
047
048    public Directive(int index, String content) throws ParseException {
049        super(index, content);
050        try {
051            parse(content);
052        } catch (Exception e) {
053            throw new ParseException(e);
054        }
055    }
056
057    public String getName() {
058        return name;
059    }
060
061    public Map<String, String> getAttributes() {
062        return attributes;
063    }
064
065    private void parse(String content)
066        throws SAXException, ParserConfigurationException, IOException {
067        InputSource is = new InputSource(new StringReader('<' + content + "/>"));
068        SAXParserFactory.newInstance().newSAXParser().parse(is, new Handler());
069    }
070
071    @Override
072    public String toString() {
073        return this.getClass().getName() + '[' + name + ", " + attributes + ']';
074    }
075
076    class Handler extends DefaultHandler {
077        @Override
078        public void startElement(String namespaceURI,
079                                 String localName,
080                                 String qName,
081                                 Attributes atts)
082            throws SAXException {
083            if (name != null)
084                throw new SAXException("illegal nested element: " + qName);
085            name = qName;
086            for (int i = 0; i < atts.getLength(); i++)
087                attributes.put(atts.getQName(i), atts.getValue(i));
088        }
089    }
090}