Example usage for org.jdom2 Namespace getNamespace

List of usage examples for org.jdom2 Namespace getNamespace

Introduction

In this page you can find the example usage for org.jdom2 Namespace getNamespace.

Prototype

public static Namespace getNamespace(final String uri) 

Source Link

Document

This will retrieve (if in existence) or create (if not) a Namespace for the supplied URI, and make it usable as a default namespace, as no prefix is supplied.

Usage

From source file:com.sun.syndication.io.impl.RSS091UserlandParser.java

License:Open Source License

protected Namespace getRSSNamespace() {
    return Namespace.getNamespace("");
}

From source file:com.sun.syndication.io.impl.RSS10Parser.java

License:Open Source License

/**
 * Returns the namespace used by RSS elements in document of the RSS 1.0
 * <P>/*from w w  w  . j a v a2  s  . c  om*/
 *
 * @return returns "http://purl.org/rss/1.0/".
 */
protected Namespace getRSSNamespace() {
    return Namespace.getNamespace(RSS_URI);
}

From source file:com.sun.syndication.io.impl.RSS20wNSParser.java

License:Open Source License

protected Namespace getRSSNamespace() {
    return Namespace.getNamespace(RSS20_URI);
}

From source file:com.xebialabs.overcast.support.libvirt.Metadata.java

License:Apache License

/**
 * Extract {@link Metadata} from the domain XML. Throws {@link IllegalArgumentException} if the metadata is
 * malformed.//w w w  . j a va 2s.c o  m
 *
 * @return the metadata or <code>null</code> if there's no metadata
 */
public static Metadata fromXml(Document domainXml) {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(XML_DATE_FORMAT);
        Element metadata = getMetadataElement(domainXml);
        if (metadata == null) {
            return null;
        }
        Namespace ns = Namespace.getNamespace(METADATA_NS_V1);
        Element ocMetadata = metadata.getChild(OVERCAST_METADATA, ns);
        if (ocMetadata == null) {
            return null;
        }
        String parentDomain = getElementText(ocMetadata, PARENT_DOMAIN, ns);
        String creationTime = getElementText(ocMetadata, CREATION_TIME, ns);
        Date date = sdf.parse(creationTime);

        if (ocMetadata.getChild(PROVISIONED_WITH, ns) != null) {
            String provisionedWith = getElementText(ocMetadata, PROVISIONED_WITH, ns);
            String checkSum = getElementText(ocMetadata, PROVISIONED_CHECKSUM, ns);

            return new Metadata(parentDomain, provisionedWith, checkSum, date);
        }
        return new Metadata(parentDomain, date);
    } catch (ParseException e) {
        throw new IllegalArgumentException("Invalid date in metadata on domain", e);
    }
}

From source file:converter.ConverterBean.java

