edu.mines.jtk.util.XmlUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of edu-mines-jtk Show documentation
Show all versions of edu-mines-jtk Show documentation
Java packages for science and engineering
The newest version!
/****************************************************************************
Copyright 2006, Colorado School of Mines and others.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/
package edu.mines.jtk.util;
/**
* Utilities for XML formatting.
* @author Dave Hale, Colorado School of Mines
* @version 06/25/1998, 08/24/2006.
*/
class XmlUtil {
/**
* Quotes an XML attribute value.
* Replaces special characters and XML entities.
* Encloses string in double or single quotes, depending on what's inside.
* @param s attribute value to be quoted.
* @return quoted attribute value.
*/
public static String quoteAttributeValue(String s) {
if (s==null) return null;
s = replaceAll("&","&",s);
s = replaceAll("<","<",s);
s = replaceAll("\r","
",s);
s = replaceAll("\n","
",s);
s = replaceAll("\t"," ",s);
String quote = "\"";
if (s.contains("\"") && !s.contains("\'")) {
quote = "\'";
} else {
s = replaceAll("\"",""",s);
}
return quote+s+quote;
}
/**
* Quotes character data.
* Replaces special characters and XML entities.
* Encloses string in double quotes if it contains whitespace.
* @param s character data to be quoted.
* @return quoted character data.
*/
public static String quoteCharacterData(String s) {
final char space = '\u0020';
if (s==null) return null;
s = replaceAll("&","&",s);
s = replaceAll("<","<",s);
s = replaceAll("\\","\\\\",s);
s = replaceAll("\r","\\r",s);
s = replaceAll("\n","\\n",s);
s = replaceAll("\t","\\t",s);
s = replaceAll("\"","\\\"",s);
s = replaceAll("\'","\\\'",s);
String quote = "";
if (s.length()==0) {
quote = "\"";
} else {
for (int i=0; i=0) {
d.append(s.substring(from,to));
d.append(y);
from = to+x.length();
to = s.indexOf(x,from);
}
return d.append(s.substring(from)).toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy