11/*****************************************************************************
2- *
2+ *
33 * Licensed to the Apache Software Foundation (ASF) under one
44 * or more contributor license agreements. See the NOTICE file
55 * distributed with this work for additional information
66 * regarding copyright ownership. The ASF licenses this file
77 * to you under the Apache License, Version 2.0 (the
88 * "License"); you may not use this file except in compliance
99 * with the License. You may obtain a copy of the License at
10- *
10+ *
1111 * http://www.apache.org/licenses/LICENSE-2.0
12- *
12+ *
1313 * Unless required by applicable law or agreed to in writing,
1414 * software distributed under the License is distributed on an
1515 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1616 * KIND, either express or implied. See the License for the
1717 * specific language governing permissions and limitations
1818 * under the License.
19- *
19+ *
2020 ****************************************************************************/
2121
2222package org .apache .xmpbox .xml ;
5353import static org .junit .jupiter .api .Assertions .assertNotNull ;
5454import static org .junit .jupiter .api .Assertions .assertNull ;
5555import static org .junit .jupiter .api .Assertions .assertThrows ;
56+ import static org .junit .jupiter .api .Assertions .assertTrue ;
5657import org .junit .jupiter .api .Test ;
5758
5859/**
@@ -178,7 +179,7 @@ void testPDFBox5288() throws XmpParsingException
178179 /**
179180 * Test PageTextSchema and XMPMediaManagementSchema.
180181 *
181- * @throws XmpParsingException
182+ * @throws XmpParsingException
182183 */
183184 @ Test
184185 void testPageTextSchema () throws XmpParsingException
@@ -291,7 +292,7 @@ void testPageTextSchema3() throws XmpParsingException
291292 * verify the content of the actual extension schema.
292293 *
293294 * @throws IOException
294- * @throws XmpParsingException
295+ * @throws XmpParsingException
295296 */
296297 @ Test
297298 void testPDFBox3882 () throws IOException , XmpParsingException
@@ -311,7 +312,7 @@ void testPDFBox3882() throws IOException, XmpParsingException
311312 ArrayProperty pageInfoProp = (ArrayProperty ) extensionSchema .getProperty ("PageInfo" );
312313 DefinedStructuredType dst = (DefinedStructuredType ) pageInfoProp .getAllProperties ().get (0 );
313314 assertEquals ("[number=IntegerType:1]" , dst .getProperty ("number" ).toString ());
314- assertEquals ("[origNumber=IntegerType:1]" , dst .getProperty ("origNumber" ).toString ());
315+ assertEquals ("[origNumber=IntegerType:1]" , dst .getProperty ("origNumber" ).toString ());
315316 }
316317 }
317318
@@ -431,7 +432,7 @@ void testPDFBox3882_2() throws XmpParsingException, BadFieldValueException
431432 /**
432433 * PDFBOX-5292: Test whether inline extension schema is detected.
433434 *
434- * @throws XmpParsingException
435+ * @throws XmpParsingException
435436 */
436437 @ Test
437438 void testPDFBox5292 () throws XmpParsingException , BadFieldValueException
@@ -526,7 +527,7 @@ void testPDFBox5292() throws XmpParsingException, BadFieldValueException
526527
527528 /**
528529 * Test that a Seq / Mag mixup gets detected in strict mode and gets read in lenient mode.
529- * @throws XmpParsingException
530+ * @throws XmpParsingException
530531 */
531532 @ Test
532533 void testLenientBagSeqMixup () throws XmpParsingException
@@ -561,7 +562,7 @@ void testLenientBagSeqMixup() throws XmpParsingException
561562 assertEquals ("Important subject" , subjects .get (0 ));
562563 assertEquals ("Unimportant subject" , subjects .get (1 ));
563564 }
564-
565+
565566 @ Test
566567 void testBadAttr () throws XmpParsingException
567568 {
@@ -701,7 +702,7 @@ void testBadXPacketEnd2() throws XmpParsingException
701702 void testNoRdfChildren () throws XmpParsingException
702703 {
703704 String s = "<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" no\" ?>\n " +
704- "<?xpacket begin=\" \" id=\" W5M0MpCehiHzreSzNTczkc9d\" ?>" +
705+ "<?xpacket begin=\" \" id=\" W5M0MpCehiHzreSzNTczkc9d\" ?>" +
705706 " <x:xmpmeta xmlns:x=\" adobe:ns:meta/\" />\n " +
706707 "<?xpacket end=\" w\" ?>" ;
707708 final DomXmpParser xmpParser = new DomXmpParser ();
@@ -777,7 +778,6 @@ void testBadAttr2() throws XmpParsingException
777778 " </rdf:Description>\n " +
778779 " </rdf:RDF>\n " +
779780 "</x:xmpmeta><?xpacket end='r'?>" ;
780-
781781 final DomXmpParser xmpParser1 = new DomXmpParser ();
782782 XmpParsingException ex = assertThrows (
783783 XmpParsingException .class ,
@@ -860,7 +860,7 @@ void testBadAttr4() throws XmpParsingException, TransformerException
860860 *
861861 * @throws XmpParsingException
862862 * @throws TransformerException
863- * @throws BadFieldValueException
863+ * @throws BadFieldValueException
864864 */
865865 @ Test
866866 void testBadAttr5 () throws XmpParsingException , TransformerException , BadFieldValueException
@@ -911,7 +911,6 @@ void testBadSchema() throws XmpParsingException
911911 " </rdf:Description>\n " +
912912 " </rdf:RDF>\n " +
913913 "</x:xmpmeta><?xpacket end='w'?>" ;
914-
915914 final DomXmpParser xmpParser1 = new DomXmpParser ();
916915 XmpParsingException ex = assertThrows (
917916 XmpParsingException .class ,
@@ -1060,4 +1059,88 @@ void testBadProp() throws XmpParsingException, TransformerException
10601059 assertNull (dublinCoreSchema3 .getProperty (DublinCoreSchema .CREATOR ));
10611060 assertEquals ("Cover" , dublinCoreSchema3 .getCoverage ());
10621061 }
1062+
1063+ @ Test
1064+ void testParseFailure () throws XmpParsingException
1065+ {
1066+ String s = "<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" no\" ?>" ;
1067+ final DomXmpParser xmpParser1 = new DomXmpParser ();
1068+ XmpParsingException ex = assertThrows (XmpParsingException .class ,
1069+ () -> xmpParser1 .parse (s .getBytes (StandardCharsets .UTF_8 )));
1070+ assertTrue (ex .getMessage ().startsWith ("Failed to parse: " ));
1071+ }
1072+
1073+ @ Test
1074+ void testNoXPacket () throws XmpParsingException
1075+ {
1076+ // must be "xpacket", not "packet"
1077+ String s = "<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" no\" ?>\n " +
1078+ "<?packet begin=\" \" id=\" W5M0MpCehiHzreSzNTczkc9d\" ?><x:xmpmeta xmlns:x=\" adobe:ns:meta/\" x:xmptk=\" 3.1-701\" >\n " +
1079+ "</x:xmpmeta><?packet end=\" w\" ?>" ;
1080+ final DomXmpParser xmpParser1 = new DomXmpParser ();
1081+ XmpParsingException ex = assertThrows (XmpParsingException .class ,
1082+ () -> xmpParser1 .parse (s .getBytes (StandardCharsets .UTF_8 )));
1083+ assertEquals ("Bad processing instruction name : packet" , ex .getMessage ());
1084+ }
1085+
1086+ @ Test
1087+ void testDoubleEnd () throws XmpParsingException
1088+ {
1089+ String s = "<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?> \n " +
1090+ "<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?> \n " +
1091+ "<x:xmpmeta xmlns:x=\" adobe:ns:meta/\" \n " +
1092+ " x:xmptk=\" Adobe XMP Core 4.0-c316 44.253921, Sun Oct 01 2006 17:14:39\" >\n " +
1093+ " <rdf:RDF xmlns:rdf=\" http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >\n " +
1094+ " </rdf:RDF>\n " +
1095+ "</x:xmpmeta> \n " +
1096+ "<?xpacket end=\" w\" ?> \n " +
1097+ "<?xpacket end='r'?> " ;
1098+ final DomXmpParser xmpParser = new DomXmpParser ();
1099+ XmpParsingException ex = assertThrows (XmpParsingException .class ,
1100+ () -> xmpParser .parse (s .getBytes (StandardCharsets .UTF_8 )));
1101+ assertEquals ("xmp should end after xpacket end processing instruction" , ex .getMessage ());
1102+ }
1103+
1104+ @ Test
1105+ void testBadInner () throws XmpParsingException
1106+ {
1107+ // file has "xmpMM:parseType". Changing this to "rdf:parseType" makes it work.
1108+ final String s = "<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" no\" ?>\n " +
1109+ "<?xpacket begin=\" \" id=\" W5M0MpCehiHzreSzNTczkc9d\" ?><x:xmpmeta xmlns:x=\" adobe:ns:meta/\" x:xmptk=\" Adobe XMP Core 5.2-c001 63.139439, 2010/09/27-13:37:26 \" >\n " +
1110+ " <rdf:RDF xmlns:rdf=\" http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >\n " +
1111+ " <rdf:Description xmlns:dc=\" http://purl.org/dc/elements/1.1/\" xmlns:pdf=\" http://ns.adobe.com/pdf/1.3/\" xmlns:photoshop=\" http://ns.adobe.com/photoshop/1.0/\" xmlns:stEvt=\" http://ns.adobe.com/xap/1.0/sType/ResourceEvent#\" xmlns:stRef=\" http://ns.adobe.com/xap/1.0/sType/ResourceRef#\" xmlns:xmpMM=\" http://ns.adobe.com/xap/1.0/mm/\" xmlns:xmpRights=\" http://ns.adobe.com/xap/1.0/rights/\" >\n " +
1112+ " <xmpMM:DerivedFrom xmpMM:parseType=\" Resource\" >\n " +
1113+ " <stRef:instanceID>uuid:6b838c4d-07e2-0611-2333-558805f93988</stRef:instanceID>\n " +
1114+ " <stRef:documentID>uuid:6b838c4d-07e2-0611-2333-558805f93988</stRef:documentID>\n " +
1115+ " </xmpMM:DerivedFrom>\n " +
1116+ " </rdf:Description>\n " +
1117+ " </rdf:RDF>\n " +
1118+ "</x:xmpmeta><?xpacket end=\" w\" ?>" ;
1119+ final DomXmpParser xmpParser1 = new DomXmpParser ();
1120+ XmpParsingException ex = assertThrows (XmpParsingException .class ,
1121+ () -> xmpParser1 .parse (s .getBytes (StandardCharsets .UTF_8 )));
1122+ assertEquals ("inner element should contain child elements : [stRef:instanceID: null]" , ex .getMessage ());
1123+ String s2 = s .replace ("xmpMM:parseType" , "rdf:parseType" );
1124+ DomXmpParser xmpParser2 = new DomXmpParser ();
1125+ XMPMetadata xmp2 = xmpParser2 .parse (s2 .getBytes (StandardCharsets .UTF_8 ));
1126+ XMPMediaManagementSchema xmpMediaManagementSchema = xmp2 .getXMPMediaManagementSchema ();
1127+ ResourceRefType derivedFromProperty = xmpMediaManagementSchema .getDerivedFromProperty ();
1128+ assertEquals ("uuid:6b838c4d-07e2-0611-2333-558805f93988" , derivedFromProperty .getInstanceID ());
1129+ assertEquals ("uuid:6b838c4d-07e2-0611-2333-558805f93988" , derivedFromProperty .getDocumentID ());
1130+ }
1131+
1132+ @ Test
1133+ void testBadRdfNameSpace () throws XmpParsingException
1134+ {
1135+ // has https in rdf namespace
1136+ final String s = "<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" no\" ?>\n " +
1137+ "<?xpacket begin=\" \" id=\" W5M0MpCehiHzreSzNTczkc9d\" ?><x:xmpmeta xmlns:x=\" adobe:ns:meta/\" x:xmptk=\" XXX\" >\n " +
1138+ " <rdf:RDF xmlns:rdf=\" https://www.w3.org/1999/02/22-rdf-syntax-ns#\" >\n " +
1139+ " </rdf:RDF>\n " +
1140+ "</x:xmpmeta><?xpacket end=\" w\" ?>" ;
1141+ final DomXmpParser xmpParser1 = new DomXmpParser ();
1142+ XmpParsingException ex = assertThrows (XmpParsingException .class ,
1143+ () -> xmpParser1 .parse (s .getBytes (StandardCharsets .UTF_8 )));
1144+ assertEquals ("Expecting namespace 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' and found 'https://www.w3.org/1999/02/22-rdf-syntax-ns#'" , ex .getMessage ());
1145+ }
10631146}
0 commit comments