Search in sources :

Example 36 with OutputFormat

use of org.dom4j.io.OutputFormat in project openolat by klemens.

the class FilePersister method createResultsReporting.

/**
 * Persist results for this user/aiid as an XML document. dlPointer is aiid in
 * this case.
 *
 * @param doc
 * @param type
 * @param info
 */
public static void createResultsReporting(Document doc, Identity subj, String type, long aiid) {
    File fUserdataRoot = new File(WebappHelper.getUserDataRoot());
    String path = RES_REPORTING + File.separator + subj.getName() + File.separator + type;
    File fReportingDir = new File(fUserdataRoot, path);
    try {
        fReportingDir.mkdirs();
        OutputStream os = new FileOutputStream(new File(fReportingDir, aiid + ".xml"));
        Element element = doc.getRootElement();
        XMLWriter xw = new XMLWriter(os, new OutputFormat("  ", true));
        xw.write(element);
        // closing steams
        xw.close();
        os.close();
    } catch (Exception e) {
        throw new OLATRuntimeException(FilePersister.class, "Error persisting results reporting for subject: '" + subj.getName() + "'; assessment id: '" + aiid + "'", e);
    }
}
Also used : OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) FileOutputStream(java.io.FileOutputStream) Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) File(java.io.File) XMLWriter(org.dom4j.io.XMLWriter) IOException(java.io.IOException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException)

Example 37 with OutputFormat

use of org.dom4j.io.OutputFormat in project openolat by klemens.

the class QTIExportProcessor method assembleTest.

/**
 * <li>List all items
 * <li>Rewrite path
 * <li>Assemble qti.xml
 * <li>Write files at new path
 * @param fullItems
 * @param zout
 */
public void assembleTest(List<QuestionItemFull> fullItems, ZipOutputStream zout) {
    ItemsAndMaterials itemAndMaterials = new ItemsAndMaterials();
    for (QuestionItemFull fullItem : fullItems) {
        collectMaterials(fullItem, itemAndMaterials);
    }
    try {
        byte[] buffer = new byte[FileUtils.BSIZE];
        // write qti.xml
        Element sectionEl = createSectionBasedAssessment("Assessment");
        for (Element itemEl : itemAndMaterials.getItemEls()) {
            // generate new ident per item
            String ident = getAttributeValue(itemEl, "ident");
            String exportIdent = QTIEditHelper.generateNewIdent(ident);
            itemEl.addAttribute("ident", exportIdent);
            sectionEl.add(itemEl);
        }
        zout.putNextEntry(new ZipEntry("qti.xml"));
        XMLWriter xw = new XMLWriter(zout, new OutputFormat("  ", true));
        xw.write(sectionEl.getDocument());
        zout.closeEntry();
        // write materials
        for (ItemMaterial material : itemAndMaterials.getMaterials()) {
            String exportPath = material.getExportUri();
            zout.putNextEntry(new ZipEntry(exportPath));
            InputStream in = material.getLeaf().getInputStream();
            int c;
            while ((c = in.read(buffer, 0, buffer.length)) != -1) {
                zout.write(buffer, 0, c);
            }
            IOUtils.closeQuietly(in);
            zout.closeEntry();
        }
    } catch (IOException e) {
        log.error("", e);
    }
}
Also used : QuestionItemFull(org.olat.modules.qpool.QuestionItemFull) InputStream(java.io.InputStream) Element(org.dom4j.Element) ZipEntry(java.util.zip.ZipEntry) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) XMLWriter(org.dom4j.io.XMLWriter)

Example 38 with OutputFormat

use of org.dom4j.io.OutputFormat 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();
    }
}
Also used : Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Metadata(com.zimbra.cs.mailbox.Metadata) XMLWriter(org.dom4j.io.XMLWriter) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) DavException(com.zimbra.cs.dav.DavException) QName(org.dom4j.QName) Color(com.zimbra.common.mailbox.Color) OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Type(com.zimbra.cs.mailbox.MailItem.Type) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 39 with OutputFormat

use of org.dom4j.io.OutputFormat 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 "";
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLWriter(org.dom4j.io.XMLWriter)

Example 40 with OutputFormat

use of org.dom4j.io.OutputFormat in project hibernate-orm by hibernate.

the class AdditionalJaxbMappingProducerImpl method dump.

private static void dump(Document document) {
    if (!log.isTraceEnabled()) {
        return;
    }
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final Writer w = new PrintWriter(baos);
    try {
        final XMLWriter xw = new XMLWriter(w, new OutputFormat(" ", true));
        xw.write(document);
        w.flush();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    log.tracef("Envers-generate entity mapping -----------------------------\n%s", baos.toString());
    log.trace("------------------------------------------------------------");
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XMLWriter(org.dom4j.io.XMLWriter) XMLWriter(org.dom4j.io.XMLWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Aggregations

OutputFormat (org.dom4j.io.OutputFormat)89 XMLWriter (org.dom4j.io.XMLWriter)75 IOException (java.io.IOException)39 Document (org.dom4j.Document)31 Element (org.dom4j.Element)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 StringWriter (java.io.StringWriter)18 SAXReader (org.dom4j.io.SAXReader)16 File (java.io.File)15 FileOutputStream (java.io.FileOutputStream)14 FileWriter (java.io.FileWriter)10 OutputStreamWriter (java.io.OutputStreamWriter)8 StringReader (java.io.StringReader)6 DocumentException (org.dom4j.DocumentException)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4