1*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2019 - The Android Open Source Project 2*c2e18aaaSAndroid Build Coastguard Worker# 3*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*c2e18aaaSAndroid Build Coastguard Worker# 7*c2e18aaaSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*c2e18aaaSAndroid Build Coastguard Worker# 9*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License. 14*c2e18aaaSAndroid Build Coastguard Worker 15*c2e18aaaSAndroid Build Coastguard Worker"""The dom_util module. 16*c2e18aaaSAndroid Build Coastguard Worker 17*c2e18aaaSAndroid Build Coastguard WorkerThis module is a collection of the help functions to operate with the minidom 18*c2e18aaaSAndroid Build Coastguard Workerrelevant objects. 19*c2e18aaaSAndroid Build Coastguard Worker""" 20*c2e18aaaSAndroid Build Coastguard Worker 21*c2e18aaaSAndroid Build Coastguard Worker# The temporary pylint disable statements are only used for the skeleton upload. 22*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=unused-argument 23*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=unnecessary-pass 24*c2e18aaaSAndroid Build Coastguard Worker 25*c2e18aaaSAndroid Build Coastguard Worker 26*c2e18aaaSAndroid Build Coastguard Workerdef find_special_node(parent, element_name, attributes=None): 27*c2e18aaaSAndroid Build Coastguard Worker """Finds the node that contains the specific element. 28*c2e18aaaSAndroid Build Coastguard Worker 29*c2e18aaaSAndroid Build Coastguard Worker Find the node of the element that contains the tag of element_name and the 30*c2e18aaaSAndroid Build Coastguard Worker attribute values listed in the attributes dict. There are two cases as 31*c2e18aaaSAndroid Build Coastguard Worker follows, 32*c2e18aaaSAndroid Build Coastguard Worker 1. The case with attributes is as below <component name="...">. 33*c2e18aaaSAndroid Build Coastguard Worker 2. The case without attributes is as below <findStrings>. 34*c2e18aaaSAndroid Build Coastguard Worker <component name="FindInProjectRecents"> 35*c2e18aaaSAndroid Build Coastguard Worker <findStrings> 36*c2e18aaaSAndroid Build Coastguard Worker <find>StackInfo</find> 37*c2e18aaaSAndroid Build Coastguard Worker <find>attachStartupAgents</find> 38*c2e18aaaSAndroid Build Coastguard Worker </findStrings> 39*c2e18aaaSAndroid Build Coastguard Worker </component> 40*c2e18aaaSAndroid Build Coastguard Worker 41*c2e18aaaSAndroid Build Coastguard Worker Args: 42*c2e18aaaSAndroid Build Coastguard Worker parent: A dom node object hierarchically contains the target_name 43*c2e18aaaSAndroid Build Coastguard Worker element object. 44*c2e18aaaSAndroid Build Coastguard Worker element_name: A string of the specific element name. 45*c2e18aaaSAndroid Build Coastguard Worker attributes: A dict of {'name': 'value', ...} for the attributes used to 46*c2e18aaaSAndroid Build Coastguard Worker match the element. 47*c2e18aaaSAndroid Build Coastguard Worker 48*c2e18aaaSAndroid Build Coastguard Worker Returns: 49*c2e18aaaSAndroid Build Coastguard Worker A node object if the element_name target is found, otherwise None. 50*c2e18aaaSAndroid Build Coastguard Worker """ 51*c2e18aaaSAndroid Build Coastguard Worker pass 52*c2e18aaaSAndroid Build Coastguard Worker 53*c2e18aaaSAndroid Build Coastguard Worker 54*c2e18aaaSAndroid Build Coastguard Workerdef compare_element_with_attributes(element, attributes=None): 55*c2e18aaaSAndroid Build Coastguard Worker """Compares whether the element contains the multiple attributes. 56*c2e18aaaSAndroid Build Coastguard Worker 57*c2e18aaaSAndroid Build Coastguard Worker Args: 58*c2e18aaaSAndroid Build Coastguard Worker element: A dom element object which is used to compare with. 59*c2e18aaaSAndroid Build Coastguard Worker attributes: A dict of {'name': 'value', ...} for the attributes used to 60*c2e18aaaSAndroid Build Coastguard Worker match the element. 61*c2e18aaaSAndroid Build Coastguard Worker 62*c2e18aaaSAndroid Build Coastguard Worker Returns: 63*c2e18aaaSAndroid Build Coastguard Worker boolean: True if the input element contains attributes the same with 64*c2e18aaaSAndroid Build Coastguard Worker the attributes, otherwise False. 65*c2e18aaaSAndroid Build Coastguard Worker """ 66*c2e18aaaSAndroid Build Coastguard Worker pass 67*c2e18aaaSAndroid Build Coastguard Worker 68*c2e18aaaSAndroid Build Coastguard Worker 69*c2e18aaaSAndroid Build Coastguard Workerdef update_element_attributes(node, element_name, attribute_set): 70*c2e18aaaSAndroid Build Coastguard Worker """Updates attribute values into the matched element in the node. 71*c2e18aaaSAndroid Build Coastguard Worker 72*c2e18aaaSAndroid Build Coastguard Worker Use the element_name and the dict in attribute_set to find the first 73*c2e18aaaSAndroid Build Coastguard Worker matched element and update its value with the last two items in 74*c2e18aaaSAndroid Build Coastguard Worker attribute_set which are the name and value. 75*c2e18aaaSAndroid Build Coastguard Worker 76*c2e18aaaSAndroid Build Coastguard Worker Example: 77*c2e18aaaSAndroid Build Coastguard Worker The following section demonstrates how to use this method to update 78*c2e18aaaSAndroid Build Coastguard Worker the PORT value to 3000. 79*c2e18aaaSAndroid Build Coastguard Worker 80*c2e18aaaSAndroid Build Coastguard Worker <component name="RunManager"> 81*c2e18aaaSAndroid Build Coastguard Worker <configuration name="aidegen_jvm" type="Remote"> 82*c2e18aaaSAndroid Build Coastguard Worker <module name="remote_template_test" /> 83*c2e18aaaSAndroid Build Coastguard Worker <option name="HOST" value="localhost" /> 84*c2e18aaaSAndroid Build Coastguard Worker <option name="PORT" value="5005" /> 85*c2e18aaaSAndroid Build Coastguard Worker </configuration> 86*c2e18aaaSAndroid Build Coastguard Worker </component> 87*c2e18aaaSAndroid Build Coastguard Worker 88*c2e18aaaSAndroid Build Coastguard Worker Args: 89*c2e18aaaSAndroid Build Coastguard Worker node: The minidom node object contains the child element to be updated. 90*c2e18aaaSAndroid Build Coastguard Worker In the example, it represents the RunManager component node. 91*c2e18aaaSAndroid Build Coastguard Worker 92*c2e18aaaSAndroid Build Coastguard Worker element_name: the string of the element tag, which is the 'option' in 93*c2e18aaaSAndroid Build Coastguard Worker the example. 94*c2e18aaaSAndroid Build Coastguard Worker 95*c2e18aaaSAndroid Build Coastguard Worker attribute_set: A set with 3 parts, ({'name': 'value', ,..}, name, value) 96*c2e18aaaSAndroid Build Coastguard Worker 97*c2e18aaaSAndroid Build Coastguard Worker {'name': 'value', ,..}: A dict of {'name': 'value', ...} for the 98*c2e18aaaSAndroid Build Coastguard Worker attributes used to match the element. 99*c2e18aaaSAndroid Build Coastguard Worker name: A string of the attribute name to update. 100*c2e18aaaSAndroid Build Coastguard Worker value: An object with integer or string type used to update to the 101*c2e18aaaSAndroid Build Coastguard Worker name attribute. 102*c2e18aaaSAndroid Build Coastguard Worker 103*c2e18aaaSAndroid Build Coastguard Worker In the example, the ({'name': 'PORT'}, 'value', 3000) is the value 104*c2e18aaaSAndroid Build Coastguard Worker of the attribute_set. 105*c2e18aaaSAndroid Build Coastguard Worker 106*c2e18aaaSAndroid Build Coastguard Worker Returns: 107*c2e18aaaSAndroid Build Coastguard Worker True if update is successful. 108*c2e18aaaSAndroid Build Coastguard Worker 109*c2e18aaaSAndroid Build Coastguard Worker Raises: 110*c2e18aaaSAndroid Build Coastguard Worker TypeError and AttributeError in bad input case. 111*c2e18aaaSAndroid Build Coastguard Worker """ 112*c2e18aaaSAndroid Build Coastguard Worker pass 113*c2e18aaaSAndroid Build Coastguard Worker 114*c2e18aaaSAndroid Build Coastguard Worker 115*c2e18aaaSAndroid Build Coastguard Workerdef update_element_with_condition(element, attributes, target_name, 116*c2e18aaaSAndroid Build Coastguard Worker target_value): 117*c2e18aaaSAndroid Build Coastguard Worker """Updates an element if it's fully matched with the compared condition. 118*c2e18aaaSAndroid Build Coastguard Worker 119*c2e18aaaSAndroid Build Coastguard Worker If all the attribute data of the element are the same as attributes, 120*c2e18aaaSAndroid Build Coastguard Worker assign target_value to the target_name attribute in it. 121*c2e18aaaSAndroid Build Coastguard Worker 122*c2e18aaaSAndroid Build Coastguard Worker Args: 123*c2e18aaaSAndroid Build Coastguard Worker element: The minidom element object. 124*c2e18aaaSAndroid Build Coastguard Worker attributes: A dict of {'name 1': 'value 1', ..., 'name n': 'value n'} 125*c2e18aaaSAndroid Build Coastguard Worker for the attributes of the element. 126*c2e18aaaSAndroid Build Coastguard Worker target_name: The string of the attribute name. 127*c2e18aaaSAndroid Build Coastguard Worker target_value: An integer or string used to set value. 128*c2e18aaaSAndroid Build Coastguard Worker 129*c2e18aaaSAndroid Build Coastguard Worker Returns: 130*c2e18aaaSAndroid Build Coastguard Worker boolean: False means there's no such element can be updated. 131*c2e18aaaSAndroid Build Coastguard Worker 132*c2e18aaaSAndroid Build Coastguard Worker Raises: 133*c2e18aaaSAndroid Build Coastguard Worker TypeError and AttributeError in bad input case. 134*c2e18aaaSAndroid Build Coastguard Worker """ 135*c2e18aaaSAndroid Build Coastguard Worker pass 136*c2e18aaaSAndroid Build Coastguard Worker 137*c2e18aaaSAndroid Build Coastguard Worker 138*c2e18aaaSAndroid Build Coastguard Workerdef insert_element_data(element, attributes, target_name, target_value): 139*c2e18aaaSAndroid Build Coastguard Worker """Inserts attribute data to an element. 140*c2e18aaaSAndroid Build Coastguard Worker 141*c2e18aaaSAndroid Build Coastguard Worker Args: 142*c2e18aaaSAndroid Build Coastguard Worker element: The minidom element object. 143*c2e18aaaSAndroid Build Coastguard Worker attributes: A dict of {'name 1': 'value 1', ..., 'name n': 'value n'} 144*c2e18aaaSAndroid Build Coastguard Worker for the attributes of the element. 145*c2e18aaaSAndroid Build Coastguard Worker target_name: The string of the attribute name. 146*c2e18aaaSAndroid Build Coastguard Worker target_value: An integer or string used to set value. 147*c2e18aaaSAndroid Build Coastguard Worker 148*c2e18aaaSAndroid Build Coastguard Worker Returns: 149*c2e18aaaSAndroid Build Coastguard Worker True if update is successful. 150*c2e18aaaSAndroid Build Coastguard Worker """ 151*c2e18aaaSAndroid Build Coastguard Worker pass 152