XmpReader.java

1
/*
2
 * Copyright 2008 by Bruno Lowagie
3
 *
4
 * The contents of this file are subject to the Mozilla Public License Version 1.1
5
 * (the "License"); you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7
 *
8
 * Software distributed under the License is distributed on an "AS IS" basis,
9
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10
 * for the specific language governing rights and limitations under the License.
11
 *
12
 * The Original Code is 'iText, a free JAVA-PDF library'.
13
 *
14
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16
 * All Rights Reserved.
17
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18
 * are Copyright (C) 2008 by Paulo Soares. All Rights Reserved.
19
 *
20
 * Contributor(s): all the names of the contributors are added in the source code
21
 * where applicable.
22
 *
23
 * Alternatively, the contents of this file may be used under the terms of the
24
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25
 * provisions of LGPL are applicable instead of those above.  If you wish to
26
 * allow use of your version of this file only under the terms of the LGPL
27
 * License and not to allow others to use your version of this file under
28
 * the MPL, indicate your decision by deleting the provisions above and
29
 * replace them with the notice and other provisions required by the LGPL.
30
 * If you do not delete the provisions above, a recipient may use your version
31
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32
 *
33
 * This library is free software; you can redistribute it and/or modify it
34
 * under the terms of the MPL as stated above or under the terms of the GNU
35
 * Library General Public License as published by the Free Software Foundation;
36
 * either version 2 of the License, or any later version.
37
 *
38
 * This library is distributed in the hope that it will be useful, but WITHOUT
39
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41
 * details.
42
 *
43
 * If you didn't download this code from the following link, you should check if
44
 * you aren't using an obsolete version:
45
 * http://www.lowagie.com/iText/
46
 */
47
package com.lowagie.text.xml.xmp;
48
49
import java.io.ByteArrayInputStream;
50
import java.io.ByteArrayOutputStream;
51
import java.io.IOException;
52
import java.io.StringReader;
53
import java.nio.charset.StandardCharsets;
54
55
import javax.xml.parsers.DocumentBuilder;
56
import javax.xml.parsers.DocumentBuilderFactory;
57
import javax.xml.parsers.ParserConfigurationException;
58
59
import com.lowagie.text.ExceptionConverter;
60
import org.w3c.dom.Document;
61
import org.w3c.dom.NamedNodeMap;
62
import org.w3c.dom.Node;
63
import org.w3c.dom.NodeList;
64
import org.xml.sax.InputSource;
65
import org.xml.sax.SAXException;
66
67
68
import com.lowagie.text.xml.XmlDomWriter;
69
70
/**
71
 * Reads an XMP stream into an org.w3c.dom.Document objects.
72
 * Allows you to replace the contents of a specific tag.
73
 * @since 2.1.3
74
 */
