Example usage for org.jdom2 Element getAttributes

List of usage examples for org.jdom2 Element getAttributes

Introduction

In this page you can find the example usage for org.jdom2 Element getAttributes.

Prototype

public List<Attribute> getAttributes() 

Source Link

Document

This returns the complete set of attributes for this element, as a List of Attribute objects in no particular order, or an empty list if there are none.

Usage

From source file:org.jumpmind.metl.ui.views.design.EditXmlParserPanel.java

License:Open Source License

protected void buildXpathChoicesForElement(String prefix, Element element) {
    String base = "/" + element.getName();
    String newPrefix = prefix + base;
    xpathChoices.add(newPrefix);/*from w ww  .  j  a v  a  2  s . c o  m*/
    for (Attribute attr : element.getAttributes()) {
        xpathChoices.add(newPrefix + "/@" + attr.getName());
    }
    xpathChoices.add(base);
    for (Attribute attr : element.getAttributes()) {
        xpathChoices.add(base + "/@" + attr.getName());
    }
    buildXpathChoicesForChildElements(newPrefix, element);
}

From source file:org.kdp.word.Parser.java

License:Apache License

/**
 * Parse the input file and return a well formed document
 *//*from  w  ww  .j  ava2s .c o m*/
private Document parseHTML(final JDOMFactory factory, final File infile) throws SAXException, IOException {

    final AtomicReference<Document> docref = new AtomicReference<>();

    String charset = getProperty(PROPERTY_INPUT_CHARSET);
    charset = charset != null ? charset : "UTF-8";

    InputStream inputStream = new FileInputStream(infile);
    Reader reader = new InputStreamReader(inputStream, charset);
    InputSource source = new InputSource(reader);
    source.setEncoding(charset);

    SAXParserImpl.newInstance(null).parse(source, new DefaultHandler() {

        Stack<Element> stack = new Stack<>();

        public void startElement(String uri, String localName, String name, Attributes inatts) {
            Element element = factory.element(localName.toLowerCase());
            List<Attribute> outatts = element.getAttributes();
            for (int i = 0; i < inatts.getLength(); i++) {
                String att = inatts.getLocalName(i);
                String val = inatts.getValue(i);
                outatts.add(factory.attribute(att, val));
            }
            if (docref.get() == null) {
                docref.set(factory.document(element));
            } else {
                Element parent = stack.peek();
                parent.getChildren().add(element);
            }
            stack.push(element);
        }

        public void endElement(String uri, String localName, String name) {
            stack.pop();
        }

        public void characters(char[] arr, int start, int length) {
            StringBuilder sb = new StringBuilder(length);
            for (int i = 0; i < length; i++) {
                char ch = arr[start + i];
                sb.append(ch);
            }
            Element parent = stack.peek();
            parent.addContent(sb.toString());
        }
    });
    return docref.get();
}

From source file:org.kdp.word.transformer.AttributeTransformer.java

License:Apache License

private void transformInternal(Parser parser, Element el, Set<Replace> replace) {
    String elname = el.getName();
    for (Attribute att : new ArrayList<Attribute>(el.getAttributes())) {
        String attname = att.getName();
        String attvalue = att.getValue();
        String attid = elname + "." + attname;
        for (Replace rep : replace) {
            if (attid.equals(rep.attid)) {
                if (rep.substr == null || attvalue.contains(rep.substr)) {
                    if (rep.newval == null || rep.newval.length() == 0) {
                        log.debug("Remote attribute: {}", att);
                        el.getAttributes().remove(att);
                    } else {
                        log.debug("Replace attribute: {}", att);
                        att.setValue(rep.newval);
                    }/*from w  w  w.  j a va  2 s .co m*/
                    break;
                }
            }
        }
    }
    for (Element ch : el.getChildren()) {
        transformInternal(parser, ch, replace);
    }
}

From source file:org.kdp.word.transformer.ListParagraphTransformer.java

License:Apache License

private void processListItemBatch(Context context, Element parent, List<Element> listItems) {
    boolean ordered = false;
    for (Element el : listItems) {
        removeNestedSpanElements(el);/*from   w ww  . jav a 2s  .  c om*/
        normalizeListItemText(el);
        ordered = processItemMarker(el);
        el.getAttributes().clear();
    }
    Element firstItem = listItems.get(0);
    int index = parent.indexOf(firstItem);
    for (Element el : listItems) {
        parent.removeContent(el);
    }
    JDOMFactory factory = context.getJDOMFactory();
    Element ul = factory.element(ordered ? "ol" : "ul");
    for (Element el : listItems) {
        Element li = factory.element("li");
        li.setAttribute("class", "MsoListParagraph");
        for (Content co : el.getContent()) {
            li.addContent(co.clone());
        }
        ul.addContent(li);
    }
    parent.addContent(index, ul);
}

