?? custombase.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.util.HashSet;import java.util.Set;import org.jibx.runtime.EnumSet;/** * Base class for all customizations. This defines a way to navigate up the tree * of nested components without making assumptions about the specific type of * the containing components. This allows for other types of customizations, * beyond the binding customizations included directly in this package. This * also includes enumeration definitions which are used with both base and * extension customizations. */public class CustomBase{ // name style value set information public static final int CAMEL_CASE_NAMES = 0; public static final int UPPER_CAMEL_CASE_NAMES = 1; public static final int HYPHENATED_NAMES = 2; public static final int DOTTED_NAMES = 3; public static final int UNDERSCORED_NAMES = 4; public static final EnumSet s_nameStyleEnum = new EnumSet(CAMEL_CASE_NAMES, new String[] { "camel-case", "upper-camel-case", "hyphenated", "dotted", "underscored" }); // require value set information public static final int REQUIRE_NONE = 0; public static final int REQUIRE_PRIMITIVES = 1; public static final int REQUIRE_OBJECTS = 2; public static final int REQUIRE_ALL = 3; public static final EnumSet s_requireEnum = new EnumSet(REQUIRE_NONE, new String[] { "none", "primitives", "objects", "all" }); // derive-namespace value set information public static final int DERIVE_NONE = 0; public static final int DERIVE_BY_PACKAGE = 1; public static final int DERIVE_FIXED = 2; public static final EnumSet s_namespaceStyleEnum = new EnumSet(DERIVE_NONE, new String[] { "none", "package", "fixed" }); // parent element (null if none) - would be final, except for unmarshalling private SharedNestingBase m_parent; /** * Constructor. * * @param parent */ public CustomBase(SharedNestingBase parent) { m_parent = parent; } /** * Get container. * * @return container */ public SharedNestingBase getParent() { return m_parent; } /** * Get global customizations root. * * @return global customization */ public GlobalCustom getGlobal() { CustomBase parent = m_parent; while (!(parent instanceof GlobalCustom)) { parent = parent.getParent(); } return (GlobalCustom)parent; } /** * Utility method to build a set from an array of names. * * @param names (<code>null</code> if none) * @return name set (<code>null</code> if none) */ protected static Set nameSet(String[] names) { if (names == null) { return null; } else { HashSet set = new HashSet(); for (int i = 0; i < names.length; i++) { set.add(names[i].toLowerCase()); } return set; } } /** * Convert class, method, or parameter name to XML name. * * @param base class or simple field name to be converted * @param code conversion format style code * @return XML name */ public static String convertName(String base, int code) { // strip off trailing array indication while (base.endsWith("[]")) { base = base.substring(0, base.length()-2); } // skip any leading special characters in name int length = base.length(); int offset = -1; char chr = 0; while (++offset < length && ((chr = base.charAt(offset)) == '_' || (chr == '$'))); if (offset >= length) { return "_"; } else { // make sure valid first character of name StringBuffer buff = new StringBuffer(); if (Character.isDigit(chr)) { buff.append('_'); } // scan for word splits in supplied name boolean split = true; boolean caps = false; while (offset < length) { // split if underscore or change to upper case, or upper ending chr = base.charAt(offset++); boolean nextlower = offset < length && !Character.isUpperCase(base.charAt(offset)); if (chr == '_') { split = true; continue; } else if (Character.isUpperCase(chr)) { if (!caps || nextlower) { split = true; } } if (split) { // convert word break caps = !nextlower; boolean tolower = nextlower || offset == length; char separator = 0; switch (code) { case CAMEL_CASE_NAMES: { if (buff.length() != 0) { chr = Character.toUpperCase(chr); tolower = false; } break; } case UPPER_CAMEL_CASE_NAMES: { tolower = false; chr = Character.toUpperCase(chr); break; } case HYPHENATED_NAMES: { separator = '-'; break; } case DOTTED_NAMES: { separator = '.'; break; } case UNDERSCORED_NAMES: { separator = '_'; break; } } if (separator > 0 && buff.length() > 0) { buff.append(separator); } if (tolower) { chr = Character.toLowerCase(chr); } split = false; } if (chr != '$') { // no split, just append the character buff.append(chr); } } return buff.toString(); } } /** * Derive name for item in a collection. If the supplied collection name * ends in a recognized plural form the derived item name is the singular * version of the collection name. Otherwise, it is the converted name of * the collection item class, or just "item" if the class is unknown. * * @param cname collection name * @param type item type (<code>null</code> if unknown) * @param code conversion format style code * @return item name */ public static String deriveItemName(String cname, String type, int code) { if (cname.endsWith("ies")) { return cname.substring(0, cname.length()-3) + 'y'; } else if (cname.endsWith("sses")) { return cname.substring(0, cname.length()-2); } else if (cname.endsWith("s")) { return cname.substring(0, cname.length()-1); } else if (type != null && !"java.lang.Object".equals(type)) { return convertName(type.substring(type.lastIndexOf('.')+1), code); } else { return "item"; } } /** * Get the package from a fully-qualified type name. * * @param type fully-qualified type name * @return package of the type (empty string if in default package) */ public static String packageOfType(String type) { int split = type.lastIndexOf('.'); if (split >= 0) { return type.substring(0, split); } else { return ""; } } /** * Create a namespace URL from a package path. * * @param pkgpth fully-qualified package name * @return namespace based on package (<code>null</code> if none) */ public static String packageToNamespace(String pkgpth) { int mark = pkgpth.indexOf('.'); if (mark >= 0) { StringBuffer buff = new StringBuffer(); buff.append("http://"); String comp = pkgpth.substring(0, mark); int base = mark + 1; char delim = '.'; if ("com".equals(comp) || "net".equals(comp) || "org".equals(comp)) { mark = pkgpth.indexOf('.', base); if (mark > 0) { buff.append(pkgpth.substring(base, mark)); } else { buff.append(pkgpth.substring(base)); } buff.append('.'); base = mark + 1; delim = '/'; } buff.append(comp); while (mark > 0) { buff.append(delim); base = mark + 1; mark = pkgpth.indexOf('.', base); if (mark > 0) { buff.append(pkgpth.substring(base, mark)); } else { buff.append(pkgpth.substring(base)); } } return buff.toString(); } else { return null; } } /** * Derive namespace using specified technique. * * @param uri base namespace URI (<code>null</code> if none) * @param pkgpth fully qualified package name * @param style namespace style code * @return derived namespace */ public static String deriveNamespace(String uri, String pkgpth, int style) { switch (style) { case DERIVE_NONE: return null; case DERIVE_FIXED: return uri; case DERIVE_BY_PACKAGE: { if (uri == null) { return packageToNamespace(pkgpth); } else if (pkgpth == null) { return uri; } else { // append the last package to passed URI String pack; int start = pkgpth.lastIndexOf('.'); if (start >= 0) { pack = pkgpth.substring(start+1); } else { pack = pkgpth; } if (uri.endsWith("/")) { return uri + pack; } else { return uri + '/' + pack; } } } default: throw new IllegalStateException("Invalid style code"); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -