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.ArrayList;
018    import java.util.List;
019    
020    /**
021     * @author Marcellus Tavares
022     */
023    public abstract class BaseVisitor<T> implements Visitor<T> {
024    
025            @Override
026            public T visitAttribute(Attribute attribute) {
027                    return handleAttribute(attribute);
028            }
029    
030            @Override
031            public T visitCDATA(CDATA cdata) {
032                    return handleCDATA(cdata);
033            }
034    
035            @Override
036            public T visitComment(Comment comment) {
037                    return handleComment(comment);
038            }
039    
040            @Override
041            public T visitDocument(Document document) {
042                    List<T> nodeResults = new ArrayList<T>(document.nodeCount());
043    
044                    for (int i = 0, size = document.nodeCount(); i < size; i++) {
045                            Node node = document.node(i);
046    
047                            T nodeResult = node.accept(this);
048    
049                            nodeResults.add(nodeResult);
050                    }
051    
052                    return handleDocument(document, nodeResults);
053            }
054    
055            @Override
056            public T visitElement(Element element) {
057                    List<Attribute> attributes = element.attributes();
058    
059                    List<T> attributeResults = new ArrayList<T>(attributes.size());
060    
061                    for (int i = 0, size = element.attributeCount(); i < size; i++) {
062                            Attribute attribute = element.attribute(i);
063    
064                            T atrributeResult = attribute.accept(this);
065    
066                            attributeResults.add(atrributeResult);
067                    }
068    
069                    List<T> nodeResults = new ArrayList<T>(element.nodeCount());
070    
071                    for (int i = 0, size = element.nodeCount(); i < size; i++) {
072                            Node node = element.node(i);
073    
074                            T nodeResult = node.accept(this);
075    
076                            if (nodeResult != null) {
077                                    nodeResults.add(nodeResult);
078                            }
079                    }
080    
081                    return handleElement(element, attributeResults, nodeResults);
082            }
083    
084            @Override
085            public T visitEntity(Entity entity) {
086                    return handleEntity(entity);
087            }
088    
089            @Override
090            public T visitNamespace(Namespace namespace) {
091                    return handleNamespace(namespace);
092            }
093    
094            @Override
095            public T visitNode(Node node) {
096                    return handleNode(node);
097            }
098    
099            @Override
100            public T visitProcessInstruction(
101                    ProcessingInstruction processingInstruction) {
102    
103                    return handleProcessInstruction(processingInstruction);
104            }
105    
106            @Override
107            public T visitText(Text text) {
108                    return handleText(text);
109            }
110    
111            protected abstract T handleAttribute(Attribute attribute);
112    
113            protected abstract T handleCDATA(CDATA cdata);
114    
115            protected abstract T handleComment(Comment comment);
116    
117            protected abstract T handleDocument(Document document, List<T> nodeResults);
118    
119            protected abstract T handleElement(
120                    Element element, List<T> attributeResults, List<T> nodeResults);
121    
122            protected abstract T handleEntity(Entity entity);
123    
124            protected abstract T handleNamespace(Namespace namespace);
125    
126            protected abstract T handleNode(Node node);
127    
128            protected abstract T handleProcessInstruction(
129                    ProcessingInstruction processingInstruction);
130    
131            protected abstract T handleText(Text text);
132    
133    }