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