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