@@ -143,7 +143,7 @@ public XMPMetadata parse(InputStream input) throws XmpParsingException
143143 XMPMetadata xmp = null ;
144144
145145 // Start reading
146- removeComments (document );
146+ removeCommentsAndBlanks (document );
147147 Node node = document .getFirstChild ();
148148
149149 // expect xpacket processing instruction
@@ -991,25 +991,17 @@ private Element findDescriptionsParent(Element root) throws XmpParsingException
991991 // empty description
992992 throw new XmpParsingException (ErrorType .Format , "No rdf description found in xmp" );
993993 }
994- else if (nl .getLength () > 1 && strictParsing )
994+ else if (nl .getLength () > 1 )
995995 {
996996 // only expect one element
997997 throw new XmpParsingException (ErrorType .Format , "More than one element found in x:xmpmeta" );
998998 }
999- // find element (there may be a text before the element)
1000- for (int i = 0 ; i < nl .getLength (); ++i )
1001- {
1002- if (nl .item (i ) instanceof Element )
1003- {
1004- rdfRdf = (Element ) nl .item (i );
1005- break ;
1006- }
1007- }
1008- if (rdfRdf == null )
999+ else if (!(root .getFirstChild () instanceof Element ))
10091000 {
10101001 // should be an element
1011- throw new XmpParsingException (ErrorType .Format , "x:xmpmeta does not contains rdf:RDF element" );
1002+ throw new XmpParsingException (ErrorType .Format , "x:xmpmeta does not contains rdf:RDF element but " + root . getFirstChild () );
10121003 } // else let's parse
1004+ rdfRdf = (Element ) root .getFirstChild ();
10131005 }
10141006 else
10151007 {
@@ -1043,24 +1035,23 @@ else if ((ln != null) && !(ln.equals(element.getLocalName())))
10431035 }
10441036
10451037 /**
1046- * Remove all the comments node in the parent element of the parameter
1047- *
1048- * @param root
1049- * the first node of an element or document to clear
1038+ * Remove all the comments and blank nodes in the parent element of the parameter
1039+ *
1040+ * @param root the first node of an element or document to clear
10501041 */
1051- private void removeComments (Node root )
1042+ private void removeCommentsAndBlanks (Node root )
10521043 {
10531044 // will hold the nodes which are to be deleted
10541045 List <Node > forDeletion = new ArrayList <>();
1055-
1046+
10561047 NodeList nl = root .getChildNodes ();
1057-
1058- if (nl .getLength ()<= 1 )
1048+
1049+ if (!( root instanceof Document ) && nl .getLength () <= 1 )
10591050 {
1060- // There is only one node so we do not remove it
1051+ // There is only one node so we're done, except when Document
10611052 return ;
10621053 }
1063-
1054+
10641055 for (int i = 0 ; i < nl .getLength (); i ++)
10651056 {
10661057 Node node = nl .item (i );
@@ -1073,15 +1064,14 @@ else if (node instanceof Text)
10731064 {
10741065 if (node .getTextContent ().isBlank ())
10751066 {
1076- // TODO: verify why this is necessary
10771067 // empty text nodes to be deleted
10781068 forDeletion .add (node );
10791069 }
10801070 }
10811071 else if (node instanceof Element )
10821072 {
10831073 // clean child
1084- removeComments (node );
1074+ removeCommentsAndBlanks (node );
10851075 } // else do nothing
10861076 }
10871077
0 commit comments