Mercurial Hosting > luan
view src/goodjava/xml/XmlElement.java @ 1792:a5f62fe28b3e
regex.pattern
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 26 Dec 2023 20:57:19 -0700 |
parents | f8f5c51f5b36 |
children | a55a891a4f67 |
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 can't be null"); this.content = content; } public void setContent(XmlElement[] content) { if( content == null ) throw new IllegalArgumentException("content can't be null"); if( content.length == 0 ) throw new IllegalArgumentException("content can't be empty"); 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( encode(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; case '\n': buf.append(" "); break; case '\r': buf.append(" "); break; default: buf.append(c); } } return buf.toString(); } }