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:
037: package com.sun.xml.bind.v2.schemagen;
038:
039: import javax.xml.bind.annotation.XmlNsForm;
040: import javax.xml.namespace.QName;
041:
042: import com.sun.xml.bind.v2.schemagen.xmlschema.LocalAttribute;
043: import com.sun.xml.bind.v2.schemagen.xmlschema.LocalElement;
044: import com.sun.xml.bind.v2.schemagen.xmlschema.Schema;
045: import com.sun.xml.txw2.TypedXmlWriter;
046:
047: /**
048: * Represents the form default value.
049: *
050: * @author Kohsuke Kawaguchi
051: */
052: enum Form {
053: QUALIFIED(XmlNsForm.QUALIFIED, true) {
054: void declare(String attName, Schema schema) {
055: schema._attribute(attName, "qualified");
056: }
057: },
058: UNQUALIFIED(XmlNsForm.UNQUALIFIED, false) {
059: void declare(String attName, Schema schema) {
060: // pointless, but required by the spec.
061: // people need to understand that @attributeFormDefault is a syntax sugar
062: schema._attribute(attName, "unqualified");
063: }
064: },
065: UNSET(XmlNsForm.UNSET, false) {
066: void declare(String attName, Schema schema) {
067: }
068: };
069:
070: /**
071: * The same constant defined in the spec.
072: */
073: private final XmlNsForm xnf;
074:
075: /**
076: * What's the effective value? UNSET means unqualified per XSD spec.)
077: */
078: public final boolean isEffectivelyQualified;
079:
080: Form(XmlNsForm xnf, boolean effectivelyQualified) {
081: this .xnf = xnf;
082: this .isEffectivelyQualified = effectivelyQualified;
083: }
084:
085: /**
086: * Writes the attribute on the generated <schema> element.
087: */
088: abstract void declare(String attName, Schema schema);
089:
090: /**
091: * Given the effective 'form' value, write (or suppress) the @form attribute
092: * on the generated XML.
093: */
094: public void writeForm(LocalElement e, QName tagName) {
095: _writeForm(e, tagName);
096: }
097:
098: public void writeForm(LocalAttribute a, QName tagName) {
099: _writeForm(a, tagName);
100: }
101:
102: private void _writeForm(TypedXmlWriter e, QName tagName) {
103: boolean qualified = tagName.getNamespaceURI().length() > 0;
104:
105: if (qualified && this != QUALIFIED)
106: e._attribute("form", "qualified");
107: else if (!qualified && this == QUALIFIED)
108: e._attribute("form", "unqualified");
109: }
110:
111: /**
112: * Gets the constant the corresponds to the given {@link XmlNsForm}.
113: */
114: public static Form get(XmlNsForm xnf) {
115: for (Form v : values()) {
116: if (v.xnf == xnf)
117: return v;
118: }
119: throw new IllegalArgumentException();
120: }
121:
122: }
|