001 /* 002 * To change this template, choose Tools | Templates 003 * and open the template in the editor. 004 */ 005 006 package org.util.xml.element; 007 008 import java.util.ArrayList; 009 010 /** 011 * 012 * @author masaru 013 */ 014 public class TagElement extends Element { 015 016 @Override public boolean isTagElement() { return true; } 017 @Override public boolean isTextElement() { return false; } 018 019 private String key_; 020 private Attributes attributes_; 021 Element[] children_; 022 private boolean is_empty_; 023 private boolean is_pi_; 024 025 public TagElement(String key) { 026 if(key == null) throw new NullPointerException("key is null"); 027 key_ = key; 028 attributes_ = Attributes.EmptySet; 029 children_ = null; 030 } 031 public TagElement(String key, String value) { 032 if(key == null) throw new NullPointerException("key is null"); 033 key_ = key; 034 attributes_ = Attributes.EmptySet; 035 setValue(value); 036 } 037 038 public String getKey() { 039 return key_; 040 } 041 042 043 044 // attribute 045 public void addAttribute(Attribute attribute) { 046 if(attributes_==Attributes.EmptySet) 047 attributes_ = new Attributes(); 048 attributes_.add(attribute); 049 } 050 public void setAttribute(Attribute attribute) { 051 if(attributes_==Attributes.EmptySet) 052 attributes_ = new Attributes(); 053 attributes_.set(attribute); 054 } 055 public void setAttribute(String name, String value) { 056 setAttribute(new Attribute(name, value)); 057 } 058 public void setAttributes(Attributes attributes) { 059 if(attributes==null) 060 attributes = Attributes.EmptySet; 061 else 062 attributes_ = attributes; 063 } 064 public Attributes getAttributes() { 065 return attributes_; 066 } 067 public String getAttributeValue(String attribute_name) { 068 return attributes_.getValue(attribute_name); 069 } 070 public String getAttributeValue(String attribute_name, String return_if_return_value_is_null) { 071 return attributes_.getValue(attribute_name, return_if_return_value_is_null); 072 } 073 074 075 076 // children 077 public void setChildren(Element... children) { 078 for(int i=0;i<children.length;i++) 079 if(children[i]==null) { 080 children = removeNullElements(children); 081 break; 082 } 083 setEmpty(false); 084 setPI(false); 085 children_ = children; 086 } 087 public void addChild(Element child) { 088 addChildren(child); 089 } 090 public void addChildren(Element... children) { 091 Element[] oldchildren = getChildren(); 092 Element[] newchildren = new Element[oldchildren.length+children.length]; 093 System.arraycopy(oldchildren, 0, newchildren, 0, oldchildren.length); 094 System.arraycopy(children, 0, newchildren, oldchildren.length, children.length); 095 setChildren(newchildren); 096 } 097 public Element[] getChildren() { 098 return children_; 099 } 100 public TagElement[] getTagChildren() { 101 int tag_count = 0; 102 if(children_==null) return null; 103 for(int i=0;i<children_.length;i++) 104 if(children_[i].isTagElement()) tag_count++; 105 TagElement[] tag_children = new TagElement[tag_count]; 106 tag_count = 0; 107 for(int i=0;i<children_.length;i++) 108 if(children_[i].isTagElement()) 109 tag_children[tag_count++] = (TagElement)children_[i]; 110 return tag_children; 111 } 112 public TagElement getTagChild(String key) { 113 for(int i=0;i<children_.length;i++) 114 if(children_[i].isTagElement()) 115 if(((TagElement)children_[i]).getKey().equals(key)) 116 return (TagElement)children_[i]; 117 return null; 118 } 119 public TagElement[] getTagChildren(String key) { 120 ArrayList<TagElement> list = new ArrayList<TagElement>(); 121 for(int i=0;i<children_.length;i++) 122 if(children_[i].isTagElement()) { 123 TagElement tag = ((TagElement)children_[i]); 124 if(tag.getKey().equals(key)) 125 list.add(tag); 126 } 127 return list.toArray(new TagElement[]{}); 128 } 129 130 public String getChildValue(String key) { 131 return getChildValue(key, null); 132 } 133 public String getChildValue(String key, String return_if_value_is_null) { 134 TagElement tag = getTagChild(key); 135 136 if(tag==null) return return_if_value_is_null; 137 138 String value = tag.getValue(); 139 if(value!=null) 140 return value; 141 else 142 return return_if_value_is_null; 143 144 } 145 public String[] getChildValues(String key) { 146 return getChildValues(key, null); 147 } 148 public String[] getChildValues(String key, String return_if_value_is_null) { 149 TagElement[] tag = getTagChildren(key); 150 String[] result = new String[tag.length]; 151 152 if(tag==null) return new String[]{return_if_value_is_null}; 153 154 for(int i=0;i<tag.length;i++) { 155 String value = tag[i].getValue(); 156 result[i] = (value!=null ? value : return_if_value_is_null); 157 } 158 return result; 159 } 160 161 162 /** 163 * all text value 164 * @return 165 */ 166 public String getValue() { 167 if(children_==null || children_.length==0) return null; 168 StringBuffer sb = new StringBuffer(); 169 for(int i=0;i<children_.length;i++) 170 if(children_[i].isTextElement()) 171 sb.append(((TextElement)children_[i]).getValue()); 172 return sb.toString(); 173 } 174 public void setValue(String value) { 175 setChildren(new TextElement(value)); 176 } 177 178 179 180 181 // flags 182 public void setEmpty(boolean is_empty) { 183 is_empty_ = is_empty; 184 } 185 public boolean isEmpty() { 186 return is_empty_; 187 } 188 public void setPI(boolean is_pi) { 189 is_pi_ = is_pi; 190 } 191 /** 192 * <div>PI tag or xml definition</div> 193 * <div> 194 * <? ... ?><br/> 195 * e.x.<br/> 196 * <?xml version="1.0" encoding="UTF-8"?><br/> 197 * </div> 198 * @return is pi tag 199 */ 200 public boolean isPI() { 201 return is_pi_; 202 } 203 204 public static Element[] removeNullElements(Element[] elements) { 205 ArrayList<Element> list = new ArrayList<Element>(); 206 for(int i=0;i<elements.length;i++) 207 if(elements != null) 208 list.add(elements[i]); 209 return list.toArray(new Element[]{}); 210 } 211 212 // toString 213 public String toString() { 214 return toString(0); 215 } 216 public String toString(int s) { 217 String space = putTab(s); 218 StringBuffer sb = new StringBuffer(space); 219 sb.append("<"); 220 if(is_pi_) sb.append("?"); 221 sb.append(key_); 222 if(attributes_.size()>0) 223 sb.append(" ").append(attributes_); 224 225 if(is_pi_) { 226 sb.append("?>\n"); 227 return sb.toString(); 228 }else if(is_empty_) { 229 sb.append("/>\n"); 230 return sb.toString(); 231 } else { 232 sb.append(">"); 233 if(children_==null || children_.length==1 && children_[0] instanceof TextElement); 234 else sb.append("\n"); 235 } 236 237 if(children_ != null){ 238 for(int i=0;i<children_.length;i++) { 239 if(children_[i] != null) 240 sb.append(children_[i].toString(s+1)); 241 else 242 sb.append("<!-- Null object (this is bug! children must not be Null) -->"); 243 } 244 } 245 if(children_==null || children_.length==1 && children_[0] instanceof TextElement); 246 else sb.append(space); 247 sb.append("</").append(key_).append(">\n"); 248 return sb.toString(); 249 } 250 }