Example usage for org.jdom2 JDOMFactory attribute

List of usage examples for org.jdom2 JDOMFactory attribute

Introduction

In this page you can find the example usage for org.jdom2 JDOMFactory attribute.

Prototype

public Attribute attribute(String name, String value);

Source Link

Document

This will create a new Attribute with the specified (local) name and value, and does not place the attribute in a org.jdom2.Namespace .

Usage

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

License:Apache License

/**
 * Parse the input file and return a well formed document
 *///from  ww w .  j a  v  a2  s . 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.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 ww .j  a v a 2  s. co 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

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();
        }// w ww  . j a v a2 s .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);
        }
    }
}