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.HashMap;
032: import java.util.Iterator;
033: import java.util.Map;
034:
035: import org.jibx.binding.model.IClassLocator;
036:
037: /**
038: * Package customization information.
039: */
040: public class PackageCustom extends NestingBase implements IApply {
041: /** Element name in XML customization file. */
042: public static final String ELEMENT_NAME = "package";
043:
044: // values specific to package level
045: private String m_simpleName;
046: private String m_fullName;
047:
048: // flag for namespace derivation done (from apply() method)
049: private boolean m_fixedNamespace;
050:
051: // map from simple name to class information for classes in package
052: private Map m_classMap;
053:
054: /**
055: * Constructor. This has package access so that it can be used from the
056: * {@link GlobalCustom} class.
057: *
058: * @param simple simple package name
059: * @param full fully-qualified package name
060: * @param parent
061: */
062: /*package*/PackageCustom(String simple, String full,
063: NestingBase parent) {
064: super (parent);
065: m_simpleName = simple;
066: m_fullName = full;
067: m_classMap = new HashMap();
068: }
069:
070: /**
071: * Get fully-qualified package name.
072: *
073: * @return package name (empty string if default package)
074: */
075: public String getName() {
076: return m_fullName;
077: }
078:
079: /**
080: * Get existing information for class in this package.
081: *
082: * @param name simple class name (without package)
083: * @return class information (<code>null</code> if no existing information)
084: */
085: public ClassCustom getClassCustomization(String name) {
086: return (ClassCustom) m_classMap.get(name);
087: }
088:
089: /**
090: * Add information for class in this package. This just creates the basic
091: * class information structure and returns it, without populating the class
092: * details.
093: *
094: * @param name simple class name (without package)
095: * @return class information (<code>null</code> if no existing information)
096: */
097: /*package*/ClassCustom addClassCustomization(String name) {
098: ClassCustom clas = new ClassCustom(this , name);
099: m_classMap.put(name, clas);
100: return clas;
101: }
102:
103: /**
104: * Apply customizations to default values. This fills in the information for
105: * classes in this package by deriving information for fields or properties
106: * in each class. This is intended to be used once, after customizations
107: * have been unmarshalled.
108: *
109: * @param loc class locator
110: */
111: public void apply(IClassLocator loc) {
112:
113: // derive namespace from parent setting, if not specified
114: String ns = getSpecifiedNamespace();
115: if (ns == null) {
116: SharedNestingBase parent = getParent();
117: if (parent instanceof PackageCustom
118: && !((PackageCustom) parent).m_fixedNamespace) {
119: ((PackageCustom) parent).apply(loc);
120: }
121: ns = deriveNamespace(parent.getNamespace(), m_fullName,
122: getNamespaceStyle());
123: }
124: setNamespace(ns);
125: m_fixedNamespace = true;
126:
127: // apply customizations for all classes
128: for (Iterator iter = m_classMap.values().iterator(); iter
129: .hasNext();) {
130: ((ClassCustom) iter.next()).apply(loc);
131: }
132: }
133: }
|