org.coos.actorframe.XmlBuilder Maven / Gradle / Ivy
/**
* COOS - Connected Objects Operating System (www.connectedobjects.org).
*
* Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*
* You may also contact one of the following for additional information:
* Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
* Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
*/
package org.coos.actorframe;
import java.util.Vector;
import org.coos.javaframe.ActorAddress;
import org.coos.javaframe.TraceObject;
import org.coos.javaframe.messages.AFPropertyMsg;
import org.coos.javaframe.messages.ActorMsg;
public class XmlBuilder {
private static String QUOTE = "\"";
/**
* Create XML representation of TraceObject.
*
* If the trace objects logger has trace enabled, i.e. logger.isTraceEnabled(),
* then message contents will also be included.
*
* @param to the TraceObject instance
* @return XML as string
*/
public static String toXML(TraceObject to) {
Vector v;
boolean includeContent = to.getLogger() == null ? false : to.getLogger().isTraceEnabled();
StringBuffer sb = new StringBuffer();
sb.append("");
if (to.getInputSignal() != null) {
sb.append("");
sb.append(toXML(to.getInputSignal(), includeContent));
sb.append("");
}
if (to.getOutputSignals() != null && to.getOutputSignals().size() > 0) {
v = to.getOutputSignals();
sb.append("");
}
if (to.getTraceTask() != null && to.getTraceTask().length() > 0) {
sb.append("");
sb.append(toXmlString(to.getTraceTask()));
sb.append(" ");
}
if (to.getErrors() != null && to.getErrors().size() > 0) {
v = to.getErrors();
sb.append("");
for (int i = 0; i < v.size(); i++) {
sb.append("");
sb.append(toXmlString((String) v.elementAt(i)));
sb.append(" ");
}
sb.append(" ");
}
if (to.getWarnings() != null && to.getWarnings().size() > 0) {
v = to.getWarnings();
sb.append("");
for (int i = 0; i < v.size(); i++) {
sb.append("");
sb.append(toXmlString((String) v.elementAt(i)));
sb.append(" ");
}
sb.append(" ");
}
sb.append(" ");
return sb.toString();
}
/**
* Create XML representation of ActorMsg.
*
* @param msg the message
* @param includeContent if true, message content will also be included
* @return XML as string
*/
public static String toXML(ActorMsg msg, boolean includeContent) {
StringBuffer sb = new StringBuffer();
sb.append("");
if (msg instanceof AFPropertyMsg) {
AFPropertyMsg afMsg = (AFPropertyMsg) msg;
sb.append("");
if (afMsg.getProperty() != null) {
sb.append(toXmlString(afMsg.getProperty().toString()));
}
sb.append(" ");
}
sb.append(" ");
return sb.toString();
}
static String quoted(int integer) {
return QUOTE + integer + QUOTE;
}
static String quoted(Object o) {
if (o == null) {
return QUOTE + QUOTE;
}
return QUOTE + toXmlString(o.toString()) + QUOTE;
}
/*
* CLDC does not support s.replace(String, String).
* Therefore, instead of s.replace("&", "&"),
* doing s.replace('&', '-'). Only to ensure that
* the resulting XML can be processed later.
*/
static String toXmlString(String s) {
String t = s.replace('&', '-')
.replace('<', '[')
.replace('>', ']');
return t;
}
}