6 * Copyright (C) 2006 The Jmol Development Team
8 * Contact: jmol-developers@lists.sf.net
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.
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.
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
28 public class XmlUtil {
31 // Jmol's PropertyManager class uses reflection
33 // / simple Xml parser/generator ///
35 public static void openDocument(SB data) {
36 data.append("<?xml version=\"1.0\"?>\n");
39 public static void openTag(SB sb, String name) {
40 sb.append("<").append(name).append(">\n");
43 public static void openTagAttr(SB sb, String name, Object[] attributes) {
44 appendTagAll(sb, name, attributes, null, false, false);
48 public static void closeTag(SB sb, String name) {
49 sb.append("</").append(name).append(">\n");
52 public static void appendTagAll(SB sb, String name,
53 Object[] attributes, Object data,
54 boolean isCdata, boolean doClose) {
56 if (name.endsWith("/")){
57 name = name.substring(0, name.length() - 1);
63 sb.append("<").append(name);
64 if (attributes != null)
65 for (int i = 0; i < attributes.length; i++) {
66 Object o = attributes[i];
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]);
73 appendAttrib(sb, o, attributes[++i]);
78 data = wrapCdata(data);
86 * wrap the string as character data, with replacements for [ noted
87 * as a list starting with * after the CDATA termination
90 * @return wrapped text
92 public static String wrapCdata(Object data) {
94 return (s.indexOf("&") < 0 && s.indexOf("<") < 0 ? (s.startsWith("\n") ? "" : "\n") + s
95 : "<![CDATA[" + PT.rep(s, "]]>", "]]]]><![CDATA[>") + "]]>");
100 * @return unwrapped text
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);
108 * standard <name attr="..." attr="...">data</name>"
115 public static void appendTagObj(SB sb, String name,
116 Object[] attributes, Object data) {
117 appendTagAll(sb, name, attributes, data, false, true);
121 * standard <name>data</name>"
122 * standard <name attr="..." attr="..."></name>"
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);
132 appendTagAll(sb, name, null, data, false, true);
136 * <name><![CDATA[data]]></name>"
138 * will convert ]]> to ]] >
145 public static void appendCdata(SB sb, String name,
146 Object[] attributes, String data) {
147 appendTagAll(sb, name, attributes, data, true, true);
156 public static void appendAttrib(SB sb, Object name, Object value) {
160 // note: <&" are disallowed but not checked for here
162 sb.append(" ").appendO(name).append("=\"").appendO(value).append("\"");