Example usage for com.itextpdf.text.xml.xmp XmpSchema getXmlns

List of usage examples for com.itextpdf.text.xml.xmp XmpSchema getXmlns

Introduction

In this page you can find the example usage for com.itextpdf.text.xml.xmp XmpSchema getXmlns.

Prototype

public String getXmlns() 

Source Link

Usage

From source file:org.crossref.pdfmark.XmpUtils.java

License:Open Source License

/** 
 * Combines the RDF description blocks from left and right. Those from
 * right will overwrite those from left in the case that left and right
 * contain description blocks with the same namespace.
 *//* w w w.j  a v  a 2s.co m*/
public static byte[] mergeXmp(byte[] left, byte[] right) throws XmpException {
    if (left == null || left.length == 0) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        try {
            XmpWriter writer = new XmpWriter(bout);
            for (XmpSchema schema : parseSchemata(right)) {
                writer.addRdfDescription(schema);
            }
            writer.close();
        } catch (IOException e) {
            throw new XmpException(e);
        }
        return bout.toByteArray();
    }

    if (right == null || right.length == 0) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        try {
            XmpWriter writer = new XmpWriter(bout);
            for (XmpSchema schema : parseSchemata(left)) {
                writer.addRdfDescription(schema);
            }
            writer.close();
        } catch (IOException e) {
            throw new XmpException(e);
        }
        return bout.toByteArray();
    }

    XmpSchema[] leftSchemata = parseSchemata(left);
    XmpSchema[] rightSchemata = parseSchemata(right);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    String[] noWriteList = new String[rightSchemata.length];
    for (int i = 0; i < noWriteList.length; i++) {
        noWriteList[i] = rightSchemata[i].getXmlns();
    }

    try {
        XmpWriter writer = new XmpWriter(bout);
        for (XmpSchema schema : leftSchemata) {
            boolean found = false;
            for (String checkAgainst : noWriteList) {
                if (schema.getXmlns().equals(checkAgainst)) {
                    found = true;
                    break;
                }
            }

            if (!found) {
                writer.addRdfDescription(schema);
            }
        }
        for (XmpSchema schema : rightSchemata) {
            writer.addRdfDescription(schema);
        }
        writer.close();
        return bout.toByteArray();
    } catch (IOException e) {
        throw new XmpException(e);
    }
}

From source file:org.crossref.pdfmark.XmpUtils.java

License:Open Source License

/**
 * Copy key value pairs from PDFX namespace into a PDF's document information
 * dictionary.//from  w  ww .  j  ava 2 s.  c  om
 */
public static Map<String, String> toInfo(byte[] xmp) throws XmpException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        Map<String, String> info = new HashMap<String, String>();

        XmpSchema[] schemata = XmpUtils.parseSchemata(xmp);
        for (XmpSchema schema : schemata) {
            if (schema.getXmlns().contains("pdfx")) {
                for (Entry<Object, Object> entry : schema.entrySet()) {
                    Object value = entry.getValue();

                    String key = (String) entry.getKey();
                    String[] parts = key.split(":");
                    String infoKey = parts.length == 2 ? parts[1] : parts[0];

                    String val = (String) entry.getValue();

                    if (val.toLowerCase().contains("<rdf:seq>") || val.toLowerCase().contains("<rdf:bag>")) {
                        val = "<xml xmlns:rdf=\"rdf\">" + val + "</xml>";
                        DocumentBuilder builder = factory.newDocumentBuilder();
                        Document doc = builder.parse(new ByteArrayInputStream(val.getBytes()));

                        NodeList nodes = doc.getElementsByTagName("rdf:li");
                        for (int i = 0; i < nodes.getLength(); i++) {
                            Element item = (Element) nodes.item(i);
                            info.put(infoKey + "[" + (i + 1) + "]", item.getTextContent());
                        }
                    } else {
                        info.put(infoKey, (String) value);
                    }
                }
            }
        }

        return info;
    } catch (DOMException e) {
        throw new XmpException(e);
    } catch (IOException e) {
        throw new XmpException(e);
    } catch (SAXException e) {
        throw new XmpException(e);
    } catch (ParserConfigurationException e) {
        throw new XmpException(e);
    }
}