75
76
public class XmpReader {
77
78
    private Document domDocument;
79
    
80
    /**
81
     * Constructs an XMP reader
82
     * @param    bytes    the XMP content
83
     * @throws ExceptionConverter 
84
     * @throws IOException 
85
     * @throws SAXException 
86
     */
87
    public XmpReader(byte[] bytes) throws SAXException, IOException {
88
        try {
89
            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
90 1 1. : removed call to javax/xml/parsers/DocumentBuilderFactory::setNamespaceAware → NO_COVERAGE
            fact.setNamespaceAware(true);
91
            DocumentBuilder db = fact.newDocumentBuilder();
92 1 1. : removed call to javax/xml/parsers/DocumentBuilder::setEntityResolver → NO_COVERAGE
            db.setEntityResolver((publicId, systemId) -> new InputSource(new StringReader("")));
93
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
94
            domDocument = db.parse(bais);
95
        } catch (ParserConfigurationException e) {
96
            throw new ExceptionConverter(e);
97
        }
98
    }
99
    
100
    /**
101
     * Replaces the content of a tag.
102
     * @param    namespaceURI    the URI of the namespace
103
     * @param    localName        the tag name
104
     * @param    value            the new content for the tag
105
     * @return    true if the content was successfully replaced
106
     * @since    2.1.6 the return type has changed from void to boolean
107
     */
108
    public boolean replace(String namespaceURI, String localName, String value) {
109
        NodeList nodes = domDocument.getElementsByTagNameNS(namespaceURI, localName);
110
        Node node;
111 1 1. replace : negated conditional → NO_COVERAGE
        if (nodes.getLength() == 0)
112 1 1. replace : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
            return false;
113 2 1. replace : changed conditional boundary → NO_COVERAGE
2. replace : negated conditional → NO_COVERAGE
        for (int i = 0; i < nodes.getLength(); i++) {
114
            node = nodes.item(i);
115
            setNodeText(domDocument, node, value);
116
        }
117 1 1. replace : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
        return true;
118
    }    
119
    
120
    /**
121
     * Adds a tag.
122
     * @param    namespaceURI    the URI of the namespace
123
     * @param    parent            the tag name of the parent
124
     * @param    localName        the name of the tag to add
125
     * @param    value            the new content for the tag
126
     * @return    true if the content was successfully added
127
     * @since    2.1.6
128
     */
129
    public boolean add(String parent, String namespaceURI, String localName, String value) {
130
        NodeList nodes = domDocument.getElementsByTagName(parent);
131 1 1. add : negated conditional → NO_COVERAGE
        if (nodes.getLength() == 0)
132 1 1. add : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
            return false;
133
        Node pNode;
134
        Node node;
135 3 1. add : changed conditional boundary → NO_COVERAGE
2. add : Changed increment from 1 to -1 → NO_COVERAGE
3. add : negated conditional → NO_COVERAGE
        for (int i = 0; i < nodes.getLength(); i++) {
136
            pNode = nodes.item(i);
137
            NamedNodeMap attrs = pNode.getAttributes();
138 2 1. add : changed conditional boundary → NO_COVERAGE
2. add : negated conditional → NO_COVERAGE
            for (int j = 0; j < attrs.getLength(); j++) {
139
                node = attrs.item(j);
140 1 1. add : negated conditional → NO_COVERAGE
                if (namespaceURI.equals(node.getNodeValue())) {
141
                    node = domDocument.createElement(localName);
142
                    node.appendChild(domDocument.createTextNode(value));
143
                    pNode.appendChild(node);
144 1 1. add : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
                    return true;
145
                }
146
            }
147
        }
148 1 1. add : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
        return false;
149
    }
150
    
151
    /**
152
     * Sets the text of this node. All the child's node are deleted and a new
153
     * child text node is created.
154
     * @param domDocument the <CODE>Document</CODE> that contains the node
155
     * @param n the <CODE>Node</CODE> to add the text to
156
     * @param value the text to add
157
     */
158
    public boolean setNodeText(Document domDocument, Node n, String value) {
159 1 1. setNodeText : negated conditional → NO_COVERAGE
        if (n == null)
160 1 1. setNodeText : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
            return false;
161
        Node nc = null;
162 1 1. setNodeText : negated conditional → NO_COVERAGE
        while ((nc = n.getFirstChild()) != null) {
163
            n.removeChild(nc);
164
        }
165
        n.appendChild(domDocument.createTextNode(value));
166 1 1. setNodeText : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
        return true;
167
    }
168
    
169
    /**
170
     * Writes the document to a byte array.
171
     */
172
    public byte[] serializeDoc() throws IOException {
173
        XmlDomWriter xw = new XmlDomWriter();
174
        ByteArrayOutputStream fout = new ByteArrayOutputStream();
175 1 1. serializeDoc : removed call to com/lowagie/text/xml/XmlDomWriter::setOutput → NO_COVERAGE
        xw.setOutput(fout, null);
176 1 1. serializeDoc : removed call to java/io/ByteArrayOutputStream::write → NO_COVERAGE
        fout.write(XmpWriter.XPACKET_PI_BEGIN.getBytes(StandardCharsets.UTF_8));
177 1 1. serializeDoc : removed call to java/io/ByteArrayOutputStream::flush → NO_COVERAGE
        fout.flush();
178
        NodeList xmpmeta = domDocument.getElementsByTagName("x:xmpmeta");
179 1 1. serializeDoc : removed call to com/lowagie/text/xml/XmlDomWriter::write → NO_COVERAGE
        xw.write(xmpmeta.item(0));
180 1 1. serializeDoc : removed call to java/io/ByteArrayOutputStream::flush → NO_COVERAGE
        fout.flush();
181 2 1. serializeDoc : changed conditional boundary → NO_COVERAGE
2. serializeDoc : negated conditional → NO_COVERAGE
        for (int i = 0; i < 20; i++) {
182 1 1. serializeDoc : removed call to java/io/ByteArrayOutputStream::write → NO_COVERAGE
            fout.write(XmpWriter.EXTRASPACE.getBytes());
183
        }
184 1 1. serializeDoc : removed call to java/io/ByteArrayOutputStream::write → NO_COVERAGE
        fout.write(XmpWriter.XPACKET_PI_END_W.getBytes());
185 1 1. serializeDoc : removed call to java/io/ByteArrayOutputStream::close → NO_COVERAGE
        fout.close();
186 1 1. serializeDoc : mutated return of Object value for com/lowagie/text/xml/xmp/XmpReader::serializeDoc to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return fout.toByteArray();
187
    }
188
}

