buildframework/helium/sf/java/sysdef/src/com/nokia/helium/sysdef/ant/taskdefs/AbstractSydefTask.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 587 85df38eb4012
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
/*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
* All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
* This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
* which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
* Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
* Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
* Description:  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
*/
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
package com.nokia.helium.sysdef.ant.taskdefs;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
import java.io.File;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    20
import java.io.FileNotFoundException;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    21
import java.io.FileOutputStream;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
import java.io.IOException;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
import java.util.Map;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
import javax.xml.transform.ErrorListener;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
import javax.xml.transform.Transformer;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    27
import javax.xml.transform.TransformerConfigurationException;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
import javax.xml.transform.TransformerException;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
import javax.xml.transform.TransformerFactory;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
import javax.xml.transform.stream.StreamResult;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
import javax.xml.transform.stream.StreamSource;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
import org.apache.tools.ant.BuildException;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
import org.apache.tools.ant.Project;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
import org.apache.tools.ant.Task;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
/**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
 * This is the base class for manipulating Sysdef v3 files using XML Stylesheet. 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
 * The implementation/interface is not frozen yet. It is morelikely going to
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
 * change in the future, depending of the underlaying implementation.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
 * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
public abstract class AbstractSydefTask extends Task implements ErrorListener {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
    private File srcFile;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
    private File destFile;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
    private File epocroot;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    48
    private boolean failOnError = true;  // Error should not fail by default.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    49
    private boolean failOnWarning; // Warning should not fail by default.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
     * Get the source file.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
     * @return
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
    public File getSrcFile() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
        return srcFile;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
     * Defines the location of the source system definition file. 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
     * @param srcfile
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
     * @ant.required
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
    public void setSrcFile(File srcfile) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
        this.srcFile = srcfile;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
     * Get the location of the output file. 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
     * @return
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
    public File getDestFile() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
        return destFile;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
     * The the name of the destination file.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
     * @param destfile
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
     * @ant.required
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
    public void setDestFile(File destfile) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
        this.destFile = destfile;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
     * Get the epocroot.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
     * If epocroot is not set by the user it return the value from the EPOCROOT environment variable.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
     * If the EPOCROOT environment variable is not defined then a BuildException is thrown.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    89
     * @return the epocroot location as a File object.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    91
    public File getEpocroot() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
        if (epocroot == null) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
            if (System.getenv("EPOCROOT") != null) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
                return new File(System.getenv("EPOCROOT"));
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
            else {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
                throw new BuildException("'epocroot' attribute has not been defined.");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
        return epocroot;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   101
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   102
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   103
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   104
     * Location of the EPOCROOT.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   105
     * @ant.not-required By default the EPOCROOT environment variable is used.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   106
     * @param epocroot path to the epocroot.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   107
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
    public void setEpocroot(File epocroot) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   109
        this.epocroot = epocroot;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   110
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   111
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   112
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   113
     * Shall we fail the build on error.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   114
     * @return is the task should failonerror.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   115
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   116
    public boolean isFailOnError() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   117
        return failOnError;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   118
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   119
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   120
    /**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   121
     * Shall we fail the build on warning.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   122
     * @return is the task should fail on warning.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   123
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   124
    public boolean isFailOnWarning() {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   125
        return failOnWarning;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   126
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   127
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   128
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   129
     * Defines if the task should fail on error.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   130
     * @param failonerror
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   131
     * @ant.not-required Default is true.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   132
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   133
    public void setFailOnError(boolean failonerror) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   134
        this.failOnError = failonerror;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   135
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   136
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   137
    /**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   138
     * Defines if the task should fail on warning.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   139
     * @param failonerror
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   140
     * @ant.not-required Default is true.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   141
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   142
    public void setFailOnWarning(boolean failOnWarning) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   143
        this.failOnWarning = failOnWarning;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   144
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   145
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   146
    /**
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   147
     * This method should be defined by the implementing class
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   148
     * to define the location of the XSLT file.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   149
     * @return the XSLT file location.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   150
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   151
    protected abstract File getXsl();
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   152
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   153
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   154
     * Check if required attribute have been configured correctly.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   155
     * If not the method will raise a BuildException.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   156
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   157
    protected void check() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   158
        if (getSrcFile() == null) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   159
            throw new BuildException("'srcfile' attribute is not defined");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   160
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   161
        if (getDestFile() == null) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   162
            throw new BuildException("'destfile' attribute is not defined");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   163
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   164
        File xslt = getXsl();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   165
        if (!xslt.exists()) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   166
            throw new BuildException("Could not find " + xslt);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   167
        }        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   168
        if (!getSrcFile().exists()) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   169
            throw new BuildException("Could not find source file " + srcFile);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   170
        }        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   171
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   172
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   173
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   174
     * Transform the srcfile using the stylesheet provided by getXsl. The data parameters are
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   175
     * passed to the template engine. The result is save to the destfile.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   176
     * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   177
     * @param data a set of key/value to pass to the XSLT engine.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   178
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   179
    public void transform(Map<String, String> data) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   180
        check();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   181
        if (getDestFile().exists()) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   182
            log("Deleting previous output file: " + destFile, Project.MSG_DEBUG);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   183
            getDestFile().delete();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   184
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   185
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   186
        FileOutputStream output = null;        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   187
        try {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   188
            output = new FileOutputStream(getDestFile());
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   189
            TransformerFactory factory = TransformerFactory.newInstance();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   190
            Transformer transformer = factory.newTransformer(new StreamSource(getXsl()));
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   191
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   192
            transformer.setParameter("path", getSrcFile());
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   193
            for (Map.Entry<String, String> entry : data.entrySet()) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   194
                transformer.setParameter(entry.getKey(), entry.getValue());
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   195
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   196
            transformer.setErrorListener(this);        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   197
            transformer.transform(new StreamSource(getSrcFile()), new StreamResult(
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   198
                    output));
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   199
            output.close();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   200
        } catch (FileNotFoundException exc) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   201
            // deleting the intermediate file in case of error.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   202
            if (getDestFile().exists()) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   203
                // closing current output stream, so we can delete the file
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   204
                try {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   205
                    if (output != null) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   206
                        output.close();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   207
                    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   208
                } catch (IOException ioe) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   209
                    // we should just ignore that error.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   210
                    log(ioe, Project.MSG_DEBUG);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   211
                }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   212
                log("Deleting " + destFile + " because an error occured.", Project.MSG_INFO);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   213
                getDestFile().delete();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   214
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   215
            // Raising the error to Ant.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   216
            throw new BuildException(exc.toString());
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   217
        } catch (TransformerConfigurationException exc) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   218
            // deleting the intermediate file in case of error.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   219
            if (destFile.exists()) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   220
                // closing current output stream, so we can delete the file
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   221
                try {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   222
                    if (output != null) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   223
                        output.close();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   224
                    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   225
                } catch (IOException ioe) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   226
                    // we should just ignore that error.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   227
                    log(ioe, Project.MSG_DEBUG);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   228
                }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   229
                log("Deleting " + destFile + " because an error occured.", Project.MSG_INFO);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   230
                getDestFile().delete();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   231
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   232
            // Raising the error to Ant.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   233
            throw new BuildException(exc.toString());
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   234
        } catch (TransformerException exc) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   235
            // deleting the intermediate file in case of error.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   236
            if (getDestFile().exists()) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   237
                // closing current output stream, so we can delete the file
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   238
                try {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   239
                    if (output != null) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   240
                        output.close();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   241
                    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   242
                } catch (IOException ioe) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   243
                    // we should just ignore that error.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   244
                    log(ioe, Project.MSG_DEBUG);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   245
                }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   246
                log("Deleting " + getDestFile() + " because an error occured.", Project.MSG_INFO);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   247
                getDestFile().delete();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   248
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   249
            // Raising the error to Ant.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   250
            throw new BuildException(exc.toString());
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   251
        } catch (IOException e) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   252
            // we should just ignore that error, error while closing the file.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   253
            log(e.getMessage(), Project.MSG_DEBUG);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   254
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   255
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   256
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   257
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   258
     * {@inheritDoc}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   259
     * Reports errors to the Ant logging system of throw the exception if the task
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   260
     * is set to failonerror.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   261
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   262
    @Override
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   263
    public void error(TransformerException message) throws TransformerException {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   264
        if (this.isFailOnError()) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   265
            throw message;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   266
        } else {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   267
            log("ERROR: " + message.getMessageAndLocation(), Project.MSG_ERR);            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   268
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   269
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   270
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   271
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   272
     * {@inheritDoc}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   273
     * Fails the task in case of fatal error. The is nothing we can do about that.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   274
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   275
    @Override
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   276
    public void fatalError(TransformerException message) throws TransformerException {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   277
        log("ERROR: " + message.getMessageAndLocation(), Project.MSG_ERR);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   278
        throw message;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   279
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   280
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   281
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   282
     * {@inheritDoc}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   283
     * Reports errors to the Ant logging system of throw the exception if the task
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   284
     * is set to failonerror.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   285
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   286
    @Override
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   287
    public void warning(TransformerException message) throws TransformerException {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   288
        if (this.isFailOnWarning()) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   289
            throw message;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   290
        } else {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   291
            log("WARNING: " + message.getMessageAndLocation(), Project.MSG_WARN);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   292
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   293
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   294
}