Orb/python/doxygen/lib.py
changeset 1 82f11024044a
parent 0 42188c7ea2d9
--- a/Orb/python/doxygen/lib.py	Thu Jan 21 17:29:01 2010 +0000
+++ b/Orb/python/doxygen/lib.py	Thu Mar 18 18:26:18 2010 +0000
@@ -12,8 +12,14 @@
 #
 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):
@@ -45,20 +51,44 @@
         return """<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">""" 
     # cxxapiref DITA specialisation Doctype Identifiers
     elif doctype == "cxxUnion":
-        return """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type//EN" "dtd/cxxUnion.dtd">"""   
+        return """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type v0.5.0//EN" "dtd/cxxUnion.dtd">"""   
     elif doctype == "cxxStruct":
-        return """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type//EN" "dtd/cxxStruct.dtd">"""
+        return """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type v0.5.0//EN" "dtd/cxxStruct.dtd">"""
     elif doctype == "cxxPackage":
-        return """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type//EN" "dtd/cxxPackage.dtd">"""
+        return """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type v0.5.0//EN" "dtd/cxxPackage.dtd">"""
     elif doctype == "cxxFile":
-        return """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type//EN" "dtd/cxxFile.dtd">"""
+        return """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type v0.5.0//EN" "dtd/cxxFile.dtd">"""
     elif doctype == "cxxClass":
-        return """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type//EN" "dtd/cxxClass.dtd">"""
+        return """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type v0.5.0//EN" "dtd/cxxClass.dtd">"""
     elif doctype == "cxxAPIMap":
-        return """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type//EN" "dtd/cxxAPIMap.dtd" >"""
+        return """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.5.0//EN" "dtd/cxxAPIMap.dtd" >"""
     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("<root id='rootid'>some content</root>"))
+    '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 <Path to the XML content>"
@@ -77,6 +107,7 @@
     def testi_can_return_anxml_declaration(self):
         self.assertEquals(xml_decl(), """<?xml version="1.0" encoding="UTF-8"?>""")
     
+    
 class Testdoctype_identifier(unittest.TestCase):
     
     def test_i_raise_an_exception_for_an_unknown_doctype(self):
@@ -104,22 +135,71 @@
         self.assertEquals(doctype_identifier("bookmap"), """<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">""")
 
     def test_i_can_return_a_cxxUnion_doctype_identifier(self):        
-        self.assertEquals(doctype_identifier("cxxUnion"), """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type//EN" "dtd/cxxUnion.dtd">""")
+        self.assertEquals(doctype_identifier("cxxUnion"), """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type v0.5.0//EN" "dtd/cxxUnion.dtd">""")
     
     def test_i_can_return_a_cxxStruct_doctype_identifier(self):        
-        self.assertEquals(doctype_identifier("cxxStruct"), """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type//EN" "dtd/cxxStruct.dtd">""")
+        self.assertEquals(doctype_identifier("cxxStruct"), """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type v0.5.0//EN" "dtd/cxxStruct.dtd">""")
         
     def test_i_can_return_a_cxxPackage_doctype_identifier(self):        
-        self.assertEquals(doctype_identifier("cxxPackage"), """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type//EN" "dtd/cxxPackage.dtd">""")
+        self.assertEquals(doctype_identifier("cxxPackage"), """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type v0.5.0//EN" "dtd/cxxPackage.dtd">""")
         
-    
     def test_i_can_return_a_cxxFile_doctype_identifier(self):        
-        self.assertEquals(doctype_identifier("cxxFile"), """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type//EN" "dtd/cxxFile.dtd">""")
+        self.assertEquals(doctype_identifier("cxxFile"), """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type v0.5.0//EN" "dtd/cxxFile.dtd">""")
         
     def test_i_can_return_a_cxxClass_doctype_identifier(self):        
-        self.assertEquals(doctype_identifier("cxxClass"), """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type//EN" "dtd/cxxClass.dtd">""")
+        self.assertEquals(doctype_identifier("cxxClass"), """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type v0.5.0//EN" "dtd/cxxClass.dtd">""")
         
     def test_i_can_return_a_cxxAPIMap_doctype_identifier(self):        
-        self.assertEquals(doctype_identifier("cxxAPIMap"), """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type//EN" "dtd/cxxAPIMap.dtd" >""")
+        self.assertEquals(doctype_identifier("cxxAPIMap"), """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.5.0//EN" "dtd/cxxAPIMap.dtd" >""")
+        
                 
-        
\ No newline at end of file
+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("<foo><bar</foo>"))
+        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 = """<?xml version='1.0' encoding='UTF-8' standalone='no'?>
+<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type v0.5.0//EN" "dtd/cxxClass.dtd" >
+<cxxClass>
+    <apiName>CActiveScheduler</apiName>
+    <shortdesc/>
+</cxxClass>
+"""
+
+cxxclass = """<?xml version='1.0' encoding='UTF-8' standalone='no'?>
+<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type v0.5.0//EN" "dtd/cxxClass.dtd" >
+<cxxClass id="class_c_active_scheduler">
+    <apiName>CActiveScheduler</apiName>
+    <shortdesc/>
+</cxxClass>
+"""
\ No newline at end of file