diff -r 82f11024044a -r 932c358ece3e Orb/python/orb/lib.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Orb/python/orb/lib.py Fri Apr 23 20:45:58 2010 +0100 @@ -0,0 +1,205 @@ +# Copyright (c) 2007-2010 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: +# +import os +import unittest +import xml +import re +import sys +from optparse import OptionParser +from cStringIO import StringIO +from xml.etree import ElementTree as etree + +nmtoken_regex = re.compile("[^a-zA-Z0-9_\.]") + +def scan(dir): + for root, _, files in os.walk(dir): + for fname in files: + yield os.path.join(root, fname) + +def xml_decl(): + return """""" + +def doctype_identifier(doctype): + """ + Return a doctype declaration string for a given doctype. + Understands DITA and cxxapiref DITA specialisation doctypes. + """ + # DITA Doctype Identifiers (no specific version number in identifier means latest DITA DTD version) + if doctype == "map": + return """""" + elif doctype == "topic": + return """""" + elif doctype == "task": + return """""" + elif doctype == "reference": + return """""" + elif doctype == "glossary": + return """""" + elif doctype == "concept": + return """""" + elif doctype == "bookmap": + return """""" + # cxxapiref DITA specialisation Doctype Identifiers + elif doctype == "cxxUnion": + return """""" + elif doctype == "cxxStruct": + return """""" + elif doctype == "cxxPackage": + return """""" + elif doctype == "cxxFile": + return """""" + elif doctype == "cxxClass": + return """""" + elif doctype == "cxxAPIMap": + return """""" + else: + raise Exception('Unknown Doctype \"%s\"' % doctype) + +def get_valid_nmtoken(attribute_value): + new_value = attribute_value + matches = nmtoken_regex.findall(new_value) + for char in set(matches): + new_value = new_value.replace(char,"") + return new_value + +class XmlParser(object): + """ + Simple class that reads an XML and returns its id + + >>> xp = XmlParser() + >>> xp.parse(StringIO("some content")) + 'rootid' + """ + def parse(self, xmlfile): + try: + root = etree.parse(xmlfile).getroot() + except xml.parsers.expat.ExpatError, e: + sys.stderr.write("ERROR: %s could not be parse: %s\n" % (xmlfile, str(e))) + return "" + if 'id' not in root.attrib: + return "" + return root.attrib['id'] + +def main(func, version): + usage = "usage: %prog " + parser = OptionParser(usage, version='%prog ' + version) + (options, args) = parser.parse_args() + if len(args) < 1: + parser.print_help() + parser.error("Please supply the path to the XML content") + func(args[0]) + +###################################### +# Test code +###################################### + +class Testxml_decl(unittest.TestCase): + def testi_can_return_anxml_declaration(self): + self.assertEquals(xml_decl(), """""") + + +class Testdoctype_identifier(unittest.TestCase): + + def test_i_raise_an_exception_for_an_unknown_doctype(self): + self.assertRaises(Exception, doctype_identifier, "invaliddoctype") + + def test_i_can_return_a_map_doctype_identifier(self): + self.assertEquals(doctype_identifier("map"), """""") + + def test_i_can_return_a_topic_doctype_identifier(self): + self.assertEquals(doctype_identifier("topic"), """""") + + def test_i_can_return_a_task_doctype_identifier(self): + self.assertEquals(doctype_identifier("task"), """""") + + def test_i_can_return_a_reference_doctype_identifier(self): + self.assertEquals(doctype_identifier("reference"), """""") + + def test_i_can_return_a_glossary_doctype_identifier(self): + self.assertEquals(doctype_identifier("glossary"), """""") + + def test_i_can_return_a_concept_doctype_identifier(self): + self.assertEquals(doctype_identifier("concept"), """""") + + def test_i_can_return_a_bookmap_doctype_identifier(self): + self.assertEquals(doctype_identifier("bookmap"), """""") + + def test_i_can_return_a_cxxUnion_doctype_identifier(self): + self.assertEquals(doctype_identifier("cxxUnion"), """""") + + def test_i_can_return_a_cxxStruct_doctype_identifier(self): + self.assertEquals(doctype_identifier("cxxStruct"), """""") + + def test_i_can_return_a_cxxPackage_doctype_identifier(self): + self.assertEquals(doctype_identifier("cxxPackage"), """""") + + def test_i_can_return_a_cxxFile_doctype_identifier(self): + self.assertEquals(doctype_identifier("cxxFile"), """""") + + def test_i_can_return_a_cxxClass_doctype_identifier(self): + self.assertEquals(doctype_identifier("cxxClass"), """""") + + def test_i_can_return_a_cxxAPIMap_doctype_identifier(self): + self.assertEquals(doctype_identifier("cxxAPIMap"), """""") + + +class Testget_valid_nmtoken(unittest.TestCase): + + def test_i_remove_non_alpha_numeric_characters(self): + input = "this is an alphanumeric string with non alpha numeric characters inside.()_+=-string0123456789" + expout = "thisisanalphanumericstringwithnonalphanumericcharactersinside._string0123456789" + output = get_valid_nmtoken(input) + self.assertEquals(output, expout) + + +class StubXmlParser(object): + def parse(self, path): + return "GUID-BED8A733-2ED7-31AD-A911-C1F4707C67F" + + +class TestXmlParser(unittest.TestCase): + def test_i_issue_a_warning_and_continue_if_a_file_is_invalid(self): + xml = XmlParser() + try: + xml.parse(StringIO("")) + except Exception, e: + self.fail("I shouldn't have raised an exception. Exception was %s" % e) + + def test_i_issue_a_warning_and_continue_if_a_file_does_not_have_an_id(self): + xml = XmlParser() + try: + id = xml.parse(StringIO(brokencxxclass)) + except Exception: + self.fail("I shouldn't have raised an exception") + self.assertTrue(id == "") + + def test_i_return_a_files_id(self): + xml = XmlParser() + id = xml.parse(StringIO(cxxclass)) + self.assertTrue(id == "class_c_active_scheduler") + +brokencxxclass = """ + + + CActiveScheduler + + +""" + +cxxclass = """ + + + CActiveScheduler + + +""" \ No newline at end of file