buildframework/helium/sf/java/sysdef/src/com/nokia/helium/sysdef/PackageDefinition.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;
       
    18 
       
    19 import java.io.File;
       
    20 import java.io.IOException;
       
    21 import java.util.Hashtable;
       
    22 import java.util.Map;
       
    23 
       
    24 import javax.xml.parsers.DocumentBuilder;
       
    25 import javax.xml.parsers.DocumentBuilderFactory;
       
    26 import javax.xml.parsers.ParserConfigurationException;
       
    27 
       
    28 import org.w3c.dom.Document;
       
    29 import org.w3c.dom.NamedNodeMap;
       
    30 import org.w3c.dom.NodeList;
       
    31 import org.xml.sax.SAXException;
       
    32 
       
    33 /**
       
    34  * Basic package_definition file parser. It is meant to extract needed data for the root system
       
    35  * definition file creation.
       
    36  * 
       
    37  */
       
    38 public class PackageDefinition {
       
    39     public static final String DEFAULT_ID_NAMESPACE = "http://www.symbian.org/system-definition";
       
    40 
       
    41     private String idNamespace;
       
    42     private String id;
       
    43     private Map<String, String> namespaces = new Hashtable<String, String>();
       
    44 
       
    45     /**
       
    46      * Construct a PackageDefinition object extracting data from the 
       
    47      * file <code>file</code>.
       
    48      *  
       
    49      * @param file
       
    50      * @throws PackageDefinitionParsingException
       
    51      */
       
    52     public PackageDefinition(File file) throws PackageDefinitionParsingException {
       
    53         try {
       
    54             DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
       
    55             Document doc = builder.parse(file);
       
    56             if (!doc.getDocumentElement().getNodeName().equals("SystemDefinition")) {
       
    57                 throw new PackageDefinitionParsingException("Invalid XML format for "
       
    58                     + file.getAbsolutePath() + " root element must be SystemDefinition");
       
    59             }
       
    60             // Getting information from the SystemDefinition element
       
    61             if (doc.getDocumentElement().hasAttribute("id-namespace")) {
       
    62                 idNamespace = doc.getDocumentElement().getAttribute("id-namespace");
       
    63             } else {
       
    64                 idNamespace = DEFAULT_ID_NAMESPACE;
       
    65             }
       
    66             NamedNodeMap attrs = doc.getDocumentElement().getAttributes();
       
    67             for (int i = 0; i < attrs.getLength(); i++) {
       
    68                 if (attrs.item(i).getNodeName().startsWith("xmlns:")) {
       
    69                     namespaces.put(attrs.item(i).getNodeName(), attrs.item(i).getNodeValue());
       
    70                 }
       
    71             }
       
    72 
       
    73             // Getting information from the package element
       
    74             NodeList nodes = doc.getDocumentElement().getChildNodes();
       
    75             for (int i = 0; i < nodes.getLength(); i++) {
       
    76                 if (nodes.item(i).getNodeName().equals("package")) {
       
    77                     if (nodes.item(i).getAttributes().getNamedItem("id") == null) {
       
    78                         throw new PackageDefinitionParsingException("Invalid XML format for "
       
    79                             + file.getAbsolutePath()
       
    80                             + " the package element must have an id attribute.");
       
    81                     }
       
    82                     id = nodes.item(i).getAttributes().getNamedItem("id").getNodeValue();
       
    83                 }
       
    84             }
       
    85             if (id == null) {
       
    86                 throw new PackageDefinitionParsingException("Invalid XML format for "
       
    87                     + file.getAbsolutePath() + " could not find any package definition.");
       
    88             }
       
    89         }
       
    90         catch (ParserConfigurationException e) {
       
    91             throw new PackageDefinitionParsingException("Error from the XML parser configuration: "
       
    92                 + e.getMessage(), e);
       
    93         }
       
    94         catch (SAXException e) {
       
    95             throw new PackageDefinitionParsingException("Error parsing the file: " + file + ": "
       
    96                 + e.getMessage(), e);
       
    97         }
       
    98         catch (IOException e) {
       
    99             throw new PackageDefinitionParsingException(e.getMessage(), e);
       
   100         }
       
   101     }
       
   102 
       
   103     /**
       
   104      * id-namespace of current package.
       
   105      * @return always returns a string
       
   106      */
       
   107     public String getIdNamespace() {
       
   108         return idNamespace;
       
   109     }
       
   110 
       
   111     /**
       
   112      * Id of the package.
       
   113      * @return
       
   114      */
       
   115     public String getId() {
       
   116         return id;
       
   117     }
       
   118 
       
   119     /**
       
   120      * List of global namespaces.
       
   121      * @return a map of namespaces <name, uri>
       
   122      */
       
   123     public Map<String, String> getNamespaces() {
       
   124         return namespaces;
       
   125     }
       
   126 }