001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared;
034:
035: import com.thoughtworks.xstream.XStream;
036: import com.thoughtworks.xstream.io.xml.CompactWriter;
037:
038: import javax.xml.stream.XMLStreamWriter;
039: import javax.xml.stream.XMLStreamException;
040: import java.io.StringWriter;
041: import java.io.Writer;
042:
043: /**
044: * XML Utilities
045: *
046: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
047: */
048: public class FxXMLUtils {
049:
050: /**
051: * Get the character content of an element in xml data
052: *
053: * @param xml the complete xml data
054: * @param element requested element
055: * @return data
056: */
057: public static String getElementData(String xml, String element) {
058: if (xml == null)
059: return null;
060: int start = xml.indexOf("<" + element + ">");
061: if (start <= 0)
062: return null;
063: int end = xml.indexOf("</", start);
064: if (start <= 0)
065: return null;
066: String ret = xml.substring(start + element.length() + 2, end);
067: // Strip CData if needed
068: // if (ret.startsWith("<![CDATA["))
069: // ret = ret.substring(9, ret.length() - 3);
070: return fromCData(ret);
071: }
072:
073: /**
074: * Convert a content to CDATA and preserve all existing CDATA
075: *
076: * @param content content to convert
077: * @return character data encoded content
078: */
079: public static String toCData(String content) {
080: if (content.contains("]]>")) {
081: //take care of nested CDATA sections
082: return "<![CDATA["
083: + content
084: .replaceAll("\\]\\]\\>", "]]]]><![CDATA[>")
085: + "]]>";
086: } else
087: return "<![CDATA[" + content + "]]>";
088: }
089:
090: /**
091: * Convert a CData back to a String
092: *
093: * @param cdata CData to convert
094: * @return String
095: */
096: public static String fromCData(String cdata) {
097: StringBuilder sb = new StringBuilder(cdata);
098: int idx;
099: while ((idx = sb.indexOf("<![CDATA[")) > 0) {
100: int close = sb.indexOf("]]>");
101: if (close > 0) {
102: sb.delete(idx, idx + 9);
103: sb.delete(close - 9, close - 9 + 3);
104: }
105: }
106: return sb.toString();
107: }
108:
109: /**
110: * Convenience method to create a more compact XML representation
111: * of an object than {@link com.thoughtworks.xstream.XStream#toXML(Object)} creates (no pretty-printing).
112: *
113: * @param xStream the xstream instance
114: * @param object the object to be marshalled
115: * @return the XML representation of the object
116: */
117: public static String toXML(XStream xStream, Object object) {
118: Writer writer = new StringWriter();
119: xStream.marshal(object, new CompactWriter(writer));
120: return writer.toString();
121: }
122:
123: /**
124: * Write a "simple" tag
125: *
126: * @param writer xml writer
127: * @param tag tag name
128: * @param value value
129: * @param asCData use CDData?
130: * @throws javax.xml.stream.XMLStreamException on errors
131: */
132: public static void writeSimpleTag(XMLStreamWriter writer,
133: String tag, Object value, boolean asCData)
134: throws XMLStreamException {
135: writer.writeStartElement(tag);
136: if (asCData)
137: writer.writeCData(String.valueOf(value));
138: else
139: writer.writeCharacters(String.valueOf(value));
140: writer.writeEndElement();
141: }
142: }
|