Mercurial Hosting > luan
view src/goodjava/xml/XmlElement.java @ 1822:aeaf2b65c005
better parse tables
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 09 Jul 2024 12:46:27 -0600 |
parents | 9daae8cf4bef |
children |
line wrap: on
line source
package goodjava.xml; import java.util.Map; public final class XmlElement { public final String name; public final Map<String,String> attributes; private Object content = null; private XmlElement parent = null; public XmlElement(String name,Map<String,String> attributes) { this.name = name; this.attributes = attributes; this.content = null; } public XmlElement getParent() { return parent; } public Object getContent() { return content; } public void removeContent() { content = null; } public void setContent(String content) { if( content == null ) throw new IllegalArgumentException("content of '"+name+"' can't be null"); this.content = content; } public void setContent(XmlElement[] content) { if( content == null ) throw new IllegalArgumentException("content of '"+name+"' can't be null"); this.content = content; for( XmlElement el : content ) { el.parent = this; } } @Override public String toString() { StringBuilder sb = new StringBuilder(); toString(sb,0); return sb.toString(); } void toString(StringBuilder sb,int indented) { indent(sb,indented); sb.append( '<' ); sb.append( name ); for( Map.Entry<String,String> attribute : attributes.entrySet() ) { sb.append( ' ' ); sb.append( attribute.getKey() ); sb.append( "=\"" ); sb.append( encodeAttr(attribute.getValue()) ); sb.append( '"' ); } if( content == null ) { sb.append( "/>\n" ); } else if( content instanceof String ) { sb.append( '>' ); String s = (String)content; sb.append( encode(s) ); closeTag(sb,name); } else { sb.append( '>' ); XmlElement[] elements = (XmlElement[])content; sb.append( '\n' ); for( XmlElement element : elements ) { element.toString(sb,indented+1); } indent(sb,indented); closeTag(sb,name); } } private static void closeTag(StringBuilder sb,String name) { sb.append( "</" ); sb.append( name ); sb.append( ">\n" ); } private static void indent(StringBuilder sb,int indented) { for( int i=0; i<indented; i++ ) { sb.append('\t'); } } public static String encode(String s) { final char[] a = s.toCharArray(); StringBuilder buf = new StringBuilder(); for( char c : a ) { switch(c) { case '&': buf.append("&"); break; case '<': buf.append("<"); break; case '>': buf.append(">"); break; case '"': buf.append("""); break; case '\'': buf.append("'"); break; default: buf.append(c); } } return buf.toString(); } public static String encodeAttr(String s) { final char[] a = s.toCharArray(); StringBuilder buf = new StringBuilder(); for( char c : a ) { switch(c) { case '&': buf.append("&"); break; case '<': buf.append("<"); break; case '>': buf.append(">"); break; case '"': buf.append("""); break; case '\'': buf.append("'"); break; case '\n': buf.append(" "); break; case '\r': buf.append(" "); break; default: buf.append(c); } } return buf.toString(); } }