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