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