<?xml version="1.0" encoding="UTF-8"?>
<!--

DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.

The contents of this file are subject to the terms of either the GNU
General Public License Version 2 only ("GPL") or the Common Development
and Distribution License("CDDL") (collectively, the "License").  You
may not use this file except in compliance with the License.  You can
obtain a copy of the License at
http://glassfish.java.net/public/CDDL+GPL_1_1.html
or packager/legal/LICENSE.txt.  See the License for the specific
language governing permissions and limitations under the License.

When distributing the software, include this License Header Notice in each
file and include the License file at packager/legal/LICENSE.txt.

GPL Classpath Exception:
Oracle designates this particular file as subject to the "Classpath"
exception as provided by Oracle in the GPL Version 2 section of the License
file that accompanied this code.

Modifications:
If applicable, add the following below the License Header, with the fields
enclosed by brackets [] replaced by your own identifying information:
"Portions Copyright [year] [name of copyright owner]"

Contributor(s):
If you wish your version of this file to be governed by only the CDDL or
only the GPL Version 2, indicate your decision by adding "[Contributor]
elects to include this software in this distribution under the [CDDL or GPL
Version 2] license."  If you don't indicate a single choice of license, a
recipient has the option to distribute your version of this file under
either the CDDL, the GPL Version 2 or to extend the choice of license to
its licensees as provided above.  However, if you add GPL Version 2 code
and therefore, elected the GPL Version 2 license, then the option applies
only if the new code is made subject to such option by the copyright
holder.

        -->
<!DOCTYPE book [
<!ENTITY % ents SYSTEM "docbook.ent">
%ents;
]>
<section version="5.0" xml:id="compiling-xml-schema-avoid-strong-databinding"
         xml:lang="en" xmlns="http://docbook.org/ns/docbook"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xmlns:ns5="http://www.w3.org/1999/xhtml"
         xmlns:ns3="http://www.w3.org/2000/svg"
         xmlns:ns="http://docbook.org/ns/docbook"
         xmlns:m="http://www.w3.org/1998/Math/MathML">
    <title>Avoid strong databinding</title>

    <para>Under some limited circumstances, a weaker databinding is preferable
    for various reasons. JAXB does offer a few ways for you to achieve
    this.</para>

    <section xml:id="Avoid_mapping_to_enum">
        <title>Avoid mapping to enum</title>

        <para>The following customization will stop binding a simple type to a
        type-safe enum. This can be convenient when number of constants is too
        large to be an useful enum (by default, the JAXB spec won't generate
        enum with more than 256 constants, but even 100 might be too large for
        you.)</para>

        <example>
            <title>Avoid mapping one simple type</title>

            <programlisting language="xml"><![CDATA[<xs:simpleType name="foo">
  <xs:annotation><xs:appinfo>
    <jaxb:typesafeEnumClass map="false" />
  </xs:appinfo></xs:annotation>
  <xs:restriction base="xs:string">
    <xs:enumeration value="x" />
    <xs:enumeration value="y" />
    <xs:enumeration value="z" />
  </xs:restriction>
</xs:simpleType>]]></programlisting>
        </example>

        <para>To disable such type-safe enum binding altogether for the entire
        schema, use a global binding setting like this (this is actually
        telling XJC not to generate enums if a simple type has more than 0
        constants --- the net effect is no enum generation):</para>

        <example>
            <title>Avoid generating enums at all</title>

            <programlisting language="xml"><![CDATA[<xs:schema ...>
  <xs:annotation><xs:appinfo>
    <jaxb:globalBindings typesafeEnumMaxMembers="0" />
  </xs:appinfo></xs:annotation>
  ...
</xs:schema>]]></programlisting>
        </example>
    </section>

    <section xml:id="Mapping_to_DOM">
        <title>Mapping to DOM</title>

        <para>The <literal>&lt;jaxb:dom&gt;</literal>customization allows you to map
        a certain part of the schema into a DOM tree. This customization can
        be attached to the following schema components:</para>

        <itemizedlist>
            <listitem>
                <para>Wildcards (<literal>&lt;xs:any&gt;</literal>)</para>
            </listitem>

            <listitem>
                <para>Type definitions (<literal>&lt;xs:complexType&gt;</literal> and
                <literal>&lt;xs:simpleType&gt;</literal>)</para>
            </listitem>

            <listitem>
                <para>Model groups
                (<literal>&lt;xs:choice&gt;</literal>,<literal>&lt;xs:all&gt;</literal>,<literal>&lt;xs:sequence&gt;</literal>)</para>
            </listitem>

            <listitem>
                <para>Model group declarations (<literal>&lt;xs:group&gt;</literal>)</para>
            </listitem>

            <listitem>
                <para>Particles</para>
            </listitem>

            <listitem>
                <para>Element declarations (<literal>&lt;xs:element&gt;</literal>)</para>
            </listitem>
        </itemizedlist>

        <para>In the following example, a wildcard is mapped to a DOM node.
        Each element that matches to the wildcard will be turned into a DOM
        tree.</para>

        <example>
            <title>Dom Customization example</title>

            <programlisting language="xml"><![CDATA[<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               jaxb:version="2.0">

        <xs:element>
           <xs:complexType>
              <xs:sequence>
                 <xs:any maxOccurs="unbounded" processContents="skip">
                    <xs:annotation><xs:appinfo>
                      <jaxb:dom/>
                    </xs:appinfo></xs:annotation>
                 </xs:any>
              </xs:sequence>
           </xs:complexType>
        </xs:element>
    .
    .
    .
    </xs:schema>]]></programlisting>
        </example>

        <para>This extension can be used to access wildcard content or can be
        used to process a part of a document by using other technologies that
        require "raw" XML. By default, JAXB generates a <code>getContent()</code> method
        for accessing wildcard content, but it only supports "lax" handling
        which means that unknown content is discarded. You may find more
        information in 7.12 chapter of <link
        xlink:href="http://www.jcp.org/en/jsr/detail?id=222">JAXB 2
        specification</link>.</para>
    </section>
</section>
