blob: 29c0c4f76905e2469b566eba0954722e4ac389ee [file] [log] [blame]
The Android Open Source Projectf8057102009-03-15 16:47:16 -07001#! /usr/bin/python
2#
Brian Muramatsu40bdc5e2010-08-13 16:20:15 -07003# Copyright 2008, The Android Open Source Project
The Android Open Source Projectf8057102009-03-15 16:47:16 -07004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# This script is used to split the jdiff xml into several smaller xml files
18# so that we could avoid the xml resource limit in Android platform.
19#
20# Usage:
21# android_api_description.py xmlfile tagname
22#
23# The script will do the following:
24# 1. Read the xml file and generate DOM tree
25# 2. Generate xml file for each tagname.
26#
27# Example:
28# xml source:
29# <Root>
30# <A name="i">
31# <B>1</B>
32# <B>2</B>
33# </A>
34# <A name="ii">
35# <B>3</B>
36# </A>
37# </Root>
38#
39# when the tagname is specified as A, it will generate two xml files:
40# first one's source:
41# <Root>
42# <A name="i">
43# <B>1</B>
44# <B>2</B>
45# </A>
46# </Root>
47# second one's source:
48# <Root>
49# <A name="ii">
50# <B>3</B>
51# </A>
52# </Root>
53#
54# when the tagname is specified as B, it will generated three xml files:
55# first one's source:
56# <Root>
57# <A name="i">
58# <B>1</B>
59# </A>
60# </Root>
61# second one's source:
62# <Root>
63# <A name="i">
64# <B>2</B>
65# </A>
66# </Root>
67# third one's source:
68# <Root>
69# <A name="ii">
70# <B>3</B>
71# </A>
72# </Root>
73#
74# NOTE:
75# 1. Currently just suppor the top level element
76# 2. Use the name attribute of the specified element as the file name
77# 3. Currently will remove all the doc element. - workaround for jdiff xml
78#
79import os, sys;
80import xml.dom.minidom;
81
82"""Split the jdiff xml into several smaller xml files by specified tag.
83"""
84class XMLSplitter:
85 def __init__(self, xmlfile, outPath):
86 self.doc = xml.dom.minidom.parse(xmlfile)
87 self.root = self.doc.documentElement
88 self.out = os.path.join(outPath, "xml")
89 if not os.path.isdir(self.out):
90 os.makedirs(self.out)
91 return
92
93 def split(self, tag):
94
95 elemlist = self.doc.getElementsByTagName(tag)
96
97 for elem in elemlist:
98 elem = self.__trimElem(elem)
99 self.__generateFile(elem)
100
101 return
102
103 def __trimElem(self, elem):
104 children = []
105 for child in elem.childNodes:
106 if child.nodeType == xml.dom.minidom.Node.ELEMENT_NODE:
107 children.append(child)
108
109 for child in children:
110 if child.nodeName == "doc":
111 elem.removeChild(child)
112 children.remove(child)
113
114 for child in children:
115 child = self.__trimElem(child)
116
117 return elem
118
119
120 def __generateFile(self, elem):
121 self.__removeAllChild(self.root)
122
123 filename = os.path.join(self.out, elem.getAttribute("name").replace(".", "_").lower() + ".xml")
124
125 doc = xml.dom.minidom.Document()
126 doc.appendChild(self.root)
127
128 self.root.appendChild(elem)
129
130 fd = open(filename, "w")
131 fd.write(doc.toxml())
132 fd.close
133
134 return
135
136 def __removeAllChild(self, elem):
137 children = []
138 for child in elem.childNodes:
139 children.append(child)
140
141 for child in children:
142 elem.removeChild(child)
143
144 return
145
146if __name__ == "__main__":
147 if len(sys.argv) < 4:
148 print "Usage: splitxml.py xmlfile outpath tagname"
149 sys.exit(1)
150
151 xmlsplitter = XMLSplitter(sys.argv[1], sys.argv[2])
152
153 xmlsplitter.split(sys.argv[3])
154