buildframework/helium/sf/java/sysdef/src/com/nokia/helium/sysdef/ant/conditions/IsVendorPackage.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     1
/*
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     2
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     3
* All rights reserved.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     4
* This component and the accompanying materials are made available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     6
* which accompanies this distribution, and is available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     8
*
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     9
* Initial Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    11
*
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    12
* Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    13
*
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    14
* Description:  
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    15
*
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    16
*/
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    17
package com.nokia.helium.sysdef.ant.conditions;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    18
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    19
import java.io.File;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    20
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    21
import org.apache.tools.ant.BuildException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    22
import org.apache.tools.ant.taskdefs.condition.Condition;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    23
import org.apache.tools.ant.types.Resource;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    24
import org.apache.tools.ant.types.resources.selectors.ResourceSelector;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    25
import org.apache.tools.ant.types.selectors.BaseExtendSelector;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    26
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    27
import com.nokia.helium.sysdef.PackageDefinition;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    28
import com.nokia.helium.sysdef.PackageDefinitionParsingException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    29
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    30
/**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    31
 * Ant condition to test if a package_definition.xml is
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    32
 * defining a vendor package. A package is considered as a vendor package
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    33
 * when its id-namespace attribute has a different from the default one (please check the
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    34
 * system definition documentation on the Symbian Foundation website for further detail).
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    35
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    36
 * <pre>
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    37
 *     &lt;condition property="name" value="true"&gt;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    38
 *         &lt;hlm:isVendorPackage file="${epocroot}/sf/app/package/package_definition.xml" /&gt;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    39
 *     &lt;/condition&gt;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    40
 * </pre>
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    41
 * 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    42
 * This condition is also usable as a resource selector:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    43
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    44
 * <pre>
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    45
 *     &lt;restrict&gt;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    46
 *         &lt;fileset dir=&quot;...&quot; includes=&quot;package_definition.xml&quot; /&gt;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    47
 *         &lt;hlm:isVendorPackage /&gt;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    48
 *     &lt;/restrict&gt;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    49
 * </pre>
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    50
 * 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    51
 */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    52
public class IsVendorPackage extends BaseExtendSelector
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    53
    implements Condition, ResourceSelector {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    54
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    55
    private File file;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    56
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    57
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    58
     * Detect is the package_definition.xml is from a vendor package.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    59
     * Vendor package have different id-namespace than the default
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    60
     * one (<code>PackageDefinition.DEFAULT_ID_NAMESPACE</code>).
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    61
     * 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    62
     * @param file
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    63
     * @return true if it denotes a vendor package.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    64
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    65
    protected boolean isVendorPackage(File file) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    66
        if ("package_definition.xml".equals(file.getName())) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    67
            try {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    68
                PackageDefinition pkgDef = new PackageDefinition(file);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    69
                return !PackageDefinition.DEFAULT_ID_NAMESPACE.equals(pkgDef.getIdNamespace());
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    70
            } catch (PackageDefinitionParsingException e) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    71
                throw new BuildException("Error parsing " + file 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    72
                        + ": " + e.getMessage(), e);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    73
            }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    74
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    75
        return false;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    76
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    77
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    78
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    79
     * {@inheritDoc}
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    80
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    81
    @Override
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    82
    public boolean eval() {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    83
        if (file == null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    84
            throw new BuildException("'file' attribute is not defined.");
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    85
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    86
        return isVendorPackage(file);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    87
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    88
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    89
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    90
     * {@inheritDoc}
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    91
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    92
    @Override
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    93
    public boolean isSelected(File basedir, String filename, File file) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    94
        return isVendorPackage(file);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    95
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    96
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    97
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    98
     * The file to test when used as a condition.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    99
     * @param file
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   100
     * @ant.required
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   101
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   102
    public void setFile(File file) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   103
        this.file = file;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   104
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   105
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   106
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   107
     * {@inheritDoc}
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   108
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   109
    @Override
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   110
    public boolean isSelected(Resource resource) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   111
        if (!resource.isFilesystemOnly()) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   112
            throw new BuildException(this.getDataTypeName() + " only supports filesystem resources.");
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   113
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   114
        return isVendorPackage(new File(resource.toString()));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   115
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   116
}