buildframework/helium/sf/java/antdata/src/com/nokia/helium/ant/data/AntFile.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 587 85df38eb4012
child 645 b8d81fa19e7d
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
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
package com.nokia.helium.ant.data;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
import java.io.File;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    21
import java.io.FileReader;
587
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.ArrayList;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
import java.util.List;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
import org.apache.tools.ant.Project;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
import org.dom4j.Document;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
import org.dom4j.Element;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
import org.dom4j.Node;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    30
import org.xml.sax.InputSource;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    31
import org.xml.sax.SAXException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    32
import org.xml.sax.XMLReader;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    33
import org.xml.sax.helpers.XMLReaderFactory;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
/**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
 * An Ant build file. It could be a project file or an antlib file.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
public class AntFile {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
    private Database database;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
    private File path;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
    private Document doc;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
    private RootAntObjectMeta fileObjectMeta;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
    private Project rootProject;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
    public AntFile(Database database, String path) throws IOException {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
        this(database, path, Database.DEFAULT_SCOPE);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
    public AntFile(Database database, String path, String scope) throws IOException {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
        this.database = database;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
        this.path = new File(path);
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    52
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    53
        readDoc();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    54
        Element node = doc.getRootElement();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    55
        if (node.getName().equals("project")) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    56
            fileObjectMeta = new ProjectMeta(this, node);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    57
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    58
        else {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    59
            fileObjectMeta = new AntlibMeta(this, node);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    60
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    61
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    62
        fileObjectMeta.setScopeFilter(scope);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    63
        fileObjectMeta.setRuntimeProject(rootProject);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    65
    }
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    67
    private void readDoc() throws IOException {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    68
        try {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    69
            DocumentFactoryWithLocator documentFactory = new DocumentFactoryWithLocator();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    70
            SAXContentHandlerExt contentHandler = new SAXContentHandlerExt(documentFactory);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    71
            documentFactory.setContentHandler(contentHandler);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    72
            XMLReader xmlReader = XMLReaderFactory.createXMLReader();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    73
            xmlReader.setContentHandler(contentHandler);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    74
            xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", contentHandler);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    75
            xmlReader.parse(new InputSource(new FileReader(path)));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    76
            doc = contentHandler.getDocument();
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
        }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    78
        catch (SAXException e) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
            throw new IOException(e.getMessage());
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
    public Project getProject() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
        return rootProject;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
    public void setProject(Project project) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
        this.rootProject = project;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    89
        fileObjectMeta.setRuntimeProject(rootProject);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    91
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
    public void setScope(String scope) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
        fileObjectMeta.setScopeFilter(scope);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
     * Get the meta object for the Ant project in this build file.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
     * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
     * @return The project meta object.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   101
    public RootAntObjectMeta getRootObjectMeta() {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   102
        return fileObjectMeta;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   103
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   104
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   105
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   106
     * The File path of the Ant file.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   107
     * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
     * @return Path.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   109
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   110
    public File getFile() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   111
        return path;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   112
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   113
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   114
    public Database getDatabase() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   115
        return database;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   116
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   117
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   118
    @SuppressWarnings("unchecked")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   119
    public List<AntFile> getAntlibs() throws IOException {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   120
        Element node = doc.getRootElement();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   121
        List<AntFile> antlibFiles = new ArrayList<AntFile>();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   122
        if (node.getName().equals("project")) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   123
            List<Node> typedefs = node.selectNodes("//*[namespace-uri()='http://www.nokia.com/helium' and local-name()='typedef']");
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   124
            for (Node typedefNode : typedefs) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   125
                String filePath = ((Element) typedefNode).attributeValue("file");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   126
                filePath = getProject().replaceProperties(filePath);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   127
                antlibFiles.add(new AntFile(database, filePath));
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   128
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   129
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   130
        return antlibFiles;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   131
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   132
}