001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.xml;
016    
017    import java.util.HashSet;
018    import java.util.Set;
019    
020    import org.xml.sax.Attributes;
021    import org.xml.sax.ContentHandler;
022    import org.xml.sax.Locator;
023    
024    /**
025     * @author Zsolt Berentey
026     */
027    public class ElementHandler implements ContentHandler {
028    
029            public ElementHandler(
030                    ElementProcessor elementProcessor, String[] triggers) {
031    
032                    _elementProcessor = elementProcessor;
033    
034                    for (String trigger : triggers) {
035                            _triggers.add(trigger);
036                    }
037            }
038    
039            @Override
040            public void characters(char[] chars, int start, int length) {
041                    if (_element != null) {
042                            _element.setText(new String(chars, start, length));
043                    }
044            }
045    
046            @Override
047            public void endDocument() {
048            }
049    
050            @Override
051            public void endElement(String uri, String localName, String qName) {
052                    if (_element != null) {
053                            _elementProcessor.processElement(_element);
054    
055                            _element = null;
056                    }
057            }
058    
059            @Override
060            public void endPrefixMapping(String prefix) {
061            }
062    
063            @Override
064            public void ignorableWhitespace(char[] chars, int start, int length) {
065            }
066    
067            @Override
068            public void processingInstruction(String target, String data) {
069            }
070    
071            @Override
072            public void setDocumentLocator(Locator locator) {
073            }
074    
075            @Override
076            public void skippedEntity(String name) {
077            }
078    
079            @Override
080            public void startDocument() {
081            }
082    
083            @Override
084            public void startElement(
085                    String uri, String localName, String qName, Attributes attributes) {
086    
087                    if (_element != null) {
088                            _elementProcessor.processElement(_element);
089    
090                            _element = null;
091                    }
092    
093                    if (!_triggers.contains(localName)) {
094                            return;
095                    }
096    
097                    Element element = SAXReaderUtil.createElement(qName);
098    
099                    for (int i = 0; i < attributes.getLength(); i++) {
100                            element.addAttribute(
101                                    attributes.getQName(i), attributes.getValue(i));
102                    }
103    
104                    _element = element;
105            }
106    
107            @Override
108            public void startPrefixMapping(String prefix, String uri) {
109            }
110    
111            private Element _element;
112            private ElementProcessor _elementProcessor;
113            private Set<String> _triggers = new HashSet<String>();
114    
115    }