buildframework/helium/sf/java/sysdef/src/com/nokia/helium/sysdef/PackageMap.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
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;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
import java.io.File;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
import java.io.IOException;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    21
import javax.xml.parsers.DocumentBuilder;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
import javax.xml.parsers.DocumentBuilderFactory;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
import javax.xml.parsers.ParserConfigurationException;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
import org.w3c.dom.Document;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
import org.xml.sax.SAXException;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
/**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
 * This class implement the logic to parse package_map.xml files.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
 * Example of package_map.xml file:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
 * <code>
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
 * &lt;?xml version="1.0"?&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
 * &lt;PackageMap root="sf" layer="app" /&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
 * </code>
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
public class PackageMap {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
    private String root;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
    private String layer;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
     * Create a PackageMapParser by loading data from an XML file.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
     * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
     * @param file the file to parse
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
     * @throws PackageMapParsingException in case of error
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
    public PackageMap(File file) throws PackageMapParsingException {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    50
        try {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    51
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    52
            Document doc = builder.parse(file);
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    53
            if (!doc.getDocumentElement().getNodeName().equals("PackageMap")) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    54
                throw new PackageMapParsingException("Invalid XML format for " + file.getAbsolutePath() + " root element must be PackageMap");
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    55
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    56
            if (!doc.getDocumentElement().hasAttribute("root")) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    57
                throw new PackageMapParsingException("root attribute under element " + doc.getDocumentElement().getTagName() + " is missing in file: " + file);
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    58
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    59
            if (!doc.getDocumentElement().hasAttribute("layer")) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    60
                throw new PackageMapParsingException("layer attribute under element " + doc.getDocumentElement().getTagName() + " is missing in file: " + file);
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    61
            }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    62
            setRoot(doc.getDocumentElement().getAttribute("root"));
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    63
            setLayer(doc.getDocumentElement().getAttribute("layer"));
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
        } catch (ParserConfigurationException e) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
            throw new PackageMapParsingException("Error from the XML parser configuration: " + e.getMessage(), e);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
        } catch (SAXException e) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
            throw new PackageMapParsingException("Error parsing the file: " + file + ": " + e.getMessage(), e);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
        } catch (IOException e) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
            throw new PackageMapParsingException(e.getMessage(), e);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
    public void setRoot(String root) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
        this.root = root;
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
    public String getRoot() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
        return root;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
    public void setLayer(String layer) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
        this.layer = layer;
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
    public String getLayer() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
        return layer;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
}