/***************************************************************************** * This file is part of Rinzo * * Author: Claudio Cancinos * WWW: https://sourceforge.net/projects/editorxml * Copyright (C): 2008, Claudio Cancinos * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; If not, see <http://www.gnu.org/licenses/> ****************************************************************************/ package ar.com.tadp.xml.rinzo.core.model.tags.xsd; import java.net.URI; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import ar.com.tadp.xml.rinzo.core.model.XMLNode; import ar.com.tadp.xml.rinzo.core.model.tags.AttributeDefinition; import ar.com.tadp.xml.rinzo.core.model.tags.OnlyNameTypeTagDefinition; import ar.com.tadp.xml.rinzo.core.model.tags.TagTypeDefinition; import ar.com.tadp.xml.rinzo.core.model.tags.XMLTagDefinitionProvider; /** * @author ccancino * */ public class CompositeXMLTagDefinitionProvider implements XMLTagDefinitionProvider { private Collection<XMLTagDefinitionProvider> tagDefinitionProviders = new ArrayList<XMLTagDefinitionProvider>(); public TagTypeDefinition getTagDefinition(XMLNode node) { TagTypeDefinition tagDefinition = null; if (node.isRoot()) { return this.getAllInRoot(node); } CollectionTagTypeDefinition definitions = new CollectionTagTypeDefinition(node); for (XMLTagDefinitionProvider definition : this.tagDefinitionProviders) { tagDefinition = definition.getTagDefinition(node); definitions.addInnerTags(tagDefinition.getInnerTags()); if (tagDefinition != null && !(tagDefinition instanceof OnlyNameTypeTagDefinition) && !(tagDefinition instanceof XSDPossibleRootsTagTypeDefinition)) { definitions.addAttributes(tagDefinition.getAttributes()); } } return definitions; } private TagTypeDefinition getAllInRoot(XMLNode node) { // TODO This implementation is not correct. // because it should delegate on the tag's associated provider // in order to use the correct behavior.Like displaying tag's // documentation CollectionTagTypeDefinition definition = new CollectionTagTypeDefinition(node); for (XMLTagDefinitionProvider provider : this.tagDefinitionProviders) { definition.addInnerTags(provider.getTagDefinition(node).getInnerTags()); } return definition; } public void addTagDefinitionProvider(XMLTagDefinitionProvider provider) { this.tagDefinitionProviders.add(provider); } public void setDefinition(String fileName, Collection<URI> uris) { Iterator<URI> it = uris.iterator(); for (Iterator<XMLTagDefinitionProvider> iterator = this.tagDefinitionProviders.iterator(); iterator.hasNext() && it.hasNext();) { try { XSDTagDefinitionProvider provider = (XSDTagDefinitionProvider) iterator.next(); provider.setDefinition(fileName, it.next()); } catch (Exception e) { e.printStackTrace(); } } } /** * @deprecated use setDefinition */ public void setSchemas(Collection<URI> uris) { Iterator<URI> it = uris.iterator(); for (Iterator<XMLTagDefinitionProvider> iterator = this.tagDefinitionProviders.iterator(); iterator.hasNext() && it.hasNext();) { try { XSDTagDefinitionProvider provider = (XSDTagDefinitionProvider) iterator.next(); provider.setSchema(it.next()); } catch (Exception e) { e.printStackTrace(); } } } /** * @deprecated use setDefinition */ public void setFileName(String fileName) { for (Iterator<XMLTagDefinitionProvider> iterator = this.tagDefinitionProviders.iterator(); iterator.hasNext();) { XSDTagDefinitionProvider provider = (XSDTagDefinitionProvider) iterator.next(); provider.setFileName(fileName); } } private static class CollectionTagTypeDefinition implements TagTypeDefinition { private Collection<TagTypeDefinition> innerTags = new ArrayList<TagTypeDefinition>(); private Collection<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>(); private XMLNode node; public CollectionTagTypeDefinition(XMLNode node) { this.node = node; } public AttributeDefinition getAttribute(String attributeName) { for (Iterator<AttributeDefinition> iterator = this.attributes.iterator(); iterator.hasNext();) { AttributeDefinition currentAttribute = iterator.next(); if(attributeName.startsWith(currentAttribute.getName())) { return currentAttribute; } } return null; } public void addAttributes(Collection<AttributeDefinition> attributes) { this.attributes = attributes; } public Collection<AttributeDefinition> getAttributes() { return this.attributes; } public String getComment() { return null; } public void addInnerTags(Collection<TagTypeDefinition> tags) { this.innerTags.addAll(tags); } public Collection<TagTypeDefinition> getInnerTags() { return this.innerTags; } public String getName() { return node.getTagName(); } public String getNamespace() { return node.getNamespace(); } } }