Example usage for org.dom4j DocumentType setSystemID

List of usage examples for org.dom4j DocumentType setSystemID

Introduction

In this page you can find the example usage for org.dom4j DocumentType setSystemID.

Prototype

void setSystemID(String systemID);

Source Link

Usage

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

/**
 * Sets the XML DOCTYPE/*w w  w  .  ja  v  a 2  s.c o  m*/
 *
 * @param document The Exchanger document
 * @param name The DOCTYPE name (should be root element's name)
 * @param type The type (SYSTEM,PUBLIC or INTERNAL)
 * @param publicID The publicID 
 * @param systemID The systemID
 * 
 */
public static void setXMLDoctype(ExchangerDocument document, String name, String type, String publicID,
        String systemID) {
    XDocument xdoc = document.getDocument();
    DocumentType dt = xdoc.getDocType();

    if (dt != null) {
        // update existing Doctype
        dt.setElementName(name);
        if (type.equals(INTERNAL)) {
            dt.setPublicID(null);
            dt.setSystemID(null);
        } else if (type.equals(SYSTEM)) {
            dt.setPublicID(null);
            dt.setSystemID(systemID);
        } else {
            dt.setPublicID(publicID);
            dt.setSystemID(systemID);

        }
    } else {
        // add new doctype
        if (type.equals(INTERNAL)) {
            xdoc.addDocType(name, null, null);
        } else if (type.equals(SYSTEM)) {
            xdoc.addDocType(name, null, systemID);
        } else {
            xdoc.addDocType(name, publicID, systemID);
        }

    }
}

From source file:com.tedi.engine.XMLOutput.java

License:Open Source License

/**
 * Set the system id./*  w w w  .  j av a2  s.  c  o  m*/
 * 
 * @param rootStr
 *            Root string
 */
private void setSystemID(String rootStr) {

    if (logger.isDebugEnabled()) {
        logger.debug("Setting system ID to the element- " + rootStr);
    }

    // set systemID
    if (dtd == null || dtd.length() == 0)
        return;
    String systemId = "";
    // if system ID is a full URL, use it, else just use DTD name
    try {
        absoluteDTD_URL = new URL(dtd);
        systemId = absoluteDTD_URL.toString();
    } catch (MalformedURLException me) {
        systemId = Utils.getFileComponent(dtd);
        try {
            absoluteDTD_URL = new File("dtd", systemId).toURL();
        } catch (Exception e) {
            String errStr = e.getMessage();
            logger.error(errStr);
            throw new RuntimeException(e);
        }
    }
    if (dtd.toLowerCase().endsWith("dtd")) {
        doc.addDocType(null, null, null);
        DocumentType type = doc.getDocType();
        type.setSystemID(systemId);
        type.setElementName(rootStr);
    }
}

From source file:org.alfresco.repo.security.permissions.impl.model.PermissionModel.java

License:Open Source License

private InputStream processModelDocType(InputStream is, String dtdSchemaUrl)
        throws DocumentException, IOException {
    SAXReader reader = new SAXReader();
    // read document without validation
    Document doc = reader.read(is);
    DocumentType docType = doc.getDocType();
    if (docType != null) {
        // replace DOCTYPE setting the full path to the xsd
        docType.setSystemID(dtdSchemaUrl);
    } else {/*  w w  w.  ja  v a  2 s . c om*/
        // add the DOCTYPE
        docType = new DefaultDocumentType(doc.getRootElement().getName(), dtdSchemaUrl);
        doc.setDocType(docType);
    }

    ByteArrayOutputStream fos = new ByteArrayOutputStream();
    try {
        OutputFormat format = OutputFormat.createPrettyPrint(); // uses UTF-8
        XMLWriter writer = new XMLWriter(fos, format);
        writer.write(doc);
        writer.flush();
    } finally {
        fos.close();
    }

    return new ByteArrayInputStream(fos.toByteArray());
}

From source file:org.mitre.jawb.gui.ReferenceEditor.java

License:Open Source License

private void save() {
    File temp = null;//from   w  w  w. ja va 2  s  .  c  om
    // check our values
    if (aifURI == null || "".equals(dtdField.getText()) || "".equals(maiaField.getText())
            || "".equals(signalField.getText())) {
        GUIUtils.beep();
        return;
    }
    try {
        // make a temp copy replacing some URL's w/ external references
        Document doc = ATLASHelper.parse(aifURI);
        DocumentType doctype = doc.getDocType();
        Element corpus = doc.getRootElement();
        Element signal = corpus.element("SimpleSignal"); // first and only

        doctype.setSystemID(dtdField.getText());
        corpus.addAttribute("schemeLocation", maiaField.getText());
        signal.addAttribute("href", signalField.getText());

        temp = File.createTempFile("callisto", ".aif.xml");
        if (DEBUG >= 0)
            System.err.println("RefEdit.save: creating temp .aif in:\n    " + temp);

        OutputStream out = new FileOutputStream(temp);
        ATLASHelper.dump(doc, out);
        out.close();

        // ...and replace the ATLAS generated version
        File aif = new File(new URI(aifURI.toASCIIString()));
        File backup = new File(aif.toString() + "~");
        if (!(aif.renameTo(backup) && temp.renameTo(aif))) {
            System.err.println(
                    "RefEdit.Error rewriting aif: " + aifURI + "\n Failed to replace w/: " + temp.getPath());
            // renaming failed, without exception, so delete the temp file
            // catch exceptions so temp is nulled.
            try {
                temp.delete();
            } catch (Exception x) {
                /* ignore */ }
        }
        temp = null;

        // this exception was caused by renameTo, or earlier, in which case
        // the user's saved file is not the externalized version... but temp
        // still exists, and should be deleted if it does. The 'catch' is just
        // a "heads-up"
    } catch (Exception x) {
        GUIUtils.showError("Error rewriting aif:\n" + aifURI);
        x.printStackTrace();

    } finally {
        if (temp != null)
            temp.delete(); // clean up in case of errors
    }
    setDirty(false);
}

From source file:org.mitre.jawb.io.ATLASHelper.java

License:Open Source License

/**
 * Read in an .aif file from the specified URI, and write it out with
 * localized refernces to the output stream.
 * @param aifURI location of input .aif file. <strong>MUST BE ABSOLUTE.</strong>
 * @param out stream that localized version of input is written to
 * @param cheatMap A map of undocumented values that we use in Callisto
 *                 to store data in the AIF which ATLAS won't.
 *//*from   w  w  w  . java 2 s  .co m*/
public static void localize(URI aifURI, OutputStream out, Map cheatMap) throws IOException {
    if (DEBUG > 0)
        System.err.println("ATHelp.localize: aifURI=" + aifURI);

    Document doc = parse(aifURI);
    DocumentType doctype = doc.getDocType();
    Element corpus = doc.getRootElement();
    Element signal = getTextSignal(corpus);

    // Replace external ATLAS DTD reference w/ local reference
    URI localDTD = URLUtils.badURLToURI(Jawb.getResource("aif.dtd"));
    doctype.setSystemID(localDTD.toString());

    // Retrieve 'Cononical' MAIA Scheme and replace w/ local reference
    String maiaString = corpus.attributeValue("schemeLocation");
    Task task = findTask(maiaString, EXTERNAL);
    if (task == null) {
        // it could be an old file that still has a local MAIA URL
        String escapedMaia = maiaString.replaceAll(" ", "%20");
        if ((task = findTask(escapedMaia, LOCAL)) == null)
            throw new RuntimeException("Unrecognized Task: MAIA URI=" + maiaString);
    }
    corpus.addAttribute("schemeLocation", task.getLocalMaiaURI().toString());

    // It's possible to /not/ have a text signal referenced
    if (signal != null) {
        // If signal is relative URI, convert to absolute,
        // resolving against .aif file
        String signalHREF = signal.attributeValue("href");
        try {
            String path = aifURI.getRawPath();
            URI aifBase = aifURI.resolve(path.substring(0, path.lastIndexOf('/') + 1));
            URI signalURI = new URI(signalHREF);
            URI resolvedURI = aifBase.resolve(signalURI);

            if (DEBUG > 0) {
                System.err.println("ATHelp.localize:\n        base= " + aifBase + "\n      signal= " + signalURI
                        + "\n    resolved= " + resolvedURI);
            }
            signal.addAttribute("href", resolvedURI.toString());

        } catch (URISyntaxException x) {
            System.err.println("WARNING: aif file specifies invalid signal URI:"
                    + " not resolving:\n    aifURI=   " + aifURI + "\n    signalURI=" + signalHREF);
            System.err.println(x.getMessage());
        }
        // ATLAS ignores encoding so use the cheats
        cheatMap.put("encoding", signal.attributeValue("encoding"));
        cheatMap.put("mimeType", signal.attributeValue("mimeType"));
        cheatMap.put(SIGNAL_CHECKSUM, signal.attributeValue("checksum"));

        Element body = signal.element("body");
        if (body != null) {
            String signalEncoding = body.attributeValue("encoding");
            if (!"Base64".equalsIgnoreCase(signalEncoding))
                System.err.println("Unrecognized embeded signal encoding: '" + signalEncoding + "'");
            else {
                String embedded = body.getText();
                cheatMap.put(SIGNAL_DATA, Base64.decode(embedded));
            }
        }
    } // if (signal != null)

    dump(doc, out);
}

From source file:org.mitre.jawb.io.ATLASHelper.java

License:Open Source License

/**
 * Read in an .aif file from the specified URI, and write it out with
 * localized refernces to the output stream.
 * @param aifURI location of input .aif file. <strong>MUST BE ABSOLUTE.</strong>
 * @param out stream that localized version of input is written to
 * @param relativize rewrite the absolute signal URI as relative based on
 *                   input//from   w ww. j ava  2 s.c o m
 * @param cheatMap A map of undocumented values that we use in Callisto
 *                    to store data in the AIF which ATLAS won't.
 */
public static void externalize(URI aifURI, OutputStream out, boolean relativize, Map cheatMap)
        throws IOException {
    if (DEBUG > 0)
        System.err.println("ATHelp.externalize: aifURI=" + aifURI);

    Document doc = parse(aifURI);
    DocumentType doctype = doc.getDocType();
    Element corpus = doc.getRootElement();
    Element signal = getTextSignal(corpus);

    // Replace local ATLAS DTD reference w/ external reference
    doctype.setSystemID("http://www.nist.gov/speech/atlas/aif.dtd");

    // Replace local MAIA Scheme w/ external reference
    String maiaString = corpus.attributeValue("schemeLocation");
    Task task = findTask(maiaString, LOCAL);
    if (task == null)
        System.err.println("Unable to extern Maia: Unknown:\n  " + maiaString);
    else
        corpus.addAttribute("schemeLocation", task.getMaiaURI().toString());

    // It's possible to /not/ have a text signal referenced
    if (signal != null) {
        // Perhaps replace absolute URI with relative URI
        if (relativize) {
            String signalHREF = signal.attributeValue("href");
            try {
                String path = aifURI.getRawPath();
                URI aifBase = aifURI.resolve(path.substring(0, path.lastIndexOf('/') + 1));
                URI signalURI = new URI(signalHREF);
                URI relativeURI = aifBase.relativize(signalURI);

                if (DEBUG > 0) {
                    System.err.println("ATHelp.extern:\n      base= " + aifBase + "\n      signal= " + signalURI
                            + "\n    relative= " + relativeURI);
                }
                signal.addAttribute("href", relativeURI.toString());

            } catch (URISyntaxException x) {
                System.err.println("WARNING: aif file specifies invalid signal URI:"
                        + " not relativizing:\n    aifURI=   " + aifURI + "\n    signalURI=" + signalHREF);
                System.err.println(x.getMessage());
            }
        }

        // ATLAS ignores encoding so use the cheats
        signal.addAttribute("encoding", (String) cheatMap.get("encoding"));
        signal.addAttribute("mimeType", (String) cheatMap.get("mimeType"));

        if (cheatMap.get(SIGNAL_DATA) != null) {
            String embedded = Base64.encode((byte[]) cheatMap.get(SIGNAL_DATA));
            Element body = signal.addElement("body");
            body.addAttribute("encoding", "Base64");
            body.addText(embedded);
        }
    } // if (signal != null)

    dump(doc, out);
}

From source file:org.mitre.jawb.io.ATLASHelper.java

License:Open Source License

/**
   * Read in an .aif file from the specified URI, and write it out with
   * localized refernces to the output stream.
   * @param aifURI location of input .aif file. <strong>MUST BE ABSOLUTE.</strong>
   * @param out stream that localized version of input is written to
   * @param relativize rewrite the absolute signal URI as relative based on
   *                   input/*from  w w w . ja v a2  s .  c o  m*/
   * @param cheatMap A map of undocumented values that we use in Callisto
   *                    to store data in the AIF which ATLAS won't.
   */
public static void externalize(URI aifURI, InputStream in, OutputStream out, boolean relativize, Map cheatMap)
        throws IOException {
    //    if (DEBUG > 0)
    //      System.err.println ("ATHelp.externalize: aifURI="+aifURI);

    Document doc = parse(in);
    DocumentType doctype = doc.getDocType();
    Element corpus = doc.getRootElement();
    Element signal = getTextSignal(corpus);

    // Replace local ATLAS DTD reference w/ external reference
    doctype.setSystemID("http://www.nist.gov/speech/atlas/aif.dtd");

    // Replace local MAIA Scheme w/ external reference
    String maiaString = corpus.attributeValue("schemeLocation");
    Task task = findTask(maiaString, LOCAL);
    if (task == null)
        System.err.println("Unable to extern Maia: Unknown:\n  " + maiaString);
    else
        corpus.addAttribute("schemeLocation", task.getMaiaURI().toString());

    // It's possible to /not/ have a text signal referenced
    if (signal != null) {
        // Perhaps replace absolute URI with relative URI
        if (relativize) {
            String signalHREF = signal.attributeValue("href");
            try {
                String path = aifURI.getRawPath();
                URI aifBase = aifURI.resolve(path.substring(0, path.lastIndexOf('/') + 1));
                URI signalURI = new URI(signalHREF);
                URI relativeURI = aifBase.relativize(signalURI);

                if (DEBUG > 0) {
                    System.err.println("ATHelp.extern:\n      base= " + aifBase + "\n      signal= " + signalURI
                            + "\n    relative= " + relativeURI);
                }
                signal.addAttribute("href", relativeURI.toString());

            } catch (URISyntaxException x) {
                System.err.println("WARNING: aif file specifies invalid signal URI:"
                        + " not relativizing:\n    aifURI=   " + aifURI + "\n    signalURI=" + signalHREF);
                System.err.println(x.getMessage());
            }
        }

        // ATLAS ignores encoding so use the cheats
        signal.addAttribute("encoding", (String) cheatMap.get("encoding"));
        signal.addAttribute("mimeType", (String) cheatMap.get("mimeType"));

        if (cheatMap.get(SIGNAL_DATA) != null) {
            String embedded = Base64.encode((byte[]) cheatMap.get(SIGNAL_DATA));
            Element body = signal.addElement("body");
            body.addAttribute("encoding", "Base64");
            body.addText(embedded);
        }
    } // if (signal != null)

    dump(doc, out);
}

From source file:org.olat.ims.qti.editor.beecom.objects.QTIDocument.java

License:Apache License

/**
 * Get the structure as an XML Document.
 * //ww  w. j  a  v a2  s .c  o  m
 * @return XML Document.
 */
public Document getDocument() {
    final Document tmp = DocumentHelper.createDocument();
    final DocumentType type = new DOMDocumentType();
    type.setElementName(DOCUMENT_ROOT);
    type.setSystemID(DOCUMENT_DTD);

    tmp.setDocType(type);
    final Element questestinterop = tmp.addElement(DOCUMENT_ROOT);
    this.assessment.addToElement(questestinterop);
    return tmp;
}