diff src/goodjava/xml/XmlElement.java @ 1790:a8c685a894b4

start xml work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 25 Dec 2023 23:07:59 -0700
parents 21f5edab1fbf
children f8f5c51f5b36
line wrap: on
line diff
--- a/src/goodjava/xml/XmlElement.java	Sun Dec 24 16:52:35 2023 -0700
+++ b/src/goodjava/xml/XmlElement.java	Mon Dec 25 23:07:59 2023 -0700
@@ -6,7 +6,7 @@
 public final class XmlElement {
 	public final String name;
 	public final Map<String,String> attributes;
-	public final Object content;
+	private Object content = null;
 
 	public XmlElement(String name,Map<String,String> attributes) {
 		this.name = name;
@@ -14,31 +14,35 @@
 		this.content = null;
 	}
 
-	public XmlElement(String name,Map<String,String> attributes,String content) {
+	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.name = name;
-		this.attributes = attributes;
 		this.content = content;
 	}
 
-	public XmlElement(String name,Map<String,String> attributes,XmlElement[] 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.name = name;
-		this.attributes = attributes;
 		this.content = content;
 	}
 
-	public String toString() {
+	@Override public String toString() {
 		StringBuilder sb = new StringBuilder();
 		toString(sb,0);
 		return sb.toString();
 	}
 
-	private void toString(StringBuilder sb,int indented) {
+	void toString(StringBuilder sb,int indented) {
 		indent(sb,indented);
 		sb.append( '<' );
 		sb.append( name );
@@ -100,6 +104,12 @@
 			case '\'':
 				buf.append("&apos;");
 				break;
+			case '\n':
+				buf.append("&#10;");
+				break;
+			case '\r':
+				buf.append("&#13;");
+				break;
 			default:
 				buf.append(c);
 			}