/*
* JFolder, Copyright 2001-2006 Gary Steinmetz
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jfolder.common.utils.xml;
//base classes
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
//project specific classes
import org.jfolder.common.UnexpectedSystemException;
import org.jfolder.common.utils.misc.MiscHelper;
//other classes
public final class XMLHelper {
//
public final static String GT_XML = ">";
public final static String LT_XML = "<";
public final static String AMP_XML = "&";
public final static String APOST_XML = "'";
public final static String QUOTE_XML = """;
//
public final static String NBSP_XML = " ";
public final static char NBSP_CHAR = (char)160;
public final static Document loadDocument(String inText)
throws ParserConfigurationException, SAXException, IOException {
//try {
Document outValue = null;
//
StringReader sr = new StringReader(inText);
InputSource is = new InputSource(sr);
//
//SAXBuilder builder = new SAXBuilder();
//org.jdom.Document doc = builder.build(is);
//
//DOMOutputter outputter = new DOMOutputter();
//outValue = outputter.output(doc);
//
//SAXReader reader = new SAXReader();
//reader.setValidation(false);
//org.dom4j.Document doc = reader.read(is);
//
//DOMWriter writer = new DOMWriter();
//outValue = writer.write(doc);
//
//org.dom4j.Document doc = writer.read(is);
//outValue = getDocumentBuilder().parse(is);
//String compareText = MiscHelper.readTextFile(
// new File("./config.xml"));
//MiscHelper.println("inText.length() = " + inText.length());
//MiscHelper.println("compareText.length() = "
// + compareText.length());
//for (int i = 0; i < 10; i++) {
// MiscHelper.println(compareText.equals(inText));
//}
//MiscHelper.writeTextFile(new File("./config.xml"), inText);
//outValue = getDocumentBuilder().parse(
// new File("./config.xml"));
outValue = getDocumentBuilder().parse(is);
//try {
// Builder builder = new Builder();
// nu.xom.Document doc = builder.build(sr);
// //
// outValue = DOMConverter.convert(
// doc, createBlankDocument().getImplementation());
//}
//catch (Exception e) {
// MiscHelper.writeMessage(XMLHelper.class, e);
//}
//for (int i = 0; i < 200; i++) {
// MiscHelper.println("Yeah! No XML parsing Exception");
//}
return outValue;
//}
//catch (JDOMException jdome) {
// throw new SAXException(jdome);
//}
}
public final static Document loadDocument(File inFile)
throws ParserConfigurationException, SAXException, IOException {
//try {
Document outValue = null;
//
//SAXBuilder builder = new SAXBuilder();
//org.jdom.Document doc = builder.build(inFile);
//
//DOMOutputter outputter = new DOMOutputter();
//outValue = outputter.output(doc);
//StringReader sr = new StringReader(inText);
//InputSource is = new InputSource(sr);
//
//SAXReader reader = new SAXReader();
//reader.setValidation(false);
//org.dom4j.Document doc = reader.read(inFile);
//
//DOMWriter writer = new DOMWriter();
//outValue = writer.write(doc);
//
//org.dom4j.Document doc = writer.read(is);
//outValue = getDocumentBuilder().parse(is);
outValue = getDocumentBuilder().parse(inFile);
//try {
// Builder builder = new Builder();
// nu.xom.Document doc = builder.build(inFile);
// //
// outValue = DOMConverter.convert(
// doc, createBlankDocument().getImplementation());
//}
//catch (Exception e) {
// MiscHelper.writeMessage(XMLHelper.class, e);
//}
return outValue;
//}
//catch (JDOMException jdome) {
// throw new SAXException(jdome);
//}
//Document outValue = getDocumentBuilder().parse(inFile);
//
//return outValue;
}
//public final static Document loadDocument(InputSource inIs)
// throws ParserConfigurationException, SAXException, IOException {
//
// //try {
// Document outValue = null;
// //
// //SAXBuilder builder = new SAXBuilder();
// //org.jdom.Document doc = builder.build(inIs);
// //
// //DOMOutputter outputter = new DOMOutputter();
// //outValue = outputter.output(doc);
// //StringReader sr = new StringReader(inText);
// //InputSource is = new InputSource(sr);
// //
// //SAXReader reader = new SAXReader();
// //reader.setValidation(false);
// //org.dom4j.Document doc = reader.read(inIs);
// //
// //DOMWriter writer = new DOMWriter();
// //outValue = writer.write(doc);
// //
// //org.dom4j.Document doc = writer.read(is);
// //outValue = getDocumentBuilder().parse(is);
//
// String compareText = MiscHelper.readTextFile(
// new File("./config.xml"));
// String compareSecondText = MiscHelper.readReader(
// inIs.getCharacterStream());
//
// for (int i = 0; i < 100; i++) {
// MiscHelper.println(
// "FILE == XML = " + compareText.equals(compareSecondText));
// }
//
// //outValue = getDocumentBuilder().parse(inIs);
// //
// //try {
// // Builder builder = new Builder();
// // nu.xom.Document doc = builder.build(inIs.getByteStream());
// // //
// // outValue = DOMConverter.convert(
// // doc, createBlankDocument().getImplementation());
// //}
// //catch (Exception e) {
// // MiscHelper.writeMessage(XMLHelper.class, e);
// //}
//
// return outValue;
// //}
// //catch (JDOMException jdome) {
// // throw new SAXException(jdome);
// //}
// //Document outValue = getDocumentBuilder().parse(inIs);
// //
// //return outValue;
//}
public final static void storeDocument(Document inDoc, File inFile)
throws IOException {
FileWriter fw = new FileWriter(inFile);
writeDocument(inDoc, fw);
fw.flush();
fw.close();
}
public final static Document createBlankDocument()
throws ParserConfigurationException {
//try {
Document outValue = null;
outValue = getDocumentBuilder().newDocument();
//
//DOMOutputter outputter = new DOMOutputter();
//outValue = outputter.output(new org.jdom.Document());
//outValue = new DOMDocument();
//outValue = getDocumentBuilder().parse(is);
return outValue;
//}
//catch (JDOMException jdome) {
// throw new SAXException(jdome);
//}
}
private final static DocumentBuilder getDocumentBuilder()
throws ParserConfigurationException {
DocumentBuilder outValue = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(false);
//MiscHelper.println("IS_NAMESPACE_AWARE = "
//+ dbf.isNamespaceAware());
outValue = dbf.newDocumentBuilder();
//
//reset to correct Xerces bug
//try {
// Class c = Class.forName(
// "org.apache.xerces.jaxp.DocumentBuilderImpl");
// Method m = c.getDeclaredMethod("reset", new Class[0]);
// m.invoke(outValue, new Object[0]);
// MiscHelper.println("---Resetting DocumentBuilderImpl");
//}
//catch (Exception e) {
// MiscHelper.writeMessage(XMLHelper.class, e);
//}
////
//MiscHelper.println("<<<<<<<<<<<<<<<QUICK TEST");
//try {
// outValue.parse(new File("./config.xml"));
//}
//catch (Exception e) {
// MiscHelper.writeMessage(XMLHelper.class, e);
//}
//MiscHelper.println("<<<<<<<<<<<<<<<QUICK TEST");
//
//outValue.setErrorHandler(new XMLErrorHandler());
return outValue;
}
public final static void writeDocument(Document inDoc, Writer inWriter)
throws IOException {
writeDocument(inDoc, inWriter, new HashMap(), false);
}
public final static void writeDocument(
Document inDoc, Writer inWriter, boolean inIndent) throws IOException {
writeDocument(inDoc, inWriter, new HashMap(), inIndent);
}
public final static void writeDocument(Document inDoc, Writer inWriter,
HashMap inNameSpaces) throws IOException {
writeDocument(inDoc, inWriter, new HashMap(), false);
}
public final static void writeDocument(Document inDoc, Writer inWriter,
HashMap inNameSpaces, boolean inIndent) throws IOException {
//MiscHelper.println("XMLHelper doc.getDoctEle() = "
// + inDoc.getDocumentElement());
StringBuffer sb = new StringBuffer();
writeDocument(inDoc.getDocumentElement(), sb,
initializeNameSpaceNames(inDoc, inNameSpaces), 0, true, inIndent,
new SpecializedXMLWriterHandler());
inWriter.write(sb.toString());
}
public final static HashMap initializeNameSpaceNames(
Document inDoc, HashMap inNameSpaces) {
HashMap outValue = new HashMap();
Iterator iter = inNameSpaces.keySet().iterator();
while (iter.hasNext()) {
String namespace = (String)iter.next();
String prefix = (String)inNameSpaces.get(namespace);
namespace = namespace.trim();
prefix = prefix.trim();
if (outValue.containsKey(namespace)) {
throw new UnexpectedSystemException(
"Namespace '" + namespace + "' already declared");
}
if (outValue.containsValue(prefix)) {
throw new UnexpectedSystemException(
"Namespace prefix '" + prefix + "' already declared");
}
outValue.put(namespace, prefix);
}
initializeNameSpaceNames(inDoc.getDocumentElement(), outValue);
return outValue;
}
private final static void initializeNameSpaceNames(
Element inElement, HashMap inNameSpaces) {
//MiscHelper.println("XMLHelper inElement = " + inElement);
assignNameSpacePrefix(inElement.getNamespaceURI(), inNameSpaces);
NamedNodeMap nnm = inElement.getAttributes();
for (int i = 0; i < nnm.getLength(); i++) {
Node nextNode = nnm.item(i);
if (nextNode instanceof Attr) {
Attr nextAttr = (Attr)nextNode;
assignNameSpacePrefix(nextAttr.getNamespaceURI(), inNameSpaces);
}
}
NodeList nl = inElement.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node nextNode = nl.item(i);
if (nextNode instanceof Element) {
initializeNameSpaceNames((Element)nextNode, inNameSpaces);
}
}
}
public final static void assignNameSpacePrefix(
String inNamespace, HashMap inNameSpaces) {
if (inNamespace != null && !inNameSpaces.containsKey(inNamespace)) {
for (int i = 1; true; i++) {
String candidatePrefix = "a" + i;
//String candidatePrefix = ((char)('A' + (i%26))) + "" + i;
if (!inNameSpaces.containsValue(candidatePrefix)) {
inNameSpaces.put(inNamespace, candidatePrefix);
break;
}
}
}
}
public final static void writeDocument(
Element inElement, StringBuffer inSb, HashMap inNamespaces,
SpecializedXMLWriterHandler inSxwh) throws IOException {
//
writeDocument(inElement, inSb, inNamespaces, 0, false, false, inSxwh);
}
private final static void writeDocument(
Element inElement, StringBuffer inSb, HashMap inNamespaces, int inDepth,
boolean inListNs, boolean inIndent, SpecializedXMLWriterHandler inSxwh)
throws IOException {
//initialize
String eleName = inElement.getTagName();
if (eleName.indexOf(':') != -1) {
eleName = eleName.substring(eleName.indexOf(':') + 1);
}
//String eleName = inElement.getLocalName();
NodeList eleNl = inElement.getChildNodes();
NamedNodeMap eleNnm = inElement.getAttributes();
String eleNs = inElement.getNamespaceURI();
String eleNsPrefix = null;
//determine if element contains other elements
boolean elementFound = false;
for (int i = 0; i < eleNl.getLength(); i++) {
if (eleNl.item(i) instanceof Element) {
elementFound = true;
break;
}
}
//write beginning of element
if (inIndent) {
if (!inListNs) {
inSb.append('\n');
}
for (int i = 0; i < inDepth; i++) {
inSb.append(' ');
}
}
//
inSb.append('<');
//write element with namespace, if present
if (eleNs != null) {
eleNsPrefix = ((String)inNamespaces.get(eleNs));
inSb.append(eleNsPrefix);
inSb.append(':');
inSb.append(eleName);
}
else {
inSb.append(eleName);
}
//declare namespaces
if (inListNs) {
Iterator iter = inNamespaces.keySet().iterator();
while (iter.hasNext()) {
String nextNamespace = ((String)iter.next());
String nextPrefix = ((String)inNamespaces.get(nextNamespace));
inSb.append(" xmlns:");
inSb.append(nextPrefix);
inSb.append("=\"");
inSb.append(nextNamespace);
inSb.append('\"');
}
}
//write attributes
for (int i = 0; i < eleNnm.getLength(); i++) {
Node nextNode = eleNnm.item(i);
if (nextNode instanceof Attr) {
//
Attr nextAttr = ((Attr)nextNode);
//
String nextAttrNs = nextAttr.getNamespaceURI();
String nextAttrName = nextAttr.getName();
String nextAttrData = nextAttr.getValue();
//write attribute with namespace, if present
if (inSxwh.isConstraintPrintable(inElement, nextAttr)) {
if (nextAttrNs != null) {
//
String nextAttrNsPrefix = null;
nextAttrNsPrefix =
((String)inNamespaces.get(nextAttrNs));
//
inSb.append(' ');
inSb.append(nextAttrNsPrefix);
inSb.append(':');
inSb.append(nextAttrName);
inSb.append('=');
inSb.append('\"');
inSb.append(fromStringToCData(nextAttrData, false));
inSb.append('\"');
}
else {
inSb.append(' ');
inSb.append(nextAttrName);
inSb.append('=');
inSb.append('\"');
inSb.append(fromStringToCData(nextAttrData, false));
inSb.append('\"');
}
}
}
}
//
//
//
if (eleNl.getLength() > 0) {
//
//
inSb.append('>');
//
//write element contents
for (int i = 0; i < eleNl.getLength(); i++) {
Node nextNode = eleNl.item(i);
if (nextNode instanceof Element) {
Element nextEle = (Element)nextNode;
//elementFound = true;
HashMap nextEleNsMap = ((HashMap)inNamespaces.clone());
//
if (inSxwh.isElementPrintable(nextEle, inSb)) {
//if (inSxwh.isElementPrintable(inElement, inSb)) {
writeDocument(
nextEle, inSb, nextEleNsMap, (inDepth + 4),
false, inIndent, inSxwh);
}
}
//new section
else if (nextNode instanceof CharacterData) {
CharacterData nextCd = (CharacterData)nextNode;
if (!(nextCd instanceof Comment)) {
String nextTextData = nextCd.getData();
inSb.append(fromStringToCData(nextTextData, true));
}
}
//end new section
}
//write ending of element
if (inIndent && elementFound) {
inSb.append('\n');
for (int i = 0; i < inDepth; i++) {
inSb.append(' ');
}
}
inSb.append('<');
inSb.append('/');
if (eleNs != null) {
inSb.append(eleNsPrefix);
inSb.append(':');
}
else {
}
inSb.append(eleName);
inSb.append('>');
}
else {
inSb.append("/>");
}
}
public final static String getAttrValue(Element inElement, String inName) {
String outValue = null;
Attr attr = inElement.getAttributeNode(inName);
if (attr != null) {
outValue = attr.getValue();
}
return outValue;
}
public final static String getAttrValue(
Element inElement, String inNamespace, String inName) {
String outValue = null;
Attr attr = inElement.getAttributeNodeNS(inNamespace, inName);
if (attr != null) {
outValue = attr.getValue();
}
return outValue;
}
public final static String fromStringToJSCData(String inInput) {
String outValue = null;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < inInput.length(); i++) {
char nextChar = inInput.charAt(i);
if (nextChar == '\'' || nextChar == '"'
|| nextChar == '\\' || nextChar == '&'
|| nextChar == '\n' || nextChar == '\r'
|| nextChar == '\t' || nextChar == '\f') {
//
sb.append('\\');
if (nextChar == '\'') {
sb.append(nextChar);
}
else if (nextChar == '"') {
sb.append(nextChar);
}
else if (nextChar == '\\') {
sb.append(nextChar);
}
else if (nextChar == '&') {
sb.append(nextChar);
}
else if (nextChar == '\n') {
sb.append('n');
}
else if (nextChar == '\r') {
sb.append('r');
}
else if (nextChar == '\t') {
sb.append('t');
}
else if (nextChar == '\f') {
sb.append('f');
}
else {
throw UnexpectedSystemException.unknownState();
}
}
else {
sb.append(nextChar);
}
//outValue.append("\\u");
//int baseLength = outValue.length();
//for (int j = 0; j < 4; j++) {
// int nextDigit = nextChar%16;
// nextChar = (char)(nextChar/16);
// outValue.insert(
// baseLength, Character.forDigit(nextDigit, 16));
//}
}
outValue = sb.toString();
//outValue = fromStringToCData(outValue);
return outValue;
}
public final static String fromStringToHTML(String inInput) {
String outValue = null;
outValue = fromStringToCData(inInput, true);
outValue = outValue.replaceAll(" ", " " + NBSP_XML);
return outValue;
}
public final static String fromStringToCData(String inInput) {
return fromStringToCData(inInput, true);
}
private final static String fromStringToCData(
String inInput, boolean inNotAttr) {
try {
StringWriter outValue = new StringWriter();
fromStringToCData(inInput, outValue, inNotAttr);
return outValue.toString();
}
catch (IOException ioe) {
throw new UnexpectedSystemException(ioe);
}
}
private final static void fromStringToCData(
String inInput, Writer inWriter, boolean inNotAttr) throws IOException {
if (inInput == null) {
MiscHelper.println("XMLHelper fromStrToCData null found");
}
StringTokenizer st = new StringTokenizer(
inInput, "&<>\"" + NBSP_CHAR, true);
// inInput, "&<>'\"" + NBSP_CHAR, true);
while (st.hasMoreTokens()) {
String nextToken = st.nextToken();
char testChar = nextToken.charAt(0);
if ((testChar == '&' && inNotAttr)
|| testChar == '<' || testChar == '>'
//|| testChar == '\''
|| testChar == '"'
|| testChar == NBSP_CHAR) {
for (int i = 0; i < nextToken.length(); i++) {
char nextChar = nextToken.charAt(i);
if (nextChar == '&' && inNotAttr) {
inWriter.write(AMP_XML);
}
else if (nextChar == '<') {
inWriter.write(LT_XML);
}
else if (nextChar == '>') {
inWriter.write(GT_XML);
}
//else if (nextChar == '\'') {
// inWriter.write(APOST_XML);
//}
else if (nextChar == '"') {
inWriter.write(QUOTE_XML);
}
else if (nextChar == NBSP_CHAR) {
inWriter.write(NBSP_XML);
}
else {
throw new UnexpectedSystemException("Character '"
+ nextChar + "' cannot be converted to an XML"
+ " escape character");
}
}
}
else {
inWriter.write(nextToken);
}
}
}
public final static String getText(Element inElement) {
String outValue = "";
NodeList nl = inElement.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node nextNode = nl.item(i);
if (nextNode instanceof CharacterData) {
if (!(nextNode instanceof Comment)) {
CharacterData cd = (CharacterData)nextNode;
outValue += cd.getData();
}
}
}
return outValue;
}
public final static String convertAttrs(HashMap inProps) {
StringBuffer outValue = new StringBuffer();
Iterator attrMap = inProps.keySet().iterator();
while (attrMap.hasNext()) {
String propName = (String)attrMap.next();
String propValue = (String)inProps.get(propName);
propValue = fromStringToCData(propValue, false);
outValue.append(" ");
outValue.append(propName);
outValue.append("=\"");
outValue.append(propValue);
outValue.append("\"");
}
return outValue.toString();
}
//public final static String convertStyles(HashMap inProps) {
//
// StringBuffer outValue = new StringBuffer();
//
// if (inProps.size() > 0) {
// //TO DO: 'style' attribute is assumed, will this cause problem?
// outValue.append(" style=\"");
// outValue.append(fromStylesToAttr(inProps));
// outValue.append("\"");
// }
//
// return outValue.toString();
//}
public final static String fromStylesToAttr(HashMap inProps) {
StringBuffer outValue = new StringBuffer();
Iterator attrMap = inProps.keySet().iterator();
while (attrMap.hasNext()) {
String propName = (String)attrMap.next();
String propValue = (String)inProps.get(propName);
outValue.append(" ");
outValue.append(propName);
outValue.append(": ");
outValue.append(propValue);
outValue.append(";");
}
return outValue.toString();
}
//
//
//
public final static String reformatTextForWebPage(String inValue) {
//
StringBuffer outValue = new StringBuffer();
//make sure Windows newline doesn't show up twice
//String nextData = inCtchar.getValueAsString(inIndex, null);
inValue = inValue.replaceAll("\\r\\n", "\n");
//
//inCtchar.removeValueWithLocalFiles(inIndex);
//
ArrayList futureData = new ArrayList();
StringTokenizer st = new StringTokenizer(inValue, "\r\n", true);
while (st.hasMoreTokens()) {
futureData.add(st.nextToken());
}
//
for (int i = futureData.size() - 1; i >= 0; i--) {
//
String updateData = ((String)futureData.get(i));
//
if (updateData.equals("\n") || updateData.equals("\r")) {
//
outValue.insert(0, "<br/>");
//ConceptTag unknownCt = new UnknownWebPageTag(null, "br");
//inCtchar.addValue(inIndex, unknownCt, true);
}
else {
updateData = updateData.replaceAll(
" ", " " + XMLHelper.NBSP_XML);
outValue.insert(0, updateData);
//updateData = //updateData.replace(' ', XMLHelper.NBSP_CHAR);
//inCtchar.addValue(inIndex, updateData);
}
}
return outValue.toString();
}
}
|