<?xml version="1.0"?>
<!--

    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    Copyright (c) 1997-2011 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
    https://glassfish.dev.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.

-->

<project name="osgi" basedir=".." default="dist">

    <description>
      OSGi Sub-Project
      ===================

      The OSGi project creates an OSGi bundle jaxb-osgi.jar for use
      in GlassFish V3.
    </description>

    <!-- import build properties -->
    <property file="${basedir}/build.properties" />

    <taskdef name="manifestreader" classname="manifestreader.ManifestReader" classpath="${osgi.build}/../ant" />

    <target name="dist" depends="bundle, javadoc, sources"/>
    
    <target name="bundle"
            description="create OSGi bundle for GlassFish V3"
	    depends="clean">

        <taskdef resource="aQute/bnd/ant/taskdef.properties"
                 classpath="${jaxb.root}/tools/lib/util/bnd-0.0.249.jar"/>

        <mkdir dir="${osgi.build.rt}"/>

        <copy file="${osgi.root}/jaxb.bnd" todir="${osgi.build.rt}">
            <filterset>
                <filter token="RELEASE_IMPL_VERSION" value="${release.impl.version}"/>
                <filter token="RELEASE_SPEC_VERSION" value="${release.spec.version}"/>
            </filterset>
        </copy>

        <!-- JAXB 1.0 uses its own classloader and should be ignored by bnd. -->
        <!-- Since bnd insists on processing the entire jar, we need to remove JAXB 1.0 classes first. -->
        <mkdir dir="${osgi.build}/jaxb-xjc"/>
        <unjar src="${jaxb.dist}/lib/jaxb-xjc.jar"
               dest="${osgi.build}/jaxb-xjc">
            <patternset>
                <exclude name="1.0/**"/>
                <exclude name="com/sun/tools/xjc/*Task*"/>
                <exclude name="com/sun/tools/jxc/*Task*"/>
                <exclude name="com/sun/istack/tools/*Task*"/>
		<exclude name="org/kohsuke/**"/>
            </patternset>
        </unjar>

        <unjar src="${jaxb.libs.util}/FastInfoset.jar"
               dest="${osgi.build}/fastinfoset">
            <patternset>
                <exclude name="org/jvnet/fastinfoset/sax/**"/>
                <exclude name="org/jvnet/fastinfoset/stax/**"/>
            </patternset>
        </unjar>

        <!-- The bnd task does not recognize when input files have changed, -->
        <!-- hence we delete the output file to ensure a rebuild            -->
        <delete file="${osgi.build.rt}/jaxb-bnd.jar" quiet="true"/>

	<bnd classpath="${jaxb.dist}/lib/jaxb-impl.jar, ${osgi.build}/jaxb-xjc, ${osgi.build}/fastinfoset, ${jaxb.libs.util}/stax-ex.jar"
             failok="false"
             output="${osgi.build.rt}/jaxb-bnd.jar"
             files="${osgi.build.rt}/jaxb.bnd"/>

        <manifestreader fileName="${osgi.build.rt}/jaxb-bnd.jar">
            <property name="jaxb.Ant-Version" value="Ant-Version" />
            <property name="jaxb.Bnd-LastModified" value="Bnd-LastModified" />
            <property name="jaxb.Build-Id" value="Build-Id" />
            <property name="jaxb.Bundle-ManifestVersion" value="Bundle-ManifestVersion" />
            <property name="jaxb.Bundle-Name" value="Bundle-Name" />
            <property name="jaxb.Bundle-SymbolicName" value="Bundle-SymbolicName" />
            <property name="jaxb.Bundle-Version" value="Bundle-Version" />
            <property name="jaxb.Class-Path" value="Class-Path" />
            <property name="jaxb.Created-By" value="Created-By" />
            <property name="jaxb.Export-Package" value="Export-Package" />
            <property name="jaxb.Extension-Name" value="Extension-Name" />
            <property name="jaxb.Implementation-Title" value="Implementation-Title" />
            <property name="jaxb.Implementation-Vendor" value="Implementation-Vendor" />
            <property name="jaxb.Implementation-Vendor-Id" value="Implementation-Vendor-Id" />
            <property name="jaxb.Implementation-Version" value="Implementation-Version" />
            <property name="jaxb.Import-Package" value="Import-Package" />
            <property name="jaxb.Manifest-Version" value="Manifest-Version" />
            <property name="jaxb.Originally-Created-By" value="Originally-Created-By" />
            <property name="jaxb.Private-Package" value="Private-Package" />
            <property name="jaxb.Specification-Title" value="Specification-Title" />
            <property name="jaxb.Specification-Vendor" value="Specification-Vendor" />
            <property name="jaxb.Specification-Version" value="Specification-Version" />
            <property name="jaxb.Tool" value="Tool" />
        </manifestreader>

        <mkdir dir="${osgi.build.rt}/META-INF/" />

        <manifest file="${osgi.build.rt}/META-INF/MANIFEST.MF">
            <attribute name="Ant-Version" value="${jaxb.Ant-Version}"/>
            <attribute name="Bnd-LastModified" value="${jaxb.Bnd-LastModified}"/>
            <attribute name="Build-Id" value="${jaxb.Build-Id}"/>
            <attribute name="Bundle-ManifestVersion" value="${jaxb.Bundle-ManifestVersion}"/>
            <attribute name="Bundle-Name" value="${jaxb.Bundle-Name}"/>
            <attribute name="Bundle-SymbolicName" value="${jaxb.Bundle-SymbolicName}"/>

	    <!-- bundle version -->
            <!-- <attribute name="Bundle-Version" value="${jaxb.Bundle-Version}"/> -->
            <attribute name="Bundle-Version" value="${release.impl.version}"/>
            <attribute name="Class-Path" value="${jaxb.Class-Path}"/>
            <attribute name="Created-By" value="${jaxb.Created-By}"/>

            <!-- adding com/sun/msv/datatype/xsd to Export-package -->
            <!-- adding com.sun.msv.grammar,com.sun.msv.grammar.trex,com.sun.msv.reader,com.sun.msv.reader.trex.ng,com.sun.msv.reader.util,com.sun.msv.util,com.sun.msv.verifier,com.sun.msv.verifier.regexp  to Export-package (fabians request for woodstock) -->

            <attribute name="Export-Package" value="${jaxb.Export-Package},com.sun.msv.datatype.xsd,com.sun.msv.grammar,com.sun.msv.grammar.trex,com.sun.msv.reader,com.sun.msv.reader.trex.ng,com.sun.msv.reader.util,com.sun.msv.util,com.sun.msv.verifier,com.sun.msv.verifier.regexp"/>
            <attribute name="Extension-Name" value="${jaxb.Extension-Name}"/>
            <attribute name="Implementation-Title" value="${jaxb.Implementation-Title}"/>
            <attribute name="Implementation-Vendor" value="${jaxb.Implementation-Vendor}"/>
            <attribute name="Implementation-Vendor-Id" value="${jaxb.Implementation-Vendor-Id}"/>
            <attribute name="Import-Package" value="${jaxb.Import-Package},com.sun.org.apache.xerces.internal.impl.xpath.regex;resolution:=optional"/>
            <attribute name="Manifest-Version" value="${jaxb.Manifest-Version}"/>
            <attribute name="Originally-Created-By" value="${jaxb.Originally-Created-By}"/>
            <attribute name="Private-Package" value="${jaxb.Private-Package}"/>
            <attribute name="Specification-Title" value="${jaxb.Specification-Title}"/>
            <attribute name="Specification-Vendor" value="${jaxb.Specification-Vendor}"/>
            <attribute name="Specification-Version" value="${jaxb.Specification-Version}"/>
            <attribute name="Tool" value="${jaxb.Tool}"/>
            <attribute name="Implementation-Version" value="${jaxb.Implementation-Version}"/>
        </manifest>

        <jar destfile="${osgi.build}/jaxb-osgi.jar"
             manifest="${osgi.build.rt}/META-INF/MANIFEST.MF">
            <zipfileset src="${osgi.build.rt}/jaxb-bnd.jar"/>
            <zipfileset src="${jaxb.dist}/lib/jaxb1-impl.jar">
                <patternset>
                    <exclude name="org/relaxng/datatype/**"/>
                </patternset>
            </zipfileset>
            <zipfileset src="${jaxb.dist}/lib/jaxb-xjc.jar">
                <include name="1.0/**"/>
                <include name="com/sun/tools/xjc/*Task*"/>
                <include name="com/sun/tools/jxc/*Task*"/>
                <include name="com/sun/istack/tools/*Task*"/>
                <include name="org/kohsuke/**"/>
            </zipfileset>
            <zipfileset src="${jaxb.libs.util}/FastInfoset.jar">
                <include name="org/jvnet/fastinfoset/sax/**"/>
                <include name="org/jvnet/fastinfoset/stax/**"/>
             </zipfileset>
        </jar>
        
    </target>

    <target name="clean">
        <delete dir="${osgi.build}" quiet="true"/>
    </target>

    <path id="javac.classpath">
        <fileset dir="${jaxb.libs.redist}" includes="*.jar"/>
        <fileset dir="${jaxb.libs.rebundle}" includes="**/*.jar"/>
        <fileset dir="${jaxb.libs.util}" includes="*.jar" excludes="ant.jar"/>
    </path>

    <target name="javadoc">
        <unzip src="${jaxb.dist}/lib/jaxb-impl.src.zip" dest="${jaxb.build}/jaxbosgisrc"/>
        <unzip src="${jaxb.dist}/lib/jaxb-xjc.src.zip" dest="${jaxb.build}/jaxbosgisrc"/>
        <mkdir dir="${jaxb.build}/jaxbosgiapi"/>
        <javadoc destdir="${jaxb.build}/jaxbosgiapi" use="true" defaultexcludes="yes"
            author="false" version="false" windowtitle="JAXB RI" useexternalfile="true" access="public">
            <fileset dir="${jaxb.build}/jaxbosgisrc" includes="com/sun/xml/bind/*.java, com/sun/xml/bind/annotation/*.java, com/sun/xml/bind/api/*.java,
                com/sun/tools/xjc/*.java, com/sun/tools/jxc/*.java, com/sun/tools/xjc/api/*.java"/>
            <classpath refid="javac.classpath"/>
            <arg value="-quiet"/>
        </javadoc>
        <zip zipfile="${osgi.build}/jaxb-osgi-javadoc.jar">
            <fileset dir="${jaxb.build}/jaxbosgisrc"/>
        </zip>
    </target>

    <target name="sources">
        <jar destfile="${osgi.build}/jaxb-osgi-sources.jar">
            <zipfileset src="${jaxb.dist}/lib/jaxb-impl.src.zip"/>
            <zipfileset src="${jaxb.dist}/lib/jaxb-xjc.src.zip"/>
        </jar>
    </target>

</project>