Mutations

90

1.1
Location :
Killed by : none
removed call to javax/xml/parsers/DocumentBuilderFactory::setNamespaceAware → NO_COVERAGE

92

1.1
Location :
Killed by : none
removed call to javax/xml/parsers/DocumentBuilder::setEntityResolver → NO_COVERAGE

111

1.1
Location : replace
Killed by : none
negated conditional → NO_COVERAGE

112

1.1
Location : replace
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

113

1.1
Location : replace
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : replace
Killed by : none
negated conditional → NO_COVERAGE

117

1.1
Location : replace
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

131

1.1
Location : add
Killed by : none
negated conditional → NO_COVERAGE

132

1.1
Location : add
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

135

1.1
Location : add
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : add
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : add
Killed by : none
negated conditional → NO_COVERAGE

138

1.1
Location : add
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : add
Killed by : none
negated conditional → NO_COVERAGE

140

1.1
Location : add
Killed by : none
negated conditional → NO_COVERAGE

144

1.1
Location : add
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

148

1.1
Location : add
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

159

1.1
Location : setNodeText
Killed by : none
negated conditional → NO_COVERAGE

160

1.1
Location : setNodeText
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

162

1.1
Location : setNodeText
Killed by : none
negated conditional → NO_COVERAGE

166

1.1
Location : setNodeText
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

175

1.1
Location : serializeDoc
Killed by : none
removed call to com/lowagie/text/xml/XmlDomWriter::setOutput → NO_COVERAGE

176

1.1
Location : serializeDoc
Killed by : none
removed call to java/io/ByteArrayOutputStream::write → NO_COVERAGE

177

1.1
Location : serializeDoc
Killed by : none
removed call to java/io/ByteArrayOutputStream::flush → NO_COVERAGE

179

1.1
Location : serializeDoc
Killed by : none
removed call to com/lowagie/text/xml/XmlDomWriter::write → NO_COVERAGE

180

1.1
Location : serializeDoc
Killed by : none
removed call to java/io/ByteArrayOutputStream::flush → NO_COVERAGE

181

1.1
Location : serializeDoc
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : serializeDoc
Killed by : none
negated conditional → NO_COVERAGE

182

1.1
Location : serializeDoc
Killed by : none
removed call to java/io/ByteArrayOutputStream::write → NO_COVERAGE

184

1.1
Location : serializeDoc
Killed by : none
removed call to java/io/ByteArrayOutputStream::write → NO_COVERAGE

185

1.1
Location : serializeDoc
Killed by : none
removed call to java/io/ByteArrayOutputStream::close → NO_COVERAGE

186

1.1
Location : serializeDoc
Killed by : none
mutated return of Object value for com/lowagie/text/xml/xmp/XmpReader::serializeDoc to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.4.2