private void init() throws MalformedURLException, JDOMException, IOException {
    if (isInit) {
        return;//from  www  .  j a va2  s.c  om
    }
    this.isInit = true;

    SAXBuilder sxb = new SAXBuilder();

    {
        URL url = new URL("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
        Document document = sxb.build(url);
        racine = document.getRootElement();
        Namespace ns = Namespace.getNamespace("http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
        racine = racine.getChild("Cube", ns);
        racine = racine.getChild("Cube", ns);
    }

    {
        URL url = new URL("http://www.currency-iso.org/dam/downloads/lists/list_one.xml");
        Document document = sxb.build(url);
        racineMore = document.getRootElement();
        racineMore = racineMore.getChild("CcyTbl");
    }
}

From source file:de.herm_detlef.java.application.io.Export.java

License:Apache License

private static Document createJdomDocument(ObservableList<ExerciseItem> exerciseItemList) {

    Namespace ns = Namespace.getNamespace(ApplicationConstants.XML_NAMESPACE);
    Element catalog = new Element(TAG.CATALOG.name(), ns);
    Document doc = new Document(catalog);
    Namespace xsi = Namespace.getNamespace("xsi", ApplicationConstants.XML_SCHEMA_INSTANCE);
    doc.getRootElement().addNamespaceDeclaration(xsi);
    doc.getRootElement().setAttribute("schemaLocation", ApplicationConstants.XML_SCHEMA_DEFINITION, xsi);

    int count = 0;
    for (ExerciseItem exItem : exerciseItemList) {

        Element item = new Element(TAG.ITEM.name(), ns);
        catalog.addContent(item);//from  ww w.ja v a2s . c  o m

        Element id = new Element(TAG.ID.name(), ns);
        id.addContent(String.valueOf(++count));
        item.addContent(id);

        Element element = null;

        for (ItemPart itemPart : exItem.getExerciseItemParts()) {
            Object obj = itemPart.get();

            if (obj == null) {
                // assert false : String.format(
                // "%s",
                // itemPart.getClass() ); // TODO
                continue;
            }

            if (itemPart instanceof QuestionText || itemPart instanceof QuestionText2) {
                isAnswerPart = false;

                if (!isQuestionPart) {
                    element = new Element(TAG.QUESTION.name(), ns);
                    item.addContent(element);
                    isQuestionPart = true;
                }

                if (element == null)
                    continue;

                if (itemPart instanceof QuestionText) {
                    Element text = new Element(TAG.TEXT.name(), ns);
                    element.addContent(text);
                    text.addContent(((QuestionText) itemPart).getStr().get());
                }

                if (itemPart instanceof QuestionText2) {
                    Element text = new Element(TAG.TEXT2.name(), ns);
                    element.addContent(text);
                    text.addContent(((QuestionText2) itemPart).getStr().get());
                }
            }

            else if (itemPart instanceof QuestionCode) {
                isAnswerPart = false;

                if (!isQuestionPart) {
                    element = new Element(TAG.QUESTION.name(), ns);
                    item.addContent(element);
                    isQuestionPart = true;
                }

                if (element == null)
                    continue;

                Element code = new Element(TAG.CODE.name(), ns);
                element.addContent(code);

                code.addContent(((QuestionCode) itemPart).getStr().get());
            }

            else if (itemPart instanceof SingleChoiceAnswerText) {
                isQuestionPart = false;

                if (!isAnswerPart) {
                    element = new Element(TAG.SINGLE_CHOICE_ANSWER.name(), ns);
                    item.addContent(element);
                    isAnswerPart = true;
                }

                if (element == null)
                    continue;

                Element answer = new Element(TAG.TEXT.name(), ns);
                element.addContent(answer);

                if (((AnswerText) itemPart).isMarked()) {
                    Attribute mark = new Attribute("mark", "true");
                    answer.setAttribute(mark);
                }

                answer.addContent(((SingleChoiceAnswerText) itemPart).getStr().get());
            }

            else if (itemPart instanceof MultipleChoiceAnswerText) {
                isQuestionPart = false;

                if (!isAnswerPart) {
                    element = new Element(TAG.MULTIPLE_CHOICE_ANSWER.name(), ns);
                    item.addContent(element);
                    isAnswerPart = true;
                }

                if (element == null)
                    continue;

                Element answer = new Element(TAG.TEXT.name(), ns);
                element.addContent(answer);

                if (((AnswerText) itemPart).isMarked()) {
                    Attribute mark = new Attribute("mark", "true");
                    answer.setAttribute(mark);
                }

                answer.addContent(((MultipleChoiceAnswerText) itemPart).getStr().get());
            }

            else if (itemPart instanceof SolutionText) {
                isQuestionPart = false;
                isAnswerPart = false;

                element = new Element(TAG.SOLUTION.name(), ns);
                item.addContent(element);

                Element solution = new Element(TAG.TEXT.name(), ns);
                element.addContent(solution);

                solution.addContent(((SolutionText) itemPart).getStr().get());
            }

            if (element == null) {
                assert false;
                return null;
            }
        }
    }

    return doc;
}

From source file:de.huberlin.german.korpling.laudatioteitool.MergeTEI.java

License:Apache License

public void merge() throws LaudatioException {
    try {/*from w  w w  .  ja v a2 s .  c o m*/
        if (outputFile.getParentFile() != null && !outputFile.getParentFile().exists()
                && !outputFile.getParentFile().mkdirs()) {
            throw new LaudatioException(
                    messages.getString("COULD NOT CREATE MERGED OUTPUTFILE: I CAN'T CREATE THE DIRECTORIES"));
        }

        Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0");
        Element root = new Element("teiCorpus", teiNS);
        Element documentRoot = new Element("teiCorpus", teiNS);
        Element preparationRoot = new Element("teiCorpus", teiNS);

        mergeMainCorpusHeader(root);
        mergeDocumentHeader(documentRoot);
        mergePreparationHeader(preparationRoot);

        root.addContent(documentRoot);
        documentRoot.addContent(documentRoot.getChildren().size(), preparationRoot);

        Document mergedDoc = new Document(root);

        // output the new XML
        XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
        xmlOut.output(mergedDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
        log.info(messages.getString("WRITTEN MERGED TEI"), outputFile.getPath());
    } catch (SAXException ex) {
        throw new LaudatioException(ex.getLocalizedMessage());
    } catch (JDOMException ex) {
        throw new LaudatioException(ex.getLocalizedMessage());
    } catch (IOException ex) {
        throw new LaudatioException(ex.getLocalizedMessage());
    }
}

From source file:de.huberlin.german.korpling.laudatioteitool.SplitTEI.java

License:Apache License

private TEIValidator.Errors extractMainCorpusHeader(Document doc)
        throws LaudatioException, IOException, SAXException {
    TEIValidator validator = corpusSchemeURL == null ? new TEICorpusValidator()
            : new FromURLValidator(corpusSchemeURL);

    Element corpusHeader = doc.getRootElement().getChild("teiHeader", null);

    if (corpusHeader != null) {
        File corpusDir = new File(outputDirectory, "CorpusHeader");
        if (!corpusDir.exists() && !corpusDir.mkdir()) {
            throw new LaudatioException(
                    messages.getString("COULD NOT CREATE DIRECTORY") + corpusDir.getAbsolutePath());
        }//w ww  .j a va2  s  .  c  o  m

        // create the subtree for the global corpus header
        Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0");
        Element newRootForCorpus = new Element("TEI", teiNS);
        newRootForCorpus.addContent(corpusHeader.clone());
        Document corpusDoc = new Document(newRootForCorpus);

        if (corpusSchemeURL == null) {
            corpusDoc.addContent(0, new ProcessingInstruction("xml-model",
                    "href=\"" + TEICorpusValidator.DEFAULT_SCHEME_URL + "\""));
        } else {
            corpusDoc.addContent(0, new ProcessingInstruction("xml-model", "href=\"" + corpusSchemeURL + "\""));
        }

        // we need to append an empty "text" element after the header
        Element text = new Element("text", teiNS);
        text.setText("");
        newRootForCorpus.addContent(text);

        // we work with the copy from now
        corpusHeader = newRootForCorpus.getChild("teiHeader", null);
        Preconditions.checkNotNull(corpusHeader, messages.getString("ERROR NO CORPUS TITLE GIVEN"));

        Preconditions.checkState("CorpusHeader".equals(corpusHeader.getAttributeValue("type")));

        Preconditions.checkNotNull(corpusHeader.getChild("fileDesc", null),
                messages.getString("ERROR NO CORPUS TITLE GIVEN"));
        Preconditions.checkNotNull(corpusHeader.getChild("fileDesc", null).getChild("titleStmt", null),
                messages.getString("ERROR NO CORPUS TITLE GIVEN"));

        String title = corpusHeader.getChild("fileDesc", null).getChild("titleStmt", null)
                .getChildTextNormalize("title", null);
        Preconditions.checkNotNull(title, messages.getString("ERROR NO CORPUS TITLE GIVEN"));

        // save the file with the title as file name
        File outputFile = new File(corpusDir, title + ".xml");
        XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
        xmlOut.output(corpusDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
        log.info(messages.getString("WRITTEN CORPUS HEADER"), outputFile.getPath());

        validator.validate(outputFile);

    }
    return validator.getErrors();
}

From source file:de.huberlin.german.korpling.laudatioteitool.SplitTEI.java

License:Apache License

private TEIValidator.Errors extractDocumentHeaders(Document doc)
        throws LaudatioException, IOException, SAXException {
    TEIValidator validator = documentSchemeURL == null ? new TEIDocumentValidator()
            : new FromURLValidator(documentSchemeURL);

    File documentDir = new File(outputDirectory, "DocumentHeader");
    if (!documentDir.exists() && !documentDir.mkdir()) {
        throw new LaudatioException(
                messages.getString("COULD NOT CREATE DIRECTORY") + documentDir.getAbsolutePath());
    }/*from  w w  w . j  a  va 2 s .  co  m*/

    Element documentRoot = Preconditions.checkNotNull(doc.getRootElement().getChild("teiCorpus", null));

    for (Element docHeader : documentRoot.getChildren("teiHeader", null)) {
        Preconditions.checkState("DocumentHeader".equals(docHeader.getAttributeValue("type")));

        // create the subtree for the global corpus header
        Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0");
        Element tei = new Element("TEI", teiNS);
        tei.addContent(docHeader.clone());
        Document newDoc = new Document(tei);

        if (documentSchemeURL == null) {
            newDoc.addContent(0, new ProcessingInstruction("xml-model",
                    "href=\"" + TEIDocumentValidator.DEFAULT_SCHEME_URL + "\""));
        } else {
            newDoc.addContent(0, new ProcessingInstruction("xml-model", "href=\"" + documentSchemeURL + "\""));
        }

        // we need to append an empty "text" element after the header
        Element text = new Element("text", teiNS);
        text.setText("");
        tei.addContent(text);

        Element fileDesc = Preconditions
                .checkNotNull(tei.getChild("teiHeader", null).getChild("fileDesc", null));

        String outName = UUID.randomUUID().toString();

        String id = fileDesc.getAttributeValue("id", Namespace.XML_NAMESPACE);
        if (id != null) {
            outName = id;
        } else {
            Element titleStmt = Preconditions.checkNotNull(fileDesc.getChild("titleStmt", null));

            String title = titleStmt.getChildText("title", null);
            if (title != null) {
                outName = title;
            }
        }

        File outputFile = new File(documentDir, outName + ".xml");
        XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
        xmlOut.output(newDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
        log.info(messages.getString("WRITTEN DOCUMENT HEADER"), outputFile.getPath());

        validator.validate(outputFile);

    }
    return validator.getErrors();
}

From source file:de.huberlin.german.korpling.laudatioteitool.SplitTEI.java

License:Apache License

private TEIValidator.Errors extractPreparationSteps(Document doc)
        throws LaudatioException, IOException, SAXException {
    TEIValidator validator = preparationSchemeURL == null ? new TEIPreparationValidator()
            : new FromURLValidator(preparationSchemeURL);
    Multiset<String> knownPreparationTitles = HashMultiset.create();

    File documentDir = new File(outputDirectory, "PreparationHeader");
    if (!documentDir.exists() && !documentDir.mkdir()) {
        throw new LaudatioException(
                messages.getString("COULD NOT CREATE DIRECTORY") + documentDir.getAbsolutePath());
    }/*from   ww w.j a v  a  2  s . c o m*/

    Preconditions.checkNotNull(doc.getRootElement().getChild("teiCorpus", null));
    Element preparationRoot = Preconditions
            .checkNotNull(doc.getRootElement().getChild("teiCorpus", null).getChild("teiCorpus", null));

    for (Element preparationHeader : preparationRoot.getChildren("teiHeader", null)) {
        Preconditions.checkState("PreparationHeader".equals(preparationHeader.getAttributeValue("type")));

        // create the subtree for the global corpus header
        Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0");
        Element tei = new Element("TEI", teiNS);
        tei.addContent(preparationHeader.clone());
        Document newDoc = new Document(tei);

        if (preparationSchemeURL == null) {
            newDoc.addContent(0, new ProcessingInstruction("xml-model",
                    "href=\"" + TEIPreparationValidator.DEFAULT_SCHEME_URL + "\""));
        } else {
            newDoc.addContent(0,
                    new ProcessingInstruction("xml-model", "href=\"" + preparationSchemeURL + "\""));
        }

        // we need to append an empty "text" element after the header
        Element text = new Element("text", teiNS);
        text.setText("");
        tei.addContent(text);

        Element fileDesc = Preconditions
                .checkNotNull(tei.getChild("teiHeader", null).getChild("fileDesc", null));

        String outName = UUID.randomUUID().toString();

        Element titleStmt = Preconditions.checkNotNull(fileDesc.getChild("titleStmt", null));
        Element title = Preconditions.checkNotNull(titleStmt.getChild("title", null));
        String corresp = title.getAttributeValue("corresp");
        if (corresp != null) {
            if (knownPreparationTitles.contains(corresp)) {
                knownPreparationTitles.add(corresp);
                outName = corresp + "_" + knownPreparationTitles.count(corresp);
                log.warn(messages.getString("MORE THAN ONE PREPARATION HEADER"), corresp);
            } else {
                outName = corresp;
                knownPreparationTitles.add(corresp);
            }
        }

        File outputFile = new File(documentDir, outName + ".xml");
        XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
        xmlOut.output(newDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
        log.info(messages.getString("WRITTEN PREPARATION HEADER"), outputFile.getPath());

        validator.validate(outputFile);

    }
    return validator.getErrors();
}