001: /*
002: Copyright (c) 2007, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.binding.generator;
030:
031: import java.util.HashSet;
032: import java.util.Iterator;
033: import java.util.Set;
034:
035: import org.jibx.schema.attributes.FormChoiceAttribute;
036: import org.jibx.schema.elements.ImportElement;
037: import org.jibx.schema.elements.SchemaElement;
038:
039: /**
040: * External data for a schema definition. This tracks references to other
041: * schemas, along with the associated namespace information. The {@link
042: * #fixReferences()} method actually generates the includes.
043: */
044: public class SchemaHolder extends HolderBase {
045: /** Actual schema definition. */
046: private final SchemaElement m_schema;
047:
048: /** Set of type names defined in schema. */
049: private final UniqueNameSet m_typeNameSet;
050:
051: /** Set of element names defined in schema (also used for
052: group/attributeGroup). */
053: private final UniqueNameSet m_elementNameSet;
054:
055: /** Set of schemas imported into this schema. */
056: private Set m_fixedSet;
057:
058: /**
059: * Constructor.
060: *
061: * @param uri (<code>null</code> if no-namespace schema)
062: */
063: public SchemaHolder(String uri) {
064: super (uri);
065: m_schema = new SchemaElement();
066: m_typeNameSet = new UniqueNameSet();
067: m_elementNameSet = new UniqueNameSet();
068: if (uri != null) {
069: m_schema
070: .setElementFormDefault(FormChoiceAttribute.QUALIFIED_FORM);
071: m_schema.setTargetNamespace(uri);
072: m_schema.addNamespaceDeclaration("tns", uri);
073: }
074: }
075:
076: /**
077: * Get the schema definition.
078: *
079: * @return definition
080: */
081: public SchemaElement getSchema() {
082: return m_schema;
083: }
084:
085: /**
086: * Add type name to set defined. This assures uniqueness of the name used,
087: * if necessary modifying the supplied base name to a unique alternative.
088: *
089: * @param base name to try adding
090: * @return name to be used for type
091: */
092: public String addTypeName(String base) {
093: return m_typeNameSet.add(base);
094: }
095:
096: /**
097: * Add element name to set defined. This assures uniqueness of the name
098: * used, if necessary modifying the supplied base name to a unique
099: * alternative. The same set of names is also used for groups and
100: * attributeGroups, even though these name sets are separate in schema
101: * terms. Doing things this way avoids the possibility of an element name
102: * matching a group name with the two representing different structures.
103: *
104: * @param base name to try adding
105: * @return name to be used for element
106: */
107: public String addElementName(String base) {
108: return m_elementNameSet.add(base);
109: }
110:
111: /* (non-Javadoc)
112: * @see org.jibx.binding.generator.HolderBase#addNamespaceDecl(java.lang.String, java.lang.String)
113: */
114: protected void addNamespaceDecl(String prefix, String uri) {
115: m_schema.addNamespaceDeclaration(prefix, uri);
116: }
117:
118: /* (non-Javadoc)
119: * @see org.jibx.binding.generator.HolderBase#fixReferences()
120: */
121: public void fixReferences() {
122: if (m_fixedSet == null) {
123: m_fixedSet = new HashSet();
124: }
125: for (Iterator iter = getReferences().iterator(); iter.hasNext();) {
126: SchemaHolder holder = (SchemaHolder) iter.next();
127: if (!m_fixedSet.contains(holder)) {
128: String ns = holder.getNamespace();
129: ImportElement imp = new ImportElement();
130: imp.setLocation(holder.getFileName());
131: imp.setNamespace(ns);
132: m_schema.getSchemaChildren().add(imp);
133: getPrefix(ns);
134: m_fixedSet.add(holder);
135: }
136: }
137: }
138: }
|