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.xml.internal.bind.v2.schemagen;
  27 
  28 import javax.xml.bind.annotation.XmlNsForm;
  29 import javax.xml.namespace.QName;
  30 
  31 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.LocalAttribute;
  32 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.LocalElement;
  33 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Schema;
  34 import com.sun.xml.internal.txw2.TypedXmlWriter;
  35 
  36 /**
  37  * Represents the form default value.
  38  *
  39  * @author Kohsuke Kawaguchi
  40  */
  41 enum Form {
  42     QUALIFIED(XmlNsForm.QUALIFIED,true) {
  43         void declare(String attName,Schema schema) {
  44             schema._attribute(attName,"qualified");
  45         }
  46     },
  47     UNQUALIFIED(XmlNsForm.UNQUALIFIED,false) {
  48         void declare(String attName,Schema schema) {
  49             // pointless, but required by the spec.
  50             // people need to understand that @attributeFormDefault is a syntax sugar
  51             schema._attribute(attName,"unqualified");
  52         }
  53     },
  54     UNSET(XmlNsForm.UNSET,false) {
  55         void declare(String attName,Schema schema) {
  56         }
  57     };
  58 
  59     /**
  60      * The same constant defined in the spec.
  61      */
  62     private final XmlNsForm xnf;
  63 
  64     /**
  65      * What's the effective value? UNSET means unqualified per XSD spec.)
  66      */
  67     public final boolean isEffectivelyQualified;
  68 
  69     Form(XmlNsForm xnf, boolean effectivelyQualified) {
  70         this.xnf = xnf;
  71         this.isEffectivelyQualified = effectivelyQualified;
  72     }
  73 
  74     /**
  75      * Writes the attribute on the generated <schema> element.
  76      */
  77     abstract void declare(String attName, Schema schema);
  78 
  79     /**
  80      * Given the effective 'form' value, write (or suppress) the @form attribute
  81      * on the generated XML.
  82      */
  83     public void writeForm(LocalElement e, QName tagName) {
  84         _writeForm(e,tagName);
  85     }
  86 
  87     public void writeForm(LocalAttribute a, QName tagName) {
  88         _writeForm(a,tagName);
  89     }
  90 
  91     private void _writeForm(TypedXmlWriter e, QName tagName) {
  92         boolean qualified = tagName.getNamespaceURI().length()>0;
  93 
  94         if(qualified && this!=QUALIFIED)
  95             e._attribute("form","qualified");
  96         else
  97         if(!qualified && this==QUALIFIED)
  98             e._attribute("form","unqualified");
  99     }
 100 
 101     /**
 102      * Gets the constant the corresponds to the given {@link XmlNsForm}.
 103      */
 104     public static Form get(XmlNsForm xnf) {
 105         for (Form v : values()) {
 106             if(v.xnf==xnf)
 107                 return v;
 108         }
 109         throw new IllegalArgumentException();
 110     }
 111 
 112 }