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

/*
 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of the License "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description: 
 *
 */

package com.nokia.helium.sbs;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
import org.dom4j.io.SAXReader;

/**
 * Parses the sysdef config file and extracts the available configurations
 */
public class SAXSysdefParser {
    private File sysdefFile;
    private List<String> layers;
    private boolean initialized;

    /**
     * Constructor
     * 
     * @param fileName - name of the sysdef file to parse
     */
    public SAXSysdefParser(File fileName) {

        sysdefFile = fileName;
    }

    public List<String> getLayers() {
        if (!initialized) {
            initialized = true;
            parseConfig("layer");
            if (layers == null) {
                throw new BuildException("No layers found from sysdef");
            }
        }
        return layers;
    }

    /**
     * Constructor
     * 
     * @return list of available configurations that can be built.
     */
    public void parseConfig(String nodeToGet) {
        layers = new ArrayList<String>();
        SAXReader reader = new SAXReader();
        reader.addHandler("/SystemDefinition/systemModel/" + nodeToGet, new ElementHandler() {
            public void onStart(ElementPath path) {
            }

            public void onEnd(ElementPath path) {
                Element row = path.getCurrent();
                Iterator itr = row.attributeIterator();
                while (itr.hasNext()) {
                    Attribute child = (Attribute) itr.next();
                    String attrName = child.getQualifiedName();
                    if (attrName.equals("name")) {
                        layers.add(child.getValue());
                    }
                }
                row.detach();
            }
        });
        try {
            reader.read(sysdefFile);
        }
        catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}