001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.tools.xjc.generator.bean;
037:
038: import java.util.Collections;
039: import java.util.HashMap;
040: import java.util.HashSet;
041: import java.util.Map;
042: import java.util.Set;
043:
044: import javax.xml.bind.annotation.XmlNsForm;
045: import javax.xml.bind.annotation.XmlSchema;
046: import javax.xml.namespace.QName;
047:
048: import com.sun.codemodel.JDefinedClass;
049: import com.sun.codemodel.JPackage;
050: import com.sun.tools.xjc.generator.annotation.spec.XmlSchemaWriter;
051: import com.sun.tools.xjc.model.CAttributePropertyInfo;
052: import com.sun.tools.xjc.model.CClassInfo;
053: import com.sun.tools.xjc.model.CElement;
054: import com.sun.tools.xjc.model.CElementPropertyInfo;
055: import com.sun.tools.xjc.model.CPropertyInfo;
056: import com.sun.tools.xjc.model.CPropertyVisitor;
057: import com.sun.tools.xjc.model.CReferencePropertyInfo;
058: import com.sun.tools.xjc.model.CTypeRef;
059: import com.sun.tools.xjc.model.CValuePropertyInfo;
060: import com.sun.tools.xjc.model.Model;
061: import com.sun.tools.xjc.outline.PackageOutline;
062: import com.sun.tools.xjc.outline.Aspect;
063:
064: /**
065: * {@link PackageOutline} enhanced with schema2java specific
066: * information.
067: *
068: * @author
069: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
070: */
071: final class PackageOutlineImpl implements PackageOutline {
072: private final Model _model;
073: private final JPackage _package;
074: private final ObjectFactoryGenerator objectFactoryGenerator;
075:
076: /*package*/final Set<ClassOutlineImpl> classes = new HashSet<ClassOutlineImpl>();
077: private final Set<ClassOutlineImpl> classesView = Collections
078: .unmodifiableSet(classes);
079:
080: private String mostUsedNamespaceURI;
081: private XmlNsForm elementFormDefault;
082:
083: /**
084: * The namespace URI most commonly used in classes in this package.
085: * This should be used as the namespace URI for {@link XmlSchema#namespace()}.
086: *
087: * <p>
088: * Null if no default
089: *
090: * @see #calcDefaultValues().
091: */
092: public String getMostUsedNamespaceURI() {
093: return mostUsedNamespaceURI;
094: }
095:
096: /**
097: * The element form default for this package.
098: * <p>
099: * The value is computed by examining what would yield the smallest generated code.
100: */
101: public XmlNsForm getElementFormDefault() {
102: assert elementFormDefault != null;
103: return elementFormDefault;
104: }
105:
106: public JPackage _package() {
107: return _package;
108: }
109:
110: public ObjectFactoryGenerator objectFactoryGenerator() {
111: return objectFactoryGenerator;
112: }
113:
114: public Set<ClassOutlineImpl> getClasses() {
115: return classesView;
116: }
117:
118: public JDefinedClass objectFactory() {
119: return objectFactoryGenerator.getObjectFactory();
120: }
121:
122: protected PackageOutlineImpl(BeanGenerator outline, Model model,
123: JPackage _pkg) {
124: this ._model = model;
125: this ._package = _pkg;
126: switch (model.strategy) {
127: case BEAN_ONLY:
128: objectFactoryGenerator = new PublicObjectFactoryGenerator(
129: outline, model, _pkg);
130: break;
131: case INTF_AND_IMPL:
132: objectFactoryGenerator = new DualObjectFactoryGenerator(
133: outline, model, _pkg);
134: break;
135: default:
136: throw new IllegalStateException();
137: }
138: }
139:
140: /**
141: * Compute the most common namespace URI in this package
142: * (to put into {@link XmlSchema#namespace()} and what value
143: * we should put into {@link XmlSchema#elementFormDefault()}.
144: *
145: * This method is called after {@link #classes} field is filled up.
146: */
147: public void calcDefaultValues() {
148: // short-circuit if xjc was told not to generate package level annotations in
149: // package-info.java
150: if (!_model.isPackageLevelAnnotations()) {
151: mostUsedNamespaceURI = "";
152: elementFormDefault = XmlNsForm.UNQUALIFIED;
153: return;
154: }
155:
156: // used to visit properties
157: CPropertyVisitor<Void> propVisitor = new CPropertyVisitor<Void>() {
158: public Void onElement(CElementPropertyInfo p) {
159: for (CTypeRef tr : p.getTypes()) {
160: countURI(propUriCountMap, tr.getTagName());
161: }
162: return null;
163: }
164:
165: public Void onReference(CReferencePropertyInfo p) {
166: for (CElement e : p.getElements()) {
167: countURI(propUriCountMap, e.getElementName());
168: }
169: return null;
170: }
171:
172: public Void onAttribute(CAttributePropertyInfo p) {
173: return null;
174: }
175:
176: public Void onValue(CValuePropertyInfo p) {
177: return null;
178: }
179: };
180:
181: for (ClassOutlineImpl co : classes) {
182: CClassInfo ci = co.target;
183: countURI(uriCountMap, ci.getTypeName());
184: countURI(uriCountMap, ci.getElementName());
185:
186: for (CPropertyInfo p : ci.getProperties())
187: p.accept(propVisitor);
188: }
189: mostUsedNamespaceURI = getMostUsedURI(uriCountMap);
190: elementFormDefault = getFormDefault();
191:
192: // generate package-info.java
193: // we won't get this far if the user specified -npa
194: if (!mostUsedNamespaceURI.equals("")
195: || elementFormDefault == XmlNsForm.QUALIFIED) {
196: XmlSchemaWriter w = _model.strategy.getPackage(_package,
197: Aspect.IMPLEMENTATION).annotate2(
198: XmlSchemaWriter.class);
199: if (!mostUsedNamespaceURI.equals(""))
200: w.namespace(mostUsedNamespaceURI);
201: if (elementFormDefault == XmlNsForm.QUALIFIED)
202: w.elementFormDefault(elementFormDefault);
203: }
204: }
205:
206: // Map to keep track of how often each type or element uri is used in this package
207: // mostly used to calculate mostUsedNamespaceURI
208: private HashMap<String, Integer> uriCountMap = new HashMap<String, Integer>();
209:
210: // Map to keep track of how often each property uri is used in this package
211: // used to calculate elementFormDefault
212: private HashMap<String, Integer> propUriCountMap = new HashMap<String, Integer>();
213:
214: /**
215: * pull the uri out of the specified QName and keep track of it in the
216: * specified hash map
217: *
218: * @param qname
219: */
220: private void countURI(HashMap<String, Integer> map, QName qname) {
221: if (qname == null)
222: return;
223:
224: String uri = qname.getNamespaceURI();
225:
226: if (map.containsKey(uri)) {
227: map.put(uri, map.get(uri) + 1);
228: } else {
229: map.put(uri, 1);
230: }
231: }
232:
233: /**
234: * Iterate through the hash map looking for the namespace used
235: * most frequently. Ties are arbitrarily broken by the order
236: * in which the map keys are iterated over.
237: */
238: private String getMostUsedURI(HashMap<String, Integer> map) {
239: String mostPopular = null;
240: int count = 0;
241:
242: for (Map.Entry<String, Integer> e : map.entrySet()) {
243: String uri = e.getKey();
244: int uriCount = e.getValue();
245: if (mostPopular == null) {
246: mostPopular = uri;
247: count = uriCount;
248: } else {
249: if (uriCount > count) {
250: mostPopular = uri;
251: count = uriCount;
252: }
253: }
254: }
255:
256: if (mostPopular == null)
257: return "";
258: return mostPopular;
259: }
260:
261: /**
262: * Calculate the element form defaulting.
263: *
264: * Compare the most frequently used property URI to the most frequently used
265: * element/type URI. If they match, then return QUALIFIED
266: */
267: private XmlNsForm getFormDefault() {
268: if (getMostUsedURI(propUriCountMap).equals(""))
269: return XmlNsForm.UNQUALIFIED;
270: else
271: return XmlNsForm.QUALIFIED;
272: }
273: }
|