util/local_database/xpathlite.py
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 #!/usr/bin/env python
       
     2 #############################################################################
       
     3 ##
       
     4 ## Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     5 ## All rights reserved.
       
     6 ## Contact: Nokia Corporation (qt-info@nokia.com)
       
     7 ##
       
     8 ## This file is part of the test suite of the Qt Toolkit.
       
     9 ##
       
    10 ## $QT_BEGIN_LICENSE:LGPL$
       
    11 ## No Commercial Usage
       
    12 ## This file contains pre-release code and may not be distributed.
       
    13 ## You may use this file in accordance with the terms and conditions
       
    14 ## contained in the Technology Preview License Agreement accompanying
       
    15 ## this package.
       
    16 ##
       
    17 ## GNU Lesser General Public License Usage
       
    18 ## Alternatively, this file may be used under the terms of the GNU Lesser
       
    19 ## General Public License version 2.1 as published by the Free Software
       
    20 ## Foundation and appearing in the file LICENSE.LGPL included in the
       
    21 ## packaging of this file.  Please review the following information to
       
    22 ## ensure the GNU Lesser General Public License version 2.1 requirements
       
    23 ## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    24 ##
       
    25 ## In addition, as a special exception, Nokia gives you certain additional
       
    26 ## rights.  These rights are described in the Nokia Qt LGPL Exception
       
    27 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    28 ##
       
    29 ## If you have questions regarding the use of this file, please contact
       
    30 ## Nokia at qt-info@nokia.com.
       
    31 ##
       
    32 ##
       
    33 ##
       
    34 ##
       
    35 ##
       
    36 ##
       
    37 ##
       
    38 ##
       
    39 ## $QT_END_LICENSE$
       
    40 ##
       
    41 #############################################################################
       
    42 
       
    43 import sys
       
    44 import os
       
    45 import xml.dom.minidom
       
    46 
       
    47 doc_cache = {}
       
    48 
       
    49 class DraftResolution:
       
    50     # See http://www.unicode.org/cldr/process.html for description
       
    51     unconfirmed = 'unconfirmed'
       
    52     provisional = 'provisional'
       
    53     contributed = 'contributed'
       
    54     approved = 'approved'
       
    55 
       
    56 
       
    57 def findChild(parent, tag_name, arg_value, draft=None):
       
    58     for node in parent.childNodes:
       
    59         if node.nodeType != node.ELEMENT_NODE:
       
    60             continue
       
    61         if node.nodeName != tag_name:
       
    62             continue
       
    63         if arg_value:
       
    64             if not node.attributes.has_key('type'):
       
    65                 continue
       
    66             if node.attributes['type'].nodeValue != arg_value:
       
    67                 continue
       
    68         if draft:
       
    69             if node.attributes.has_key('draft'):
       
    70                 if node.attributes['draft'].nodeValue != draft:
       
    71                     continue
       
    72             elif draft != DraftResolution.approved:
       
    73                 continue
       
    74         return node
       
    75     return False
       
    76 
       
    77 def _findEntry(file, path, draft=None):
       
    78     doc = False
       
    79     if doc_cache.has_key(file):
       
    80         doc = doc_cache[file]
       
    81     else:
       
    82         doc = xml.dom.minidom.parse(file)
       
    83         doc_cache[file] = doc
       
    84 
       
    85     elt = doc.documentElement
       
    86     tag_spec_list = path.split("/")
       
    87     last_entry = None
       
    88     if draft is not None:
       
    89         last_entry = tag_spec_list[-1]
       
    90         tag_spec_list = tag_spec_list[:-1]
       
    91     for tag_spec in tag_spec_list:
       
    92         tag_name = tag_spec
       
    93         arg_value = ''
       
    94         left_bracket = tag_spec.find('[')
       
    95         if left_bracket != -1:
       
    96             tag_name = tag_spec[:left_bracket]
       
    97             arg_value = tag_spec[left_bracket+1:-1]
       
    98         elt = findChild(elt, tag_name, arg_value)
       
    99         if not elt:
       
   100             return ""
       
   101     if last_entry is not None:
       
   102         elt = findChild(elt, last_entry, '', draft)
       
   103         if not elt:
       
   104             return ""
       
   105     return elt.firstChild.nodeValue
       
   106 
       
   107 def findAlias(file):
       
   108     doc = doc_cache[file]
       
   109     alias_elt = findChild(doc.documentElement, "alias", "")
       
   110     if not alias_elt:
       
   111         return False
       
   112     if not alias_elt.attributes.has_key('source'):
       
   113         return False
       
   114     return alias_elt.attributes['source'].nodeValue
       
   115 
       
   116 def findEntry(base, path, draft=None):
       
   117     file = base + ".xml"
       
   118 
       
   119     if os.path.isfile(file):
       
   120         result = _findEntry(file, path, draft)
       
   121         if result:
       
   122             return result
       
   123 
       
   124         alias = findAlias(file)
       
   125         if alias:
       
   126             file = os.path.dirname(base) + "/" + alias + ".xml"
       
   127             if os.path.isfile(file):
       
   128                 result = _findEntry(file, path, draft)
       
   129                 if result:
       
   130                     return result
       
   131 
       
   132     file = base[:-3] + ".xml"
       
   133     if os.path.isfile(file):
       
   134         result = _findEntry(file, path, draft)
       
   135         if result:
       
   136             return result
       
   137         alias = findAlias(file)
       
   138         if alias:
       
   139             file = os.path.dirname(base) + "/" + alias + ".xml"
       
   140             if os.path.isfile(file):
       
   141                 result = _findEntry(file, path, draft)
       
   142                 if result:
       
   143                     return result
       
   144 
       
   145     if not draft:
       
   146         file = os.path.dirname(base) + "/root.xml"
       
   147         result = _findEntry(file, path, draft)
       
   148     return result
       
   149