buildframework/helium/sf/java/sbs/src/com/nokia/helium/sbs/SAXSysdefParser.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
/*
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
     2
 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
     3
 * All rights reserved.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
     4
 * This component and the accompanying materials are made available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
     6
 * which accompanies this distribution, and is available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
     8
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
     9
 * Initial Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    10
 * Nokia Corporation - initial contribution.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    11
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    12
 * Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    13
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    14
 * Description: 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    15
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    16
 */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    17
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
package com.nokia.helium.sbs;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    20
import java.io.File;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    21
import java.util.ArrayList;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    22
import java.util.Iterator;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    23
import java.util.List;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    24
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
import org.apache.tools.ant.BuildException;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    26
import org.dom4j.Attribute;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    27
import org.dom4j.DocumentException;
587
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.ElementHandler;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    30
import org.dom4j.ElementPath;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
import org.dom4j.io.SAXReader;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
/**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
 * Parses the sysdef config file and extracts the available configurations
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
public class SAXSysdefParser {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
    private File sysdefFile;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
    private List<String> layers;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
    private boolean initialized;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
     * Constructor
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    43
     * 
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
     * @param fileName - name of the sysdef file to parse
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
    public SAXSysdefParser(File fileName) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    47
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
        sysdefFile = fileName;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
    }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    50
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
    public List<String> getLayers() {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    52
        if (!initialized) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
            initialized = true;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
            parseConfig("layer");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
            if (layers == null) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
                throw new BuildException("No layers found from sysdef");
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
        return layers;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
     * Constructor
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    64
     * 
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
     * @return list of available configurations that can be built.
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    66
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    67
    public void parseConfig(String nodeToGet) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
        layers = new ArrayList<String>();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
        SAXReader reader = new SAXReader();
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    70
        reader.addHandler("/SystemDefinition/systemModel/" + nodeToGet, new ElementHandler() {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    71
            public void onStart(ElementPath path) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    72
            }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    73
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    74
            public void onEnd(ElementPath path) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    75
                Element row = path.getCurrent();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    76
                Iterator itr = row.attributeIterator();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    77
                while (itr.hasNext()) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    78
                    Attribute child = (Attribute) itr.next();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    79
                    String attrName = child.getQualifiedName();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    80
                    if (attrName.equals("name")) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    81
                        layers.add(child.getValue());
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
                    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
                }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    84
                row.detach();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    85
            }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    86
        });
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
        try {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
            reader.read(sysdefFile);
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    89
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    90
        catch (DocumentException e) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    91
            e.printStackTrace();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
}