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.
9 * Copyright (C) 2006 The Jmol Development Team
11 * Contact: jmol-developers@lists.sf.net
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.
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.
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
32 * A very simplistic XML generator
35 public class XmlUtil {
38 // Jmol's PropertyManager and JvxlCoder classes use reflection
40 public static void openDocument(SB data) {
41 data.append("<?xml version=\"1.0\"?>\n");
44 public static void openTag(SB sb, String name) {
45 sb.append("<").append(name).append(">\n");
48 public static void openTagAttr(SB sb, String name, Object[] attributes) {
49 appendTagAll(sb, name, attributes, null, false, false);
53 public static void closeTag(SB sb, String name) {
54 sb.append("</").append(name).append(">\n");
57 public static void appendTagAll(SB sb, String name,
58 Object[] attributes, Object data,
59 boolean isCdata, boolean doClose) {
61 if (name.endsWith("/")){
62 name = name.substring(0, name.length() - 1);
68 sb.append("<").append(name);
69 if (attributes != null)
70 for (int i = 0; i < attributes.length; i++) {
71 Object o = attributes[i];
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]);
78 appendAttrib(sb, o, attributes[++i]);
83 data = wrapCdata(data);
91 * wrap the string as character data, with replacements for [ noted
92 * as a list starting with * after the CDATA termination
95 * @return wrapped text
97 public static String wrapCdata(Object data) {
99 return (s.indexOf("&") < 0 && s.indexOf("<") < 0 ? (s.startsWith("\n") ? "" : "\n") + s
100 : "<![CDATA[" + PT.rep(s, "]]>", "]]]]><![CDATA[>") + "]]>");
104 * standard <name attr="..." attr="...">data</name>"
111 public static void appendTagObj(SB sb, String name,
112 Object[] attributes, Object data) {
113 appendTagAll(sb, name, attributes, data, false, true);
117 * standard <name>data</name>"
118 * standard <name attr="..." attr="..."></name>"
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);
128 appendTagAll(sb, name, null, data, false, true);
132 * <name><![CDATA[data]]></name>"
134 * will convert ]]> to ]] >
141 public static void appendCdata(SB sb, String name,
142 Object[] attributes, String data) {
143 appendTagAll(sb, name, attributes, data, true, true);
152 public static void appendAttrib(SB sb, Object name, Object value) {
156 // note: <&" are disallowed but not checked for here
158 sb.append(" ").appendO(name).append("=\"").appendO(value).append("\"");
163 // * @return unwrapped text
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);