1 /* 2 * Copyright (C) 2007 Esmertec AG. 3 * Copyright (C) 2007 The Android Open Source Project 4 * 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 18 package com.android.im.imps; 19 20 import java.util.ArrayList; 21 import java.util.Collections; 22 import java.util.HashMap; 23 import java.util.Map; 24 25 /** 26 * Represents a XML element of Primitive. Note that this class is not 27 * thread-safe. 28 */ 29 final public class PrimitiveElement { 30 private String mTagName; 31 private HashMap<String, String> mAttributes; 32 private ArrayList<PrimitiveElement> mChildren; 33 private String mContents; 34 35 public PrimitiveElement(String tagName) { 36 mTagName = tagName; 37 } 38 39 public String getTagName() { 40 return mTagName; 41 } 42 43 public void setTagName(String tagName) { 44 this.mTagName = tagName; 45 } 46 47 public Map<String, String> getAttributes() { 48 if (mAttributes == null) { 49 return null; 50 } 51 return Collections.unmodifiableMap(mAttributes); 52 } 53 54 public void setAttribute(String key, String value) { 55 if (key != null && value != null) { 56 if (mAttributes == null) { 57 mAttributes = new HashMap<String, String>(); 58 } 59 mAttributes.put(key, value); 60 } 61 } 62 63 public ArrayList<PrimitiveElement> getChildren() { 64 if (mChildren == null) { 65 mChildren = new ArrayList<PrimitiveElement>(); 66 } 67 return mChildren; 68 } 69 70 public ArrayList<PrimitiveElement> getChildren(String tagName) { 71 ArrayList<PrimitiveElement> children = new ArrayList<PrimitiveElement>(); 72 73 for (PrimitiveElement child : getChildren()) { 74 if (tagName.equals(child.getTagName())) { 75 children.add(child); 76 } 77 } 78 79 return children; 80 } 81 82 public PrimitiveElement getChild(String tagName) { 83 for (PrimitiveElement child : getChildren()) { 84 if (tagName.equals(child.getTagName())) { 85 return child; 86 } 87 } 88 return null; 89 } 90 91 public String getChildContents(String tagName) { 92 PrimitiveElement child = getChild(tagName); 93 return child == null ? null : child.getContents(); 94 } 95 96 public int getChildCount() { 97 if (mChildren == null || mChildren.isEmpty()) { 98 return 0; 99 } else { 100 return mChildren.size(); 101 } 102 } 103 104 public PrimitiveElement getFirstChild() { 105 if ((mChildren == null) || mChildren.isEmpty()) { 106 return null; 107 } 108 return mChildren.get(0); 109 } 110 111 public PrimitiveElement addChild(PrimitiveElement child) { 112 if (child != null) { 113 getChildren().add(child); 114 } 115 116 return child; 117 } 118 119 public PrimitiveElement addChild(String tagName) { 120 if (null == tagName) { 121 return null; 122 } 123 PrimitiveElement element = new PrimitiveElement(tagName); 124 getChildren().add(element); 125 return element; 126 } 127 128 public void addChild(String tagName, String contents) { 129 PrimitiveElement element = addChild(tagName); 130 if (null != contents) { 131 element.setContents(contents); 132 } 133 } 134 135 public void addChild(String tagName, boolean value) { 136 addChild(tagName).setContents(value ? 137 ImpsConstants.TRUE : ImpsConstants.FALSE); 138 } 139 140 public void addPropertyChild(String name, String value) 141 { 142 PrimitiveElement ret = addChild(ImpsTags.Property); 143 ret.addChild(ImpsTags.Name, name); 144 ret.addChild(ImpsTags.Value, value); 145 } 146 147 public void addPropertyChild(String name, boolean value) 148 { 149 PrimitiveElement ret = addChild(ImpsTags.Property); 150 ret.addChild(ImpsTags.Name, name); 151 ret.addChild(ImpsTags.Value, value); 152 } 153 154 public String getContents() { 155 return mContents; 156 } 157 158 public void setContents(String contents) { 159 mContents = contents; 160 } 161 } 162