Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.caliper; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | |
| 22 | import org.w3c.dom.Attr; |
| 23 | import org.w3c.dom.Element; |
| 24 | import org.w3c.dom.NamedNodeMap; |
| 25 | import org.w3c.dom.Node; |
| 26 | import org.w3c.dom.NodeList; |
| 27 | |
| 28 | public final class XmlUtils { |
| 29 | public static ImmutableList<Node> childrenOf(Node node) { |
| 30 | NodeList children = node.getChildNodes(); |
| 31 | ImmutableList.Builder<Node> result = ImmutableList.builder(); |
| 32 | for (int i = 0, size = children.getLength(); i < size; i++) { |
| 33 | result.add(children.item(i)); |
| 34 | } |
| 35 | return result.build(); |
| 36 | } |
| 37 | |
| 38 | public static ImmutableMap<String, String> attributesOf(Element element) { |
| 39 | NamedNodeMap map = element.getAttributes(); |
| 40 | ImmutableMap.Builder<String, String> result = ImmutableMap.builder(); |
| 41 | for (int i = 0, size = map.getLength(); i < size; i++) { |
| 42 | Attr attr = (Attr) map.item(i); |
| 43 | result.put(attr.getName(), attr.getValue()); |
| 44 | } |
| 45 | return result.build(); |
| 46 | } |
| 47 | |
| 48 | private XmlUtils() {} |
| 49 | } |