use of org.dom4j.io.XMLWriter in project zm-mailbox by Zimbra.
the class DavRequest method getRequestMessageString.
public String getRequestMessageString() throws IOException {
if (mDoc != null) {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setTrimText(false);
format.setOmitEncoding(false);
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLWriter writer = new XMLWriter(out, format);
writer.write(mDoc);
return new String(out.toByteArray(), "UTF-8");
}
return "";
}
use of org.dom4j.io.XMLWriter in project zm-mailbox by Zimbra.
the class MailItemResource method patchProperties.
/* Modifies the set of dead properties saved for this resource.
* Properties in the parameter 'set' are added to the existing properties.
* Properties in 'remove' are removed.
*/
@Override
public void patchProperties(DavContext ctxt, java.util.Collection<Element> set, java.util.Collection<QName> remove) throws DavException, IOException {
List<QName> reqProps = new ArrayList<QName>();
for (QName n : remove) {
mDeadProps.remove(n);
reqProps.add(n);
}
for (Element e : set) {
QName name = e.getQName();
if (name.equals(DavElements.E_DISPLAYNAME) && (type == MailItem.Type.FOLDER || type == MailItem.Type.MOUNTPOINT)) {
// rename folder
try {
String val = e.getText();
String uri = getUri();
Mailbox mbox = getMailbox(ctxt);
mbox.rename(ctxt.getOperationContext(), mId, type, val, mFolderId);
setProperty(DavElements.P_DISPLAYNAME, val);
UrlNamespace.addToRenamedResource(getOwner(), uri, this);
UrlNamespace.addToRenamedResource(getOwner(), uri.substring(0, uri.length() - 1), this);
} catch (ServiceException se) {
ctxt.getResponseProp().addPropError(DavElements.E_DISPLAYNAME, new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
}
mDeadProps.remove(name);
continue;
} else if (name.equals(DavElements.E_CALENDAR_COLOR) && (type == MailItem.Type.FOLDER || type == MailItem.Type.MOUNTPOINT)) {
// change color
String colorStr = e.getText();
Color color = new Color(colorStr.substring(0, 7));
byte col = (byte) COLOR_LIST.indexOf(colorStr);
if (col >= 0)
color.setColor(col);
try {
Mailbox mbox = getMailbox(ctxt);
mbox.setColor(ctxt.getOperationContext(), new int[] { mId }, type, color);
} catch (ServiceException se) {
ctxt.getResponseProp().addPropError(DavElements.E_CALENDAR_COLOR, new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
}
mDeadProps.remove(name);
continue;
} else if (name.equals(DavElements.E_SUPPORTED_CALENDAR_COMPONENT_SET)) {
// change default view
@SuppressWarnings("unchecked") List<Element> elements = e.elements(DavElements.E_COMP);
boolean isTodo = false;
boolean isEvent = false;
for (Element element : elements) {
Attribute attr = element.attribute(DavElements.P_NAME);
if (attr != null && CalComponent.VTODO.name().equals(attr.getValue())) {
isTodo = true;
} else if (attr != null && CalComponent.VEVENT.name().equals(attr.getValue())) {
isEvent = true;
}
}
if (isEvent ^ isTodo) {
// we support a calendar collection of type event or todo, not both or none.
Type type = (isEvent) ? Type.APPOINTMENT : Type.TASK;
try {
Mailbox mbox = getMailbox(ctxt);
mbox.setFolderDefaultView(ctxt.getOperationContext(), mId, type);
// See UrlNamespace.addToRenamedResource()
if (this instanceof Collection) {
((Collection) this).view = type;
}
} catch (ServiceException se) {
ctxt.getResponseProp().addPropError(name, new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
}
} else {
ctxt.getResponseProp().addPropError(name, new DavException.CannotModifyProtectedProperty(name));
}
continue;
}
mDeadProps.put(name, e);
reqProps.add(name);
}
String configVal = "";
if (mDeadProps.size() > 0) {
org.dom4j.Document doc = org.dom4j.DocumentHelper.createDocument();
Element top = doc.addElement(CONFIG_KEY);
for (Map.Entry<QName, Element> entry : mDeadProps.entrySet()) top.add(entry.getValue().detach());
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputFormat format = OutputFormat.createCompactFormat();
XMLWriter writer = new XMLWriter(out, format);
writer.write(doc);
configVal = new String(out.toByteArray(), "UTF-8");
if (configVal.length() > PROP_LENGTH_LIMIT)
for (Map.Entry<QName, Element> entry : mDeadProps.entrySet()) ctxt.getResponseProp().addPropError(entry.getKey(), new DavException("prop length exceeded", DavProtocol.STATUS_INSUFFICIENT_STORAGE));
}
Mailbox mbox = null;
try {
mbox = getMailbox(ctxt);
mbox.lock.lock();
Metadata data = mbox.getConfig(ctxt.getOperationContext(), CONFIG_KEY);
if (data == null) {
data = new Metadata();
}
data.put(Integer.toString(mId), configVal);
mbox.setConfig(ctxt.getOperationContext(), CONFIG_KEY, data);
} catch (ServiceException se) {
for (QName qname : reqProps) ctxt.getResponseProp().addPropError(qname, new DavException(se.getMessage(), HttpServletResponse.SC_FORBIDDEN));
} finally {
if (mbox != null)
mbox.lock.release();
}
}
use of org.dom4j.io.XMLWriter in project ats-framework by Axway.
the class XmlText method getFormattedString.
private String getFormattedString(OutputFormat format) throws XMLException {
StringWriter sw = new StringWriter();
XMLWriter writer = new XMLWriter(sw, format);
try {
writer.write(this.root.getDocument());
} catch (IOException e) {
throw new XMLException("Error returning formatted XMLText", e);
} finally {
try {
writer.flush();
writer.close();
} catch (IOException e) {
}
}
return sw.toString();
}
use of org.dom4j.io.XMLWriter in project cubrid-manager by CUBRID.
the class FormatXMLAction method writeTo.
/**
* Format XML strings to a output stream.
*
* @param out OutputStream
* @param content the content to be formated.
* @param encoding String
* @throws DocumentException when document error raised.
* @throws IOException when IO errors.
*/
public static void writeTo(OutputStream out, String content, String encoding) throws DocumentException, IOException {
StringReader reader = new StringReader(content);
SAXReader xmlReader = new SAXReader();
Document doc = xmlReader.read(reader);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding(encoding);
format.setIndent(true);
format.setIndent(" ");
format.setIndentSize(4);
XMLWriter writer = new XMLWriter(out, format);
writer.write(doc);
writer.flush();
writer.close();
reader.close();
}
use of org.dom4j.io.XMLWriter in project CodeUtils by boredream.
the class XmlUtil method write2xml.
/**
* 将Document写回至xml文件
* @param file
* @param doc
*/
public static void write2xml(File file, Document doc) {
XMLWriter writer;
try {
writer = new XMLWriter(new FileOutputStream(file));
writer.write(doc);
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Aggregations