org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/core/deploy/emulator/EmulatorTargetType.java
changeset 458 5ff93668b08c
equal deleted inserted replaced
457:f1087591ff71 458:5ff93668b08c
       
     1 /**
       
     2  * Copyright (c) 2010 Symbian Foundation 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  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  */
       
    19 package org.symbian.tools.wrttools.core.deploy.emulator;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.FileInputStream;
       
    23 import java.io.IOException;
       
    24 import java.util.Collection;
       
    25 import java.util.HashMap;
       
    26 import java.util.Map;
       
    27 
       
    28 import javax.xml.parsers.DocumentBuilder;
       
    29 import javax.xml.parsers.DocumentBuilderFactory;
       
    30 import javax.xml.parsers.ParserConfigurationException;
       
    31 
       
    32 import org.eclipse.core.runtime.IPath;
       
    33 import org.eclipse.core.runtime.IProgressMonitor;
       
    34 import org.eclipse.core.runtime.NullProgressMonitor;
       
    35 import org.eclipse.core.runtime.Path;
       
    36 import org.eclipse.core.runtime.jobs.ISchedulingRule;
       
    37 import org.symbian.tools.mtw.core.projects.IMTWProject;
       
    38 import org.symbian.tools.mtw.ui.deployment.IDeploymentTarget;
       
    39 import org.symbian.tools.mtw.ui.deployment.IDeploymentTargetType;
       
    40 import org.symbian.tools.wrttools.core.IWRTConstants;
       
    41 import org.w3c.dom.Document;
       
    42 import org.w3c.dom.NamedNodeMap;
       
    43 import org.w3c.dom.Node;
       
    44 import org.w3c.dom.NodeList;
       
    45 import org.xml.sax.SAXException;
       
    46 
       
    47 public final class EmulatorTargetType implements IDeploymentTargetType {
       
    48     /**
       
    49      * Map containing of the SDKS with the corresponding paths.
       
    50      */
       
    51     private Map<String, Emulator> listofEmulators;
       
    52 
       
    53     public EmulatorTargetType() {
       
    54         discoverTargets(new NullProgressMonitor());
       
    55     }
       
    56 
       
    57     public IDeploymentTarget[] getTargets(IMTWProject project) {
       
    58         Collection<Emulator> values = listofEmulators.values();
       
    59         return values.toArray(new Emulator[values.size()]);
       
    60     }
       
    61 
       
    62     public void discoverTargets(IProgressMonitor monitor) {
       
    63         if (listofEmulators == null) {
       
    64             listofEmulators = new HashMap<String, Emulator>();
       
    65         }
       
    66         listofEmulators.clear();
       
    67         initialize();
       
    68     }
       
    69 
       
    70     public IDeploymentTarget findTarget(IMTWProject project, String id) {
       
    71         return listofEmulators.get(id);
       
    72     }
       
    73 
       
    74     public boolean targetsDiscovered() {
       
    75         return true;
       
    76     }
       
    77 
       
    78     public ISchedulingRule getSchedulingRule(IDeploymentTarget target) {
       
    79         return null;
       
    80     }
       
    81 
       
    82     // helper methods
       
    83 
       
    84     /**
       
    85      * This will parse the xml and create the nodes to be displayed on the table
       
    86      * viewer. The information will be used by content & label provider to get
       
    87      * the contents and display accordingly in the list of the projects in the view.
       
    88      */
       
    89     public Map<String, Emulator> initialize() {
       
    90 
       
    91         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       
    92         // Parse the devices.xml and retrieve the list of the emulators from it
       
    93         // and build the list to be displayed in the view.
       
    94 
       
    95         try {
       
    96             DocumentBuilder builder = factory.newDocumentBuilder();
       
    97             File file = new File(IWRTConstants.DEVICES_XML_PATH);
       
    98             if (!file.exists()) {
       
    99                 IPath otherPath = new Path(System.getProperty("user.home"))
       
   100                         .append(IWRTConstants.DEVICES_VISTA_XML_PATH);
       
   101                 file = otherPath.toFile();
       
   102             }
       
   103             if (file.exists()) {
       
   104                 FileInputStream fin = new FileInputStream(file);
       
   105                 Document document = builder.parse(fin);
       
   106                 NodeList childNodes = document.getChildNodes();
       
   107 
       
   108                 for (int i = 0; i < childNodes.getLength(); i++) {
       
   109 
       
   110                     Node node = childNodes.item(i);
       
   111                     String nodeName = node.getNodeName();
       
   112                     // If the node name is "devices" it is the root node of the xml.
       
   113                     if (nodeName.equals(IWRTConstants.DEVICES_TAG)) {
       
   114                         // once we have the root node get the child information to
       
   115                         // build the devices list.
       
   116                         createDevicesList(node);
       
   117 
       
   118                     }
       
   119                 }
       
   120             }
       
   121         } catch (ParserConfigurationException e) {
       
   122             //          WidgetUtils.getView().getLogger().severe(e.getMessage());
       
   123         } catch (SAXException e) {
       
   124             //          WidgetUtils.getView().getLogger().severe(e.getMessage());
       
   125         } catch (IOException e) {
       
   126             //          WidgetUtils.getView().getLogger().severe(e.getMessage());
       
   127         }
       
   128         return listofEmulators;
       
   129     }
       
   130 
       
   131     /**
       
   132      * Creates the devices nodes in the table.
       
   133      * @param parentNode
       
   134      */
       
   135     private void createDevicesList(Node parentNode) {
       
   136         NodeList list = getChildNodes(parentNode);
       
   137         for (int i = 0; i < list.getLength(); i++) {
       
   138             Node node = list.item(i);
       
   139             String nodeName = node.getNodeName();
       
   140             if (nodeName.equals(IWRTConstants.DEVICE_TAG)) {
       
   141                 createDeviceChildNode(node);
       
   142             }
       
   143         }
       
   144     }
       
   145 
       
   146     /**
       
   147      * Gets the EPOC ROOT node and than finally the list of devices.
       
   148      * @param parentNode
       
   149      */
       
   150     private void createDeviceChildNode(Node parentNode) {
       
   151         NodeList list = getChildNodes(parentNode);
       
   152         for (int i = 0; i < list.getLength(); i++) {
       
   153             Node node = list.item(i);
       
   154             String nodeName = node.getNodeName();
       
   155             if (nodeName.equals(IWRTConstants.EPOC_ROOT_TAG)) {
       
   156                 addProject(node, parentNode);
       
   157             }
       
   158         }
       
   159     }
       
   160 
       
   161     /**
       
   162      * Adds the devices to the list to be displayed.
       
   163      * @param deviceNode the device node 
       
   164      * @param epocNode the epoc root node.
       
   165      */
       
   166     private void addProject(Node epocNode, Node deviceNode) {
       
   167         NodeList list = getChildNodes(epocNode);
       
   168         NamedNodeMap attributes = deviceNode.getAttributes();
       
   169         String sdkId = "";
       
   170         String sdkName = "";
       
   171         for (int i = 0; i < attributes.getLength(); i++) {
       
   172             Node item = attributes.item(i);
       
   173             if (item.getNodeName().equals(IWRTConstants.NAME_ATTR)) {
       
   174                 sdkName = item.getNodeValue();
       
   175             }
       
   176             if (item.getNodeName().equals(IWRTConstants.ID_ATTR)) {
       
   177                 sdkId = item.getNodeValue();
       
   178             }
       
   179         }
       
   180         for (int i = 0; i < list.getLength(); i++) {
       
   181             Node node = list.item(i);
       
   182             if (sdkName.equals(IWRTConstants.EMULATOR_NAME)) {
       
   183                 listofEmulators.put(sdkId, new Emulator(sdkId, node.getNodeValue()));
       
   184             }
       
   185         }
       
   186     }
       
   187 
       
   188     /**
       
   189      * Returns the child list of the particular Node.
       
   190      * @param parentNode
       
   191      */
       
   192     private static NodeList getChildNodes(Node parentNode) {
       
   193         return parentNode.getChildNodes();
       
   194     }
       
   195 
       
   196 }