📄 bindinggenerator.java
字号:
/*Copyright (c) 2007, Dennis M. SosnoskiAll rights reserved.Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of JiBX nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FORANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ONANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.jibx.binding.generator;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.jibx.binding.model.BindingElement;import org.jibx.binding.model.CollectionElement;import org.jibx.binding.model.IClass;import org.jibx.binding.model.IncludeElement;import org.jibx.binding.model.MappingElement;import org.jibx.binding.model.NestingElementBase;import org.jibx.binding.model.StructureElement;import org.jibx.binding.model.StructureElementBase;import org.jibx.binding.model.ValueElement;import org.jibx.runtime.BindingDirectory;import org.jibx.runtime.IBindingFactory;import org.jibx.runtime.IMarshallable;import org.jibx.runtime.IMarshallingContext;import org.jibx.runtime.JiBXException;import org.jibx.runtime.QName;import org.jibx.schema.codegen.ReferenceCountMap;import org.jibx.util.InsertionOrderedMap;import org.jibx.util.Types;/** * Binding generator implementation. */public class BindingGenerator{ /** Binding generation customizations. */ private final GlobalCustom m_global; /** Set of class names to be included. */ private final Set m_includeSet; /** Set of class names to be ignored. */ private final Set m_ignoreSet; /** Set of class names referenced directly as members of other classes. */ private final Set m_directSet; /** Set of class names subclassed by other classes in binding. */ private final Set m_superSet; /** Map from fully-qualified class name to mapping details. */ private final Map m_mappingDetailsMap; /** Map from namespace URI to binding holder. */ private final InsertionOrderedMap m_uriBindingMap; /** Target package for binding code generation. */ private String m_targetPackage; /** * Create a generator based on a particular set of customizations. * * @param glob */ public BindingGenerator(GlobalCustom glob) { m_global = glob; m_includeSet = new HashSet(); m_ignoreSet = new HashSet(); m_directSet = new HashSet(); m_superSet = new HashSet(); m_mappingDetailsMap = new HashMap(); m_uriBindingMap = new InsertionOrderedMap(); } /** * Check if a class represents a simple value. * TODO: implement ClassCustom hooks for this purpose * * @param type fully qualified class name * @return <code>true</code> if simple value, <code>false</code> if not */ public boolean isValueClass(String type) { ClassCustom clas = m_global.forceClassCustomization(type); IClass sinfo = clas.getClassInformation().getSuperClass(); if (sinfo != null && "java.lang.Enum".equals(sinfo.getName())) { return true; } else { return false; } } /** * Check if a class needs to be included in the binding. This checks all * members of the class and if necessary superclasses, returning * <code>true</code> if any member is ultimately found with a simple value. * * @param type fully qualified class name * @return <code>true</code> if class to be included in binding, * <code>false</code> if it should be skipped */ public boolean checkInclude(String type) { if (m_includeSet.contains(type)) { return true; } else if (m_ignoreSet.contains(type)) { return false; } else { // check if any members are to be included boolean include = false; ClassCustom clas = m_global.forceClassCustomization(type); for (Iterator iter = clas.getMembers().iterator(); iter.hasNext();) { MemberCustom memb = (MemberCustom)iter.next(); String mtype = memb.isCollection() ? memb.getItemType() : memb.getWorkingType(); if (Types.isSimpleValue(mtype) || isValueClass(mtype) || !m_global.isKnownMapping(mtype) || checkInclude(mtype)) { include = true; break; } } if (!include) { // force superclass handling if appropriate if (clas.getMembers().size() > 0 && clas.isUseSuper()) { String stype = clas.getClassInformation(). getSuperClass().getName(); if (!"java.lang.Object".equals(stype) && checkInclude(stype)) { include = true; } } } if (include) { m_includeSet.add(type); } else { m_ignoreSet.add(type); } return include; } } /** * Expand all references from a class. This checks the types of all members * of the class, counting references and calling itself recursively for any * types which are not primitives and not java.* or javax.* classes. It also * expands the superclass, if specified by the class customizations. * * @param type fully qualified class name * @param refmap reference count map */ public void expandReferences(String type, ReferenceCountMap refmap) { if (checkInclude(type)) { // expand all references from members ClassCustom clas = m_global.forceClassCustomization(type); for (Iterator iter = clas.getMembers().iterator(); iter.hasNext();) { MemberCustom memb = (MemberCustom)iter.next(); String mtype = memb.isCollection() ? memb.getItemType() : memb.getWorkingType(); if (!Types.isSimpleValue(mtype) && !isValueClass(mtype) && !m_global.isKnownMapping(mtype)) { if ((mtype.startsWith("java.")) || (mtype.startsWith("javax."))) { throw new IllegalStateException("No way to handle type '" + mtype + '\''); } else if (checkInclude(mtype)) { if (refmap.incrementCount(mtype) <= 1) { expandReferences(mtype, refmap); } m_directSet.add(mtype); } } } // force superclass handling if appropriate if (clas.getMembers().size() > 0 && clas.isUseSuper()) { String stype = clas.getClassInformation(). getSuperClass().getName(); if (!"java.lang.Object".equals(stype) && !Types.isSimpleValue(stype) && !isValueClass(stype) && !m_global.isKnownMapping(stype) && checkInclude(stype)) { if (refmap.incrementCount(stype) <= 1) { expandReferences(stype, refmap); } m_superSet.add(stype); } } } } /** * Set creation information for structure binding component. This includes * the declared type, as well as the create type and factory method. * * @param memb * @param struct */ private void setTypes(MemberCustom memb, StructureElementBase struct) { String type = memb.getActualType(); if (type != null && !type.equals(memb.getStatedType())) { struct.setDeclaredType(type); } struct.setCreateType(memb.getCreateType()); struct.setFactoryName(memb.getFactoryMethod()); } /** * General object comparison method. Don't know why Sun hasn't seen fit to * include this somewhere, but at least it's easy to write (over and over * again). * * @param a first object to be compared * @param b second object to be compared * @return <code>true</code> if both objects are <code>null</code>, or if * <code>a.equals(b)</code>; <code>false</code> otherwise */ public static boolean isEqual(Object a, Object b) { return (a == null) ? b == null : a.equals(b); } /** * Check for dependency on another binding. * * @param uri namespace for binding of referenced component * @param hold binding holder */ private void checkDependency(String uri, BindingHolder hold) { if (!isEqual(uri, hold.getNamespace())) { BindingHolder tohold = findBinding(uri); hold.addReference(tohold); } } /** * Define the details of a collection binding. Collection bindings may be * empty (in the case where the item type is directly mapped), have a * <value> child element, or have either a mapping reference or direct * definition <structure> child element. This determines the appropriate * form based on the item type. * * @param itype * @param iname * @param coll * @param hold */ public void defineCollection(String itype, String iname, CollectionElement coll, BindingHolder hold) { // check item type handling MappingDetail detail = (MappingDetail)m_mappingDetailsMap.get(itype); if (detail != null) { ClassCustom icust = m_global.getClassCustomization(itype); if (detail.isUseAbstract() && !detail.isExtended()) { // add reference to appropriate binding QName qname = detail.getTypeQName(); hold.addReference(findBinding(qname.getUri())); // add <structure> with map-as for abstract mapping if (iname == null) { iname = icust.getElementName(); } StructureElement struct = new StructureElement(); if (qname == null) { struct.setMapAsName(itype); } else { // set the type reference struct.setMapAsQName(qname); // check dependency on other binding checkDependency(qname.getUri(), hold); } struct.setName(iname); struct.setCreateType(icust.getCreateType()); struct.setFactoryName(icust.getFactoryMethod()); coll.addChild(struct); } else { // just set item-type for concrete mapping coll.setItemTypeName(itype); } } else if (m_global.isKnownMapping(itype)) { // just set item-type for known mapping coll.setItemTypeName(itype); } else if (Types.isSimpleValue(itype) || isValueClass(itype)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -