1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.internal.xjc.reader.xmlschema.bindinfo;
  27 
  28 import javax.xml.bind.annotation.XmlAttribute;
  29 import javax.xml.bind.annotation.XmlElement;
  30 import javax.xml.bind.annotation.XmlRootElement;
  31 import javax.xml.bind.annotation.XmlType;
  32 import javax.xml.namespace.QName;
  33 
  34 import com.sun.tools.internal.xjc.reader.Const;
  35 import com.sun.xml.internal.xsom.XSAttributeDecl;
  36 import com.sun.xml.internal.xsom.XSComponent;
  37 import com.sun.xml.internal.xsom.XSElementDecl;
  38 import com.sun.xml.internal.xsom.XSModelGroup;
  39 import com.sun.xml.internal.xsom.XSModelGroupDecl;
  40 import com.sun.xml.internal.xsom.XSType;
  41 
  42 /**
  43  * Schema-wide binding customization.
  44  *
  45  * @author
  46  *  Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  47  */
  48 @XmlRootElement(name="schemaBindings")
  49 public final class BISchemaBinding extends AbstractDeclarationImpl {
  50 
  51     /**
  52      * Name conversion rules. All defaults to {@link BISchemaBinding#defaultNamingRule}.
  53      */
  54     @XmlType(propOrder={})
  55     private static final class NameRules {
  56         @XmlElement
  57         NamingRule typeName = defaultNamingRule;
  58         @XmlElement
  59         NamingRule elementName = defaultNamingRule;
  60         @XmlElement
  61         NamingRule attributeName = defaultNamingRule;
  62         @XmlElement
  63         NamingRule modelGroupName = defaultNamingRule;
  64         @XmlElement
  65         NamingRule anonymousTypeName = defaultNamingRule;
  66     }
  67 
  68     @XmlElement
  69     private NameRules nameXmlTransform = new NameRules();
  70 
  71     private static final class PackageInfo {
  72         @XmlAttribute
  73         String name;
  74         @XmlElement
  75         String javadoc;
  76     }
  77 
  78     @XmlElement(name="package")
  79     private PackageInfo packageInfo = new PackageInfo();
  80 
  81     /**
  82      * If false, it means not to generate any classes from this namespace.
  83      * No ObjectFactory, no classes (the only way to bind them is by using
  84      * <jaxb:class ref="..."/>)
  85      */
  86     @XmlAttribute(name="map")
  87     public boolean map = true;
  88 
  89     /**
  90      * Default naming rule, that doesn't change the name.
  91      */
  92     private static final NamingRule defaultNamingRule = new NamingRule("","");
  93 
  94 
  95     /**
  96      * Default naming rules of the generated interfaces.
  97      *
  98      * It simply adds prefix and suffix to the name, but
  99      * the caller shouldn't care how the name mangling is
 100      * done.
 101      */
 102     public static final class NamingRule {
 103         @XmlAttribute
 104         private String prefix = "";
 105         @XmlAttribute
 106         private String suffix = "";
 107 
 108         public NamingRule( String _prefix, String _suffix ) {
 109             this.prefix = _prefix;
 110             this.suffix = _suffix;
 111         }
 112 
 113         public NamingRule() {
 114         }
 115 
 116         /** Changes the name according to the rule. */
 117         public String mangle( String originalName ) {
 118             return prefix+originalName+suffix;
 119         }
 120     }
 121 
 122     /**
 123      * Transforms the default name produced from XML name
 124      * by following the customization.
 125      *
 126      * This shouldn't be applied to a class name specified
 127      * by a customization.
 128      *
 129      * @param cmp
 130      *      The schema component from which the default name is derived.
 131      */
 132     public String mangleClassName( String name, XSComponent cmp ) {
 133         if( cmp instanceof XSType )
 134             return nameXmlTransform.typeName.mangle(name);
 135         if( cmp instanceof XSElementDecl )
 136             return nameXmlTransform.elementName.mangle(name);
 137         if( cmp instanceof XSAttributeDecl )
 138             return nameXmlTransform.attributeName.mangle(name);
 139         if( cmp instanceof XSModelGroup || cmp instanceof XSModelGroupDecl )
 140             return nameXmlTransform.modelGroupName.mangle(name);
 141 
 142         // otherwise no modification
 143         return name;
 144     }
 145 
 146     public String mangleAnonymousTypeClassName( String name ) {
 147         return nameXmlTransform.anonymousTypeName.mangle(name);
 148     }
 149 
 150 
 151     public String getPackageName() { return packageInfo.name; }
 152 
 153     public String getJavadoc() { return packageInfo.javadoc; }
 154 
 155     public QName getName() { return NAME; }
 156     public static final QName NAME = new QName(
 157         Const.JAXB_NSURI, "schemaBinding" );
 158 }