001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.schemagen;
027:
028: import javax.xml.bind.annotation.XmlNsForm;
029: import javax.xml.namespace.QName;
030:
031: import com.sun.xml.internal.bind.v2.schemagen.xmlschema.LocalAttribute;
032: import com.sun.xml.internal.bind.v2.schemagen.xmlschema.LocalElement;
033: import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Schema;
034: import com.sun.xml.internal.txw2.TypedXmlWriter;
035:
036: /**
037: * Represents the form default value.
038: *
039: * @author Kohsuke Kawaguchi
040: */
041: enum Form {
042: QUALIFIED(XmlNsForm.QUALIFIED, true) {
043: void declare(String attName, Schema schema) {
044: schema._attribute(attName, "qualified");
045: }
046: },
047: UNQUALIFIED(XmlNsForm.UNQUALIFIED, false) {
048: void declare(String attName, Schema schema) {
049: // pointless, but required by the spec.
050: // people need to understand that @attributeFormDefault is a syntax sugar
051: schema._attribute(attName, "unqualified");
052: }
053: },
054: UNSET(XmlNsForm.UNSET, false) {
055: void declare(String attName, Schema schema) {
056: }
057: };
058:
059: /**
060: * The same constant defined in the spec.
061: */
062: private final XmlNsForm xnf;
063:
064: /**
065: * What's the effective value? UNSET means unqualified per XSD spec.)
066: */
067: public final boolean isEffectivelyQualified;
068:
069: Form(XmlNsForm xnf, boolean effectivelyQualified) {
070: this .xnf = xnf;
071: this .isEffectivelyQualified = effectivelyQualified;
072: }
073:
074: /**
075: * Writes the attribute on the generated <schema> element.
076: */
077: abstract void declare(String attName, Schema schema);
078:
079: /**
080: * Given the effective 'form' value, write (or suppress) the @form attribute
081: * on the generated XML.
082: */
083: public void writeForm(LocalElement e, QName tagName) {
084: _writeForm(e, tagName);
085: }
086:
087: public void writeForm(LocalAttribute a, QName tagName) {
088: _writeForm(a, tagName);
089: }
090:
091: private void _writeForm(TypedXmlWriter e, QName tagName) {
092: boolean qualified = tagName.getNamespaceURI().length() > 0;
093:
094: if (qualified && this != QUALIFIED)
095: e._attribute("form", "qualified");
096: else if (!qualified && this == QUALIFIED)
097: e._attribute("form", "unqualified");
098: }
099:
100: /**
101: * Gets the constant the corresponds to the given {@link XmlNsForm}.
102: */
103: public static Form get(XmlNsForm xnf) {
104: for (Form v : values()) {
105: if (v.xnf == xnf)
106: return v;
107: }
108: throw new IllegalArgumentException();
109: }
110:
111: }
|