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