2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.io.parsers.phyloxml;
28 import java.util.ArrayList;
29 import java.util.HashMap;
31 import org.forester.io.parsers.util.PhylogenyParserException;
32 import org.forester.util.ForesterUtil;
33 import org.xml.sax.Attributes;
35 public class XmlElement {
37 public final static boolean DEBUG = false;
38 private final String _namespaceUri;
39 private final String _localName;
40 private final String _qualifiedName;
41 private String _value;
42 private final HashMap<String, String> _attributes;
43 private final ArrayList<XmlElement> _childElements;
44 private XmlElement _parent;
46 public XmlElement( final String namespaceUri,
47 final String localName,
48 final String qualifiedName,
49 final Attributes attributes ) {
50 _namespaceUri = namespaceUri;
51 _localName = localName;
52 _qualifiedName = qualifiedName;
53 if ( attributes != null ) {
54 _attributes = new HashMap<String, String>( attributes.getLength() );
55 for( int i = 0; i < attributes.getLength(); ++i ) {
56 getAttributes().put( new String( attributes.getQName( i ) ), new String( attributes.getValue( i ) ) );
60 _attributes = new HashMap<String, String>();
62 _childElements = new ArrayList<XmlElement>();
66 public void addChildElement( final XmlElement element ) {
67 element.setParent( this );
68 getChildElements().add( element );
71 public void appendValue( final String value ) {
72 _value = _value + value;
75 public String getAttribute( final String attribute_name ) {
76 if ( !isHasAttribute( attribute_name ) ) {
77 throw new IllegalArgumentException( "no attribute named [" + attribute_name + "] present in element ["
78 + getQualifiedName() + "]" );
80 return getAttributes().get( attribute_name );
83 public HashMap<String, String> getAttributes() {
87 public XmlElement getChildElement( final int i ) {
88 if ( ( i < 0 ) || ( i >= getNumberOfChildElements() ) ) {
89 throw new IllegalArgumentException( "attempt to get child element with index " + i + " for element with "
90 + getNumberOfChildElements() + " child elements" );
92 return getChildElements().get( i );
95 ArrayList<XmlElement> getChildElements() {
96 return _childElements;
99 String getLocalName() {
103 String getNamespaceUri() {
104 return _namespaceUri;
107 public int getNumberOfChildElements() {
108 return getChildElements().size();
111 public XmlElement getParent() {
115 public String getQualifiedName() {
116 return _qualifiedName;
119 XmlElement getRoot() {
121 while ( e.getParent() != null ) {
127 public boolean getValueAsBoolean() throws PhylogenyParserException {
130 b = ( new Boolean( getValueAsString() ) ).booleanValue();
132 catch ( final NumberFormatException ex ) {
133 throw new PhylogenyParserException( "attempt to parse [" + getValueAsString() + "] into boolean, in "
139 public double getValueAsDouble() throws PhylogenyParserException {
142 d = Double.parseDouble( getValueAsString() );
144 catch ( final NumberFormatException ex ) {
145 throw new PhylogenyParserException( "attempt to parse [" + getValueAsString() + "] into double, in "
151 public int getValueAsInt() throws PhylogenyParserException {
154 i = Integer.parseInt( getValueAsString() );
156 catch ( final NumberFormatException ex ) {
157 throw new PhylogenyParserException( "attempt to parse [" + getValueAsString() + "] into integer, in "
163 public String getValueAsString() {
164 if ( _value == null ) {
167 return _value.replaceAll( "\\s+", " " ).trim();
170 public boolean isHasAttribute( final String attribute_name ) {
171 return getAttributes().containsKey( attribute_name );
174 public boolean isHasValue() {
175 return !ForesterUtil.isEmpty( _value );
178 void setParent( final XmlElement parent ) {
183 * [Careful, this does not call "new String(...)"]
187 public void setValue( final String value ) {
189 if ( XmlElement.DEBUG ) {
190 System.out.println();
191 System.out.println( "Value is \"" + value + "\" for" );
192 System.out.println( "Local name = " + getLocalName() );
193 System.out.println( "Qualified name = " + getQualifiedName() );
194 System.out.println( "Namespace URI = " + getNamespaceUri() );
195 System.out.print( "Attributes : " );
196 for( final String string : getAttributes().keySet() ) {
197 final String key = string;
198 System.out.print( key + " = \"" + getAttributes().get( key ) + "\" " );
200 System.out.println();
201 System.out.println();
206 public String toString() {
207 if ( getParent() != null ) {
208 return "\"" + getQualifiedName() + "\" [value: " + getValueAsString() + ", parent element: \""
209 + getParent().getQualifiedName() + "\"]";
211 return "\"" + getQualifiedName() + "\" [value: " + getValueAsString() + "]";