From source file:org.kdp.word.transformer.OPFTransformer.java

License:Apache License

private void processMetadata(Context context, Element opf) {

    Element metadata = JDOMUtils.findElement(opf, "metadata");
    JDOMFactory factory = context.getJDOMFactory();
    Parser parser = context.getParser();

    // Title// w  w  w  .  j av  a2  s  .c o  m
    String title = parser.getProperty(Parser.PROPERTY_OPF_METADATA_TITLE);
    IllegalStateAssertion.assertNotNull(title, "Cannot obtain property: " + Parser.PROPERTY_OPF_METADATA_TITLE);
    Element elTitle = factory.element("title", NS_DC);
    elTitle.setText(title != null ? title : "Undefined Title");
    metadata.getChildren().add(elTitle);

    // Creator
    String author = parser.getProperty(Parser.PROPERTY_OPF_METADATA_AUTHOR);
    IllegalStateAssertion.assertNotNull(author,
            "Cannot obtain property: " + Parser.PROPERTY_OPF_METADATA_AUTHOR);
    Element elCreator = factory.element("creator", NS_DC);
    elCreator.getAttributes().add(factory.attribute("role", "aut", NS_OPF));
    elCreator.setText(author != null ? author : "Undefined Author");
    metadata.getChildren().add(elCreator);

    // Language
    String language = parser.getProperty(Parser.PROPERTY_OPF_METADATA_LANGUAGE);
    if (language != null && language.length() > 0) {
        Element elLanguage = factory.element("language", NS_DC);
        elLanguage.setText(language);
        metadata.getChildren().add(elLanguage);
    }
}

From source file:org.kdp.word.transformer.OPFTransformer.java

License:Apache License

private void processManifest(Context context, Element opf) {
    Element manifest = JDOMUtils.findElement(opf, "manifest");
    JDOMFactory factory = context.getJDOMFactory();
    Parser parser = context.getParser();

    // Cover Image
    String imgsrc = parser.getProperty(Parser.PROPERTY_OPF_MANIFEST_COVER_IMAGE);
    IllegalStateAssertion.assertNotNull(imgsrc,
            "Cannot obtain property: " + Parser.PROPERTY_OPF_MANIFEST_COVER_IMAGE);
    String imgtype = parser.getProperty(Parser.PROPERTY_OPF_MANIFEST_COVER_IMAGE_TYPE);
    IllegalStateAssertion.assertNotNull(imgsrc,
            "Cannot obtain property: " + Parser.PROPERTY_OPF_MANIFEST_COVER_IMAGE_TYPE);
    Element item = factory.element("item");
    item.getAttributes().add(factory.attribute("id", "CoverImage"));
    item.getAttributes().add(factory.attribute("href", imgsrc));
    item.getAttributes().add(factory.attribute("properties", "cover-image"));
    item.getAttributes().add(factory.attribute("media-type", imgtype));
    manifest.getChildren().add(item);/*  w w w.  j  a v a2 s  . c  o m*/

    // Write sections
    Sections sections = context.getAttribute(Sections.class);
    if (sections != null) {
        for (Section section : sections) {
            item = factory.element("item");
            item.setAttribute("id", section.name);
            Path targetPath = IOUtils.bookRelative(context, section.target);
            item.setAttribute("href", targetPath.toString());
            if (section.isnav) {
                item.setAttribute("properties", "nav");
            }
            item.setAttribute("media-type", "application/xhtml+xml");
            manifest.getChildren().add(item);
        }
    }

    // Write Content
    item = factory.element("item");
    item.setAttribute("id", "Content");
    Path targetPath = IOUtils.bookRelative(context, context.getTarget());
    item.setAttribute("href", targetPath.toString());
    item.setAttribute("media-type", "application/xhtml+xml");
    manifest.getChildren().add(item);
}

From source file:org.kdp.word.transformer.TOCTransformer.java

License:Apache License

@Override
public void transform(Context context) {
    JDOMFactory factory = context.getJDOMFactory();

    Element root = context.getSourceRoot();
    for (Element el : root.getChildren()) {
        transformInternal(context, el);//from   w w w  .j  a v  a 2 s  . co  m
    }

    Element first = JDOMUtils.findElement(root, "p", "class", "MsoToc1");
    if (first != null) {
        Element parent = first.getParentElement();
        List<Element> children = parent.getChildren();

        // Add the nav element
        Element nav = factory.element("nav");
        nav.getAttributes().add(factory.attribute("type", "toc", OPFTransformer.NS_OPF));
        int index = children.indexOf(first);
        children.add(index, nav);

        // Add the ol element
        Element ol = factory.element("ol");
        ol.setAttribute("class", "Toc");
        nav.getChildren().add(ol);

        Iterator<Element> itel = children.iterator();
        while (itel.hasNext()) {
            Element el = itel.next();
            if (JDOMUtils.isElement(el, "p", "class", "MsoToc1")) {
                Element li = factory.element("li");
                li.getAttributes().add(factory.attribute("class", "MsoToc1"));
                li.addContent(el.cloneContent());
                ol.getChildren().add(li);
                itel.remove();
            }
        }
    }
}

From source file:org.kdp.word.transformer.TOCTransformer.java

License:Apache License

private void transformInternal(Context context, Element el) {
    JDOMFactory factory = context.getJDOMFactory();
    Element root = context.getSourceRoot();
    if (JDOMUtils.isElement(el, "p", "class", "MsoToc1")) {
        Element tocel = getFirstTextElement(el);
        String tocname = tocel.getText();
        int dotidx = tocname.indexOf("...");
        if (dotidx > 0) {
            tocname = tocname.substring(0, dotidx).trim();
        }/*  ww  w.  jav  a 2s .  c o m*/
        String aname = findAnchorName(root, tocname);
        IllegalStateAssertion.assertNotNull(aname, "Cannot find anchor for: " + tocname);
        Element anchor = factory.element("a");
        Path targetPath = IOUtils.bookRelative(context, context.getTarget());
        anchor.getAttributes().add(factory.attribute("href", targetPath + "#" + aname));
        anchor.setText(tocname);
        el.getChildren().clear();
        el.setText(null);
        el.getChildren().add(anchor);
    } else {
        for (Element ch : el.getChildren()) {
            transformInternal(context, ch);
        }
    }
}

From source file:org.knoxcraft.database.xml.XmlDatabase.java

License:Open Source License

@Override
public void updateSchema(DataAccess data) throws DatabaseWriteException {
    File file = new File("db/" + data.getName() + ".xml");

    if (!file.exists()) {
        try {//from w w  w.  j a v a2 s.c o m
            if (!file.createNewFile()) {
                throw new DatabaseWriteException("Failed to create database XML file: " + data.getName());
            }
        } catch (IOException e) {
            throw new DatabaseWriteException(e.getMessage(), e);
        }
    }
    try {
        Document table = verifyTable(file, data.getName());

        if (table.getRootElement().getChild("tableProperties") == null) {
            table.getRootElement().addContent(generateProperties(data));
        }

        HashSet<Column> tableLayout = data.getTableLayout();

        for (Column column : tableLayout) {
            if (table.getRootElement().getChild("tableProperties").getChild(column.columnName()) == null) {
                setPropertyFor(column, table.getRootElement().getChild("tableProperties"));
            }
        }
        for (Element element : table.getRootElement().getChildren()) {
            if (!element.getName().equals("tableProperties")) {
                addFields(element, tableLayout);
                removeFields(element, tableLayout);

                // Clean out the old attribute data
                for (Element child : element.getChildren()) {
                    child.getAttributes().clear();
                }
            }
        }
        write(file, table);
    } catch (JDOMException e) {
        throw new DatabaseWriteException(e.getMessage(), e);
    } catch (IOException e) {
        throw new DatabaseWriteException(e.getMessage(), e);
    } catch (DatabaseTableInconsistencyException e) {
        throw new DatabaseWriteException(e.getMessage(), e);
    }
}

From source file:org.lendingclub.mercator.ucs.UCSScanner.java

License:Apache License

ObjectNode toJson(Element element) {
    ObjectNode n = mapper.createObjectNode();

    element.getAttributes().forEach(it -> {
        n.put(it.getName(), it.getValue());
    });//from  w  ww  . j a  v  a  2  s. c  o  m
    n.put("ucsManagerId", getUCSClient().getUCSManagerId());
    return n;
}