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