3e30e4b0cc84f867ff8f62ffe00dc34c11ec530c
[jalview.git] / srcjar / javajs / util / XmlUtil.java
1 /* $RCSfile$
2  * $Author$
3  * $Date$
4  * $Revision$
5  *
6  * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017
7  * for use in SwingJS via transpilation into JavaScript using Java2Script.
8  *
9  * Copyright (C) 2006  The Jmol Development Team
10  *
11  * Contact: jmol-developers@lists.sf.net
12  *
13  *  This library is free software; you can redistribute it and/or
14  *  modify it under the terms of the GNU Lesser General Public
15  *  License as published by the Free Software Foundation; either
16  *  version 2.1 of the License, or (at your option) any later version.
17  *
18  *  This library is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  *  Lesser General Public License for more details.
22  *
23  *  You should have received a copy of the GNU Lesser General Public
24  *  License along with this library; if not, write to the Free Software
25  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26  *  02110-1301, USA.
27  */
28
29 package javajs.util;
30
31 /**
32  * A very simplistic XML generator
33  */
34
35 public class XmlUtil {
36
37   public XmlUtil() {
38     // Jmol's PropertyManager and JvxlCoder classes use reflection 
39   }
40   public static void openDocument(SB data) {
41     data.append("<?xml version=\"1.0\"?>\n");
42   }
43
44   public static void openTag(SB sb, String name) {
45     sb.append("<").append(name).append(">\n");
46   }
47
48   public static void openTagAttr(SB sb, String name, Object[] attributes) {
49     appendTagAll(sb, name, attributes, null, false, false);
50     sb.append("\n");
51   }
52
53   public static void closeTag(SB sb, String name) {
54     sb.append("</").append(name).append(">\n");
55   }
56
57   public static void appendTagAll(SB sb, String name,
58                                Object[] attributes, Object data,
59                                boolean isCdata, boolean doClose) {
60     String closer = ">";
61     if (name.endsWith("/")){
62       name = name.substring(0, name.length() - 1);
63       if (data == null) {
64         closer = "/>\n";
65         doClose = false;
66       }
67     }
68     sb.append("<").append(name);
69     if (attributes != null)
70       for (int i = 0; i < attributes.length; i++) {
71         Object o = attributes[i];
72         if (o == null)
73           continue;
74         if (o instanceof Object[])
75           for (int j = 0; j < ((Object[]) o).length; j+= 2)
76           appendAttrib(sb, ((Object[]) o)[j], ((Object[]) o)[j + 1]);
77         else
78           appendAttrib(sb, o, attributes[++i]);
79       }
80     sb.append(closer);
81     if (data != null) {
82       if (isCdata)
83         data = wrapCdata(data);
84       sb.appendO(data);
85     }
86     if (doClose)
87       closeTag(sb, name);
88   }
89
90   /**
91    * wrap the string as character data, with replacements for [ noted 
92    * as a list starting with * after the CDATA termination
93    * 
94    * @param data
95    * @return      wrapped text
96    */
97   public static String wrapCdata(Object data) {
98     String s = "" + data;
99     return (s.indexOf("&") < 0 && s.indexOf("<") < 0 ? (s.startsWith("\n") ? "" : "\n") + s 
100         : "<![CDATA[" + PT.rep(s, "]]>", "]]]]><![CDATA[>") + "]]>");
101   }
102   
103   /**
104    * standard <name attr="..." attr="...">data</name>"
105    * 
106    * @param sb
107    * @param name
108    * @param attributes
109    * @param data
110    */
111   public static void appendTagObj(SB sb, String name,
112                                Object[] attributes, Object data) {
113     appendTagAll(sb, name, attributes, data, false, true);
114   }
115
116   /**
117    * standard <name>data</name>"
118    * standard <name attr="..." attr="..."></name>"
119    * 
120    * @param sb
121    * @param name
122    * @param data
123    */
124   public static void appendTag(SB sb, String name, Object data) {
125     if (data instanceof Object[])
126       appendTagAll(sb, name, (Object[]) data, null, false, true);
127     else
128       appendTagAll(sb, name, null, data, false, true);
129   }
130
131   /**
132    * <name><![CDATA[data]]></name>"
133    * 
134    * will convert ]]> to ]] >
135    * 
136    * @param sb
137    * @param name
138    * @param attributes 
139    * @param data
140    */
141   public static void appendCdata(SB sb, String name, 
142                                  Object[] attributes, String data) {
143     appendTagAll(sb, name, attributes, data, true, true);
144   }
145
146   /**
147    * 
148    * @param sb
149    * @param name
150    * @param value
151    */
152   public static void appendAttrib(SB sb, Object name, Object value) {
153     if (value == null)
154       return;
155     
156     // note: <&" are disallowed but not checked for here
157     
158     sb.append(" ").appendO(name).append("=\"").appendO(value).append("\"");
159   }
160
161 //  /**
162 //   * @param s
163 //   * @return   unwrapped text
164 //   */
165 //  public static String unwrapCdata(String s) {
166 //    return (s.startsWith("<![CDATA[") && s.endsWith("]]>") ?
167 //        PT.rep(s.substring(9, s.length()-3),"]]]]><![CDATA[>", "]]>") : s);
168 //  }
169 //  
170
171 }