sysmodelmgr/com.symbian.smt.gui/src/com/symbian/smt/gui/SystemDefinition.java
changeset 0 522a326673b6
equal deleted inserted replaced
-1:000000000000 0:522a326673b6
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 package com.symbian.smt.gui;
       
    17 
       
    18 import java.io.IOException;
       
    19 
       
    20 import javax.xml.parsers.DocumentBuilder;
       
    21 import javax.xml.parsers.DocumentBuilderFactory;
       
    22 import javax.xml.parsers.ParserConfigurationException;
       
    23 import javax.xml.xpath.XPath;
       
    24 import javax.xml.xpath.XPathConstants;
       
    25 import javax.xml.xpath.XPathExpression;
       
    26 import javax.xml.xpath.XPathExpressionException;
       
    27 import javax.xml.xpath.XPathFactory;
       
    28 
       
    29 import org.w3c.dom.Document;
       
    30 import org.xml.sax.ErrorHandler;
       
    31 import org.xml.sax.SAXException;
       
    32 import org.xml.sax.SAXParseException;
       
    33 
       
    34 public class SystemDefinition {
       
    35 
       
    36 	/**
       
    37 	 * Determines whether the System Definition file is likely to be valid for the System Model Generator
       
    38 	 * Even if this function fails the SMG may still accept the file.
       
    39 	 * @param filename - the path to the System Definition file
       
    40 	 * @throws SystemDefinitionValidationException - Thrown if the parsing of the document fails. If the document parses
       
    41 	 * correctly but does not appear to be a System Definition file a {@link SystemDefinitionValidationFatalException} is thrown.
       
    42 	 */
       
    43 	public static void checkValidSystemDefinitionFile(String filename) throws SystemDefinitionValidationException {
       
    44 		Document doc;
       
    45 		try {
       
    46 			doc = createDocument(filename);
       
    47 		} catch (ParserConfigurationException e) {
       
    48 			throw new SystemDefinitionValidationException("Problem found when parsing "+filename+".", e);
       
    49 		} catch (SAXException e) {
       
    50 			throw new SystemDefinitionValidationException("Problem found when parsing "+filename+".", e);
       
    51 		} catch (IOException e) {
       
    52 			throw new SystemDefinitionValidationException("Problem found when reading "+filename+".", e);
       
    53 		}
       
    54 
       
    55 		// Check that the file is a system definition file
       
    56 		if (doc.getDoctype() != null && !doc.getDoctype().getName().equals("SystemDefinition")) {
       
    57 			throw new SystemDefinitionValidationFatalException(filename + " has the doctype "+doc.getDoctype()+". Where specified, the doctype should be \"SystemDefinition\".");
       
    58 		}
       
    59 	}
       
    60 
       
    61 	public static int coreOSType(String filename)
       
    62 			throws ParserConfigurationException, SAXException, IOException,
       
    63 			XPathExpressionException {
       
    64 		Document doc = createDocument(filename);
       
    65 
       
    66 		int type = 0;
       
    67 
       
    68 		XPathFactory factory = XPathFactory.newInstance();
       
    69 		XPath xpath = factory.newXPath();
       
    70 
       
    71 		XPathExpression expr = xpath
       
    72 				.compile("count(//layer[@name='Hardware']) > 0");
       
    73 		Object result = expr.evaluate(doc, XPathConstants.BOOLEAN);
       
    74 
       
    75 		if ((Boolean) result) {
       
    76 			type = 1;
       
    77 		}
       
    78 
       
    79 		expr = xpath.compile("count(//layer[@name='HAL']) > 0");
       
    80 		Object result2 = expr.evaluate(doc, XPathConstants.BOOLEAN);
       
    81 
       
    82 		if ((Boolean) result2) {
       
    83 			type = 2;
       
    84 		}
       
    85 
       
    86 		return type;
       
    87 	}
       
    88 
       
    89 	private static Document createDocument(final String filename)
       
    90 			throws ParserConfigurationException, SAXException, IOException {
       
    91 		DocumentBuilderFactory domFactory = DocumentBuilderFactory
       
    92 				.newInstance();
       
    93 		domFactory.setNamespaceAware(true);
       
    94 		domFactory.setValidating(false);
       
    95 
       
    96 		DocumentBuilder builder = domFactory.newDocumentBuilder();
       
    97 		ErrorHandler errorHandler = new ErrorHandler() {
       
    98 			public void error(SAXParseException exception)
       
    99 					throws SAXParseException {
       
   100 				throw exception;
       
   101 			}
       
   102 
       
   103 			public void fatalError(SAXParseException exception)
       
   104 					throws SAXParseException {
       
   105 				throw exception;
       
   106 			}
       
   107 
       
   108 			public void warning(SAXParseException exception)
       
   109 					throws SAXParseException {
       
   110 				throw exception;
       
   111 			}
       
   112 		};
       
   113 		builder.setErrorHandler(errorHandler);
       
   114 		Document doc = builder.parse(filename);
       
   115 
       
   116 		return doc;
       
   117 	}
       
   118 
       
   119 }