0001: /* Copyright 2002-2005 Elliotte Rusty Harold
0002:
0003: This library is free software; you can redistribute it and/or modify
0004: it under the terms of version 2.1 of the GNU Lesser General Public
0005: License as published by the Free Software Foundation.
0006:
0007: This library is distributed in the hope that it will be useful,
0008: but WITHOUT ANY WARRANTY; without even the implied warranty of
0009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0010: GNU Lesser General Public License for more details.
0011:
0012: You should have received a copy of the GNU Lesser General Public
0013: License along with this library; if not, write to the
0014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
0015: Boston, MA 02111-1307 USA
0016:
0017: You can contact Elliotte Rusty Harold by sending e-mail to
0018: elharo@metalab.unc.edu. Please include the word "XOM" in the
0019: subject line. The XOM home page is located at http://www.xom.nu/
0020: */
0021:
0022: package nu.xom.tests;
0023:
0024: import java.io.BufferedInputStream;
0025: import java.io.ByteArrayInputStream;
0026: import java.io.ByteArrayOutputStream;
0027: import java.io.DataInputStream;
0028: import java.io.File;
0029: import java.io.FileInputStream;
0030: import java.io.FilenameFilter;
0031: import java.io.IOException;
0032: import java.io.InputStream;
0033: import java.io.OutputStream;
0034:
0035: import nu.xom.Attribute;
0036: import nu.xom.Builder;
0037: import nu.xom.Comment;
0038: import nu.xom.DocType;
0039: import nu.xom.Document;
0040: import nu.xom.Element;
0041: import nu.xom.Elements;
0042: import nu.xom.Namespace;
0043: import nu.xom.Nodes;
0044: import nu.xom.ParsingException;
0045: import nu.xom.ProcessingInstruction;
0046: import nu.xom.Serializer;
0047: import nu.xom.Text;
0048: import nu.xom.XPathContext;
0049: import nu.xom.canonical.CanonicalizationException;
0050: import nu.xom.canonical.Canonicalizer;
0051:
0052: /**
0053: * <p>
0054: * Tests canonicalization.
0055: * </p>
0056: *
0057: * @author Elliotte Rusty Harold
0058: * @version 1.1b6
0059: *
0060: */
0061: public class CanonicalizerTest extends XOMTestCase {
0062:
0063: private final static double version = Double.parseDouble(System
0064: .getProperty("java.version").substring(0, 3));
0065:
0066: private File canonical;
0067: private File input;
0068: private File output;
0069:
0070: public CanonicalizerTest(String name) {
0071: super (name);
0072: }
0073:
0074: private Builder builder = new Builder();
0075:
0076: protected void setUp() {
0077: File data = new File("data");
0078: canonical = new File(data, "canonical");
0079: input = new File(canonical, "input");
0080: output = new File(canonical, "output");
0081: }
0082:
0083: public void testCanonicalizeOnlyAttributes() throws IOException {
0084:
0085: Element pdu = new Element("doc");
0086: pdu.addAttribute(new Attribute("a1", "v1"));
0087: pdu.addAttribute(new Attribute("a2", "v2"));
0088:
0089: String expected = " a1=\"v1\" a2=\"v2\"";
0090: ByteArrayOutputStream out = new ByteArrayOutputStream();
0091: Canonicalizer canonicalizer = new Canonicalizer(out);
0092:
0093: Document doc = new Document(pdu);
0094: canonicalizer.write(doc.query("//@*"));
0095:
0096: byte[] result = out.toByteArray();
0097: out.close();
0098: String s = new String(out.toByteArray(), "UTF8");
0099: assertEquals(expected, s);
0100:
0101: }
0102:
0103: public void testRemoveDuplicateAttributes() throws IOException {
0104:
0105: Element pdu = new Element("doc");
0106: Attribute a1 = new Attribute("a1", "v1");
0107: pdu.addAttribute(a1);
0108: Attribute a2 = new Attribute("a2", "v2");
0109: pdu.addAttribute(a2);
0110:
0111: String expected = " a1=\"v1\" a2=\"v2\"";
0112: ByteArrayOutputStream out = new ByteArrayOutputStream();
0113: Canonicalizer canonicalizer = new Canonicalizer(out);
0114:
0115: Document doc = new Document(pdu);
0116: Nodes subset = doc.query("//@*");
0117: subset.append(a1);
0118: subset.append(a2);
0119: canonicalizer.write(subset);
0120:
0121: byte[] result = out.toByteArray();
0122: out.close();
0123: String s = new String(out.toByteArray(), "UTF8");
0124: assertEquals(expected, s);
0125:
0126: }
0127:
0128: public void testCanonicalizeOnlyNamespaces() throws IOException {
0129:
0130: Element pdu = new Element("doc", "http://www.example.com");
0131:
0132: String expected = " xmlns=\"http://www.example.com\"";
0133: ByteArrayOutputStream out = new ByteArrayOutputStream();
0134: Canonicalizer canonicalizer = new Canonicalizer(out);
0135:
0136: Document doc = new Document(pdu);
0137: canonicalizer.write(doc.query("//namespace::node()"));
0138:
0139: byte[] result = out.toByteArray();
0140: out.close();
0141: String s = new String(out.toByteArray(), "UTF8");
0142: assertEquals(expected, s);
0143:
0144: }
0145:
0146: public void testCanonicalizeOnlyPrefixedNamespaces()
0147: throws IOException {
0148:
0149: Element pdu = new Element("pre:doc", "http://www.example.com");
0150:
0151: String expected = " xmlns:pre=\"http://www.example.com\"";
0152: ByteArrayOutputStream out = new ByteArrayOutputStream();
0153: Canonicalizer canonicalizer = new Canonicalizer(out);
0154:
0155: Document doc = new Document(pdu);
0156: canonicalizer.write(doc.query("//namespace::node()"));
0157:
0158: byte[] result = out.toByteArray();
0159: out.close();
0160: String s = new String(out.toByteArray(), "UTF8");
0161: assertEquals(expected, s);
0162:
0163: }
0164:
0165: public void testCanonicalizeWithNullAlgorithm() throws IOException {
0166:
0167: ByteArrayOutputStream out = new ByteArrayOutputStream();
0168: try {
0169: new Canonicalizer(out, null);
0170: fail("Allowed null algorithm");
0171: } catch (NullPointerException success) {
0172: assertNotNull(success.getMessage());
0173: }
0174:
0175: }
0176:
0177: public void testCanonicalizeCommentsInPrologAndEpilog()
0178: throws IOException {
0179:
0180: Element pdu = new Element("doc");
0181:
0182: Document doc = new Document(pdu);
0183: doc.insertChild(new Comment("comment 1"), 0);
0184: doc.insertChild(new Comment("comment 2"), 1);
0185: doc.appendChild(new Comment("comment 3"));
0186: doc.appendChild(new Comment("comment 4"));
0187:
0188: String expected = "<!--comment 1-->\n<!--comment 2-->\n\n<!--comment 3-->\n<!--comment 4-->";
0189: ByteArrayOutputStream out = new ByteArrayOutputStream();
0190: Canonicalizer canonicalizer = new Canonicalizer(out);
0191:
0192: canonicalizer.write(doc.query("//comment()"));
0193:
0194: byte[] result = out.toByteArray();
0195: out.close();
0196: String s = new String(out.toByteArray(), "UTF8");
0197: assertEquals(expected, s);
0198:
0199: }
0200:
0201: public void testTamin() throws ParsingException, IOException {
0202:
0203: String input = "<ns1:root xmlns:ns1='http://www.example.org/'><elt1></elt1></ns1:root>";
0204: Document doc = builder.build(input, null);
0205:
0206: ByteArrayOutputStream out = new ByteArrayOutputStream();
0207: Canonicalizer canonicalizer = new Canonicalizer(out);
0208:
0209: canonicalizer.write(doc);
0210:
0211: byte[] result = out.toByteArray();
0212: out.close();
0213: String s = new String(out.toByteArray(), "UTF8");
0214: assertEquals(
0215: "<ns1:root xmlns:ns1=\"http://www.example.org/\"><elt1></elt1></ns1:root>",
0216: s);
0217:
0218: }
0219:
0220: public void testCanonicalizePrologAndEpilog() throws IOException {
0221:
0222: Element pdu = new Element("doc");
0223:
0224: Document doc = new Document(pdu);
0225: doc
0226: .insertChild(new ProcessingInstruction("target",
0227: "value"), 0);
0228: doc.insertChild(new Comment("comment 2"), 1);
0229: doc.appendChild(new Comment("comment 3"));
0230: doc.appendChild(new ProcessingInstruction("target", "value"));
0231:
0232: String expected = "<?target value?>\n<!--comment 2-->\n<doc></doc>\n<!--comment 3-->\n<?target value?>";
0233: ByteArrayOutputStream out = new ByteArrayOutputStream();
0234: Canonicalizer canonicalizer = new Canonicalizer(out);
0235:
0236: canonicalizer.write(doc);
0237:
0238: byte[] result = out.toByteArray();
0239: out.close();
0240: String s = new String(out.toByteArray(), "UTF8");
0241: assertEquals(expected, s);
0242:
0243: }
0244:
0245: public void testCanonicalizeOnlyAttributesOnDifferentElements()
0246: throws IOException {
0247:
0248: Element pdu = new Element("doc");
0249: pdu.addAttribute(new Attribute("a2", "v1"));
0250: Element child = new Element("child");
0251: child.addAttribute(new Attribute("a1", "v2"));
0252: pdu.appendChild(child);
0253:
0254: String expected = " a2=\"v1\" a1=\"v2\"";
0255: ByteArrayOutputStream out = new ByteArrayOutputStream();
0256: Canonicalizer canonicalizer = new Canonicalizer(out);
0257:
0258: Document doc = new Document(pdu);
0259: canonicalizer.write(doc.query("//@*"));
0260:
0261: byte[] result = out.toByteArray();
0262: out.close();
0263: String s = new String(out.toByteArray(), "UTF8");
0264: assertEquals(expected, s);
0265:
0266: }
0267:
0268: public void testCanonicalizeAttributesWithFunkyCharacters()
0269: throws IOException {
0270:
0271: Element pdu = new Element("doc");
0272: pdu.addAttribute(new Attribute("a2", "v1&<>\"\t\r\n"));
0273:
0274: String expected = " a2=\"v1&<>"	
\"";
0275: ByteArrayOutputStream out = new ByteArrayOutputStream();
0276: Canonicalizer canonicalizer = new Canonicalizer(out);
0277:
0278: Document doc = new Document(pdu);
0279: canonicalizer.write(doc.query("//@*"));
0280:
0281: byte[] result = out.toByteArray();
0282: out.close();
0283: String s = new String(out.toByteArray(), "UTF8");
0284: assertEquals(expected, s);
0285:
0286: }
0287:
0288: public void testExclusiveEmptyRootElementInNoNamespace()
0289: throws IOException {
0290:
0291: Element pdu = new Element("doc");
0292:
0293: String expected = "<doc></doc>";
0294: ByteArrayOutputStream out = new ByteArrayOutputStream();
0295: Canonicalizer canonicalizer = new Canonicalizer(
0296: out,
0297: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
0298:
0299: Document doc = new Document(pdu);
0300: canonicalizer.write(doc);
0301:
0302: byte[] result = out.toByteArray();
0303: out.close();
0304: String s = new String(out.toByteArray(), "UTF8");
0305: assertEquals(expected, s);
0306:
0307: }
0308:
0309: public void testExclusiveEmptyRootElementInNoNamespaceWithTwoAttributes()
0310: throws IOException {
0311:
0312: Element pdu = new Element("doc");
0313: pdu.addAttribute(new Attribute("a1", "v1"));
0314: pdu.addAttribute(new Attribute("a2", "v2"));
0315:
0316: String expected = "<doc a1=\"v1\" a2=\"v2\"></doc>";
0317: ByteArrayOutputStream out = new ByteArrayOutputStream();
0318: Canonicalizer canonicalizer = new Canonicalizer(
0319: out,
0320: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
0321:
0322: Document doc = new Document(pdu);
0323: canonicalizer.write(doc);
0324:
0325: byte[] result = out.toByteArray();
0326: out.close();
0327: String s = new String(out.toByteArray(), "UTF8");
0328: assertEquals(expected, s);
0329:
0330: }
0331:
0332: public void testExclusiveDoesntRenderUnusedPrefix()
0333: throws IOException {
0334:
0335: Element pdu = new Element("n0:tuck", "http://a.example");
0336: pdu.addNamespaceDeclaration("pre", "http://www.example.org/");
0337:
0338: String expected = "<n0:tuck xmlns:n0=\"http://a.example\"></n0:tuck>";
0339: ByteArrayOutputStream out = new ByteArrayOutputStream();
0340: Canonicalizer canonicalizer = new Canonicalizer(
0341: out,
0342: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
0343:
0344: Document doc = new Document(pdu);
0345: canonicalizer.write(doc);
0346:
0347: byte[] result = out.toByteArray();
0348: out.close();
0349: String s = new String(out.toByteArray(), "UTF8");
0350: assertEquals(expected, s);
0351:
0352: }
0353:
0354: public void testWriteDefaultNamespace() throws IOException {
0355:
0356: Element pdu = new Element("tuck", "http://www.example.org/");
0357:
0358: String expected = " xmlns=\"http://www.example.org/\"";
0359: ByteArrayOutputStream out = new ByteArrayOutputStream();
0360: Canonicalizer canonicalizer = new Canonicalizer(
0361: out,
0362: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
0363:
0364: Document doc = new Document(pdu);
0365: Nodes subset = doc.query("//namespace::node()");
0366: canonicalizer.write(subset);
0367:
0368: byte[] result = out.toByteArray();
0369: out.close();
0370: String s = new String(out.toByteArray(), "UTF8");
0371: assertEquals(expected, s);
0372:
0373: }
0374:
0375: public void testOutputAncestorAttributeAndChildHaveDifferentLanguages()
0376: throws IOException {
0377:
0378: Element pdu = new Element("tuck");
0379: pdu.addAttribute(new Attribute("xml:lang",
0380: Namespace.XML_NAMESPACE, "fr"));
0381:
0382: Element middle = new Element("middle");
0383: pdu.appendChild(middle);
0384: Element child = new Element("child");
0385: child.addAttribute(new Attribute("xml:lang",
0386: Namespace.XML_NAMESPACE, "en"));
0387: middle.appendChild(child);
0388:
0389: String expected = "<tuck xml:lang=\"fr\"><child xml:lang=\"en\"></child></tuck>";
0390: ByteArrayOutputStream out = new ByteArrayOutputStream();
0391: Canonicalizer canonicalizer = new Canonicalizer(
0392: out,
0393: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
0394:
0395: Document doc = new Document(pdu);
0396: Nodes subset = doc.query("/* | //child | //@*");
0397: canonicalizer.write(subset);
0398:
0399: byte[] result = out.toByteArray();
0400: out.close();
0401: String s = new String(out.toByteArray(), "UTF8");
0402: assertEquals(expected, s);
0403:
0404: }
0405:
0406: public void testOutputAncestorAttributeUsesPrefix()
0407: throws IOException {
0408:
0409: Element pdu = new Element("tuck");
0410: pdu.addAttribute(new Attribute("pre:foo",
0411: "http://www.example.org/", "value"));
0412: Element child = new Element("pre:test",
0413: "http://www.example.org/");
0414: pdu.appendChild(child);
0415:
0416: String expected = "<tuck xmlns:pre=\"http://www.example.org/\" pre:foo=\"value\"><pre:test></pre:test></tuck>";
0417: ByteArrayOutputStream out = new ByteArrayOutputStream();
0418: Canonicalizer canonicalizer = new Canonicalizer(
0419: out,
0420: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
0421:
0422: Document doc = new Document(pdu);
0423: canonicalizer.write(doc);
0424:
0425: byte[] result = out.toByteArray();
0426: out.close();
0427: String s = new String(out.toByteArray(), "UTF8");
0428: assertEquals(expected, s);
0429:
0430: }
0431:
0432: public void testOutputAncestorAttributeRedefinesPrefix()
0433: throws IOException {
0434:
0435: Element pdu = new Element("tuck");
0436: pdu.addAttribute(new Attribute("pre:foo",
0437: "http://www.example.com/", "value"));
0438: Element child = new Element("pre:test",
0439: "http://www.example.org/");
0440: pdu.appendChild(child);
0441:
0442: String expected = "<tuck xmlns:pre=\"http://www.example.com/\" "
0443: + "pre:foo=\"value\"><pre:test xmlns:pre=\"http://www.example.org/\"></pre:test></tuck>";
0444: ByteArrayOutputStream out = new ByteArrayOutputStream();
0445: Canonicalizer canonicalizer = new Canonicalizer(
0446: out,
0447: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
0448:
0449: Document doc = new Document(pdu);
0450: canonicalizer.write(doc);
0451:
0452: byte[] result = out.toByteArray();
0453: out.close();
0454: String s = new String(out.toByteArray(), "UTF8");
0455: assertEquals(expected, s);
0456:
0457: }
0458:
0459: public void testExclusiveDoesntRenderUnusedPrefixFromUnincludedAttribute()
0460: throws IOException {
0461:
0462: Element pdu = new Element("n0:tuck", "http://a.example");
0463: pdu.addAttribute(new Attribute("pre:foo",
0464: "http://www.example.org/", "test"));
0465:
0466: String expected = "<n0:tuck xmlns:n0=\"http://a.example\"></n0:tuck>";
0467: ByteArrayOutputStream out = new ByteArrayOutputStream();
0468: Canonicalizer canonicalizer = new Canonicalizer(
0469: out,
0470: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
0471:
0472: Document doc = new Document(pdu);
0473: canonicalizer.write(doc.query("//* | //namespace::node()"));
0474:
0475: byte[] result = out.toByteArray();
0476: out.close();
0477: String s = new String(out.toByteArray(), "UTF8");
0478: assertEquals(expected, s);
0479:
0480: }
0481:
0482: public void testWithComments() throws ParsingException, IOException {
0483:
0484: File tests = input;
0485: String[] inputs = tests.list(new XMLFilter());
0486: for (int i = 0; i < inputs.length; i++) {
0487: File input = new File(tests, inputs[i]);
0488: Document doc = builder.build(input);
0489: ByteArrayOutputStream out = new ByteArrayOutputStream();
0490: try {
0491: Canonicalizer serializer = new Canonicalizer(out);
0492: serializer.write(doc);
0493: } finally {
0494: out.close();
0495: }
0496: byte[] actual = out.toByteArray();
0497:
0498: // for debugging
0499: /* File debug = new File(canonical, "debug/"
0500: + input.getName() + ".dbg");
0501: OutputStream fout = new FileOutputStream(debug);
0502: fout.write(actual);
0503: fout.close(); */
0504:
0505: File expected = new File(output, input.getName() + ".out");
0506: assertEquals(input.getName(), expected.length(),
0507: actual.length);
0508: byte[] expectedBytes = new byte[actual.length];
0509: InputStream fin = new FileInputStream(expected);
0510: DataInputStream in = new DataInputStream(fin);
0511: try {
0512: in.readFully(expectedBytes);
0513: } finally {
0514: in.close();
0515: }
0516: for (int j = 0; j < expectedBytes.length; j++) {
0517: assertEquals(expectedBytes[i], actual[i]);
0518: }
0519:
0520: }
0521:
0522: }
0523:
0524: public void testNamedAlgorithmWithComments()
0525: throws ParsingException, IOException {
0526:
0527: File tests = input;
0528: String[] inputs = tests.list(new XMLFilter());
0529: for (int i = 0; i < inputs.length; i++) {
0530: File input = new File(tests, inputs[i]);
0531: Document doc = builder.build(input);
0532: ByteArrayOutputStream out = new ByteArrayOutputStream();
0533: try {
0534: Canonicalizer serializer = new Canonicalizer(out,
0535: Canonicalizer.CANONICAL_XML_WITH_COMMENTS);
0536: serializer.write(doc);
0537: } finally {
0538: out.close();
0539: }
0540: byte[] actual = out.toByteArray();
0541:
0542: // for debugging
0543: /* File debug = new File(canonical, "debug/"
0544: + input.getName() + ".dbg");
0545: OutputStream fout = new FileOutputStream(debug);
0546: fout.write(actual);
0547: fout.close(); */
0548:
0549: File expected = new File(output, input.getName() + ".out");
0550: assertEquals(input.getName(), expected.length(),
0551: actual.length);
0552: byte[] expectedBytes = new byte[actual.length];
0553: InputStream fin = new FileInputStream(expected);
0554: DataInputStream in = new DataInputStream(fin);
0555: try {
0556: in.readFully(expectedBytes);
0557: } finally {
0558: in.close();
0559: }
0560: for (int j = 0; j < expectedBytes.length; j++) {
0561: assertEquals(expectedBytes[i], actual[i]);
0562: }
0563:
0564: }
0565:
0566: }
0567:
0568: public void testWithoutComments() throws ParsingException,
0569: IOException {
0570:
0571: File tests = input;
0572: String[] inputs = tests.list(new XMLFilter());
0573: for (int i = 0; i < inputs.length; i++) {
0574: File input = new File(tests, inputs[i]);
0575: Document doc = builder.build(input);
0576:
0577: ByteArrayOutputStream out = new ByteArrayOutputStream();
0578: try {
0579: Canonicalizer serializer = new Canonicalizer(out, false);
0580: serializer.write(doc);
0581: } finally {
0582: out.close();
0583: }
0584:
0585: byte[] actual = out.toByteArray();
0586:
0587: File expected = new File(canonical, "wocommentsoutput/");
0588: expected = new File(expected, input.getName() + ".out");
0589: byte[] expectedBytes = new byte[actual.length];
0590: InputStream fin = new FileInputStream(expected);
0591: DataInputStream in = new DataInputStream(fin);
0592: try {
0593: in.readFully(expectedBytes);
0594: } finally {
0595: in.close();
0596: }
0597: for (int j = 0; j < expectedBytes.length; j++) {
0598: assertEquals(expectedBytes[i], actual[i]);
0599: }
0600: out.close();
0601:
0602: }
0603:
0604: }
0605:
0606: public void testNamedAlgorithmWithoutComments()
0607: throws ParsingException, IOException {
0608:
0609: File tests = input;
0610: String[] inputs = tests.list(new XMLFilter());
0611: for (int i = 0; i < inputs.length; i++) {
0612: File input = new File(tests, inputs[i]);
0613: Document doc = builder.build(input);
0614:
0615: ByteArrayOutputStream out = new ByteArrayOutputStream();
0616: try {
0617: Canonicalizer serializer = new Canonicalizer(out,
0618: Canonicalizer.CANONICAL_XML);
0619: serializer.write(doc);
0620: } finally {
0621: out.close();
0622: }
0623:
0624: byte[] actual = out.toByteArray();
0625:
0626: File expected = new File(canonical, "wocommentsoutput/");
0627: expected = new File(expected, input.getName() + ".out");
0628: byte[] expectedBytes = new byte[actual.length];
0629: InputStream fin = new FileInputStream(expected);
0630: DataInputStream in = new DataInputStream(fin);
0631: try {
0632: in.readFully(expectedBytes);
0633: } finally {
0634: in.close();
0635: }
0636: for (int j = 0; j < expectedBytes.length; j++) {
0637: assertEquals(expectedBytes[i], actual[i]);
0638: }
0639: out.close();
0640:
0641: }
0642:
0643: }
0644:
0645: public void testXMLNamespaceAttributeInheritance()
0646: throws IOException {
0647:
0648: Element root = new Element("root");
0649: Document doc = new Document(root);
0650: root.addAttribute(new Attribute("xml:id",
0651: Namespace.XML_NAMESPACE, "p1"));
0652: root.appendChild(new Element("child312"));
0653:
0654: String expected = "<child312 xml:id=\"p1\"></child312>";
0655:
0656: ByteArrayOutputStream out = new ByteArrayOutputStream();
0657: try {
0658: Canonicalizer serializer = new Canonicalizer(out, false);
0659: serializer.write(doc.query("/*/child312"));
0660: } finally {
0661: out.close();
0662: }
0663:
0664: String actual = new String(out.toByteArray(), "UTF-8");
0665: assertEquals(expected, actual);
0666:
0667: }
0668:
0669: public void testXMLNamespaceAttributeInheritanceThroughMultipleLevels()
0670: throws IOException {
0671:
0672: Element super root = new Element("superroot");
0673: Element root = new Element("root");
0674: super root.appendChild(root);
0675: super root.addAttribute(new Attribute("xml:id",
0676: Namespace.XML_NAMESPACE, "p0"));
0677: Document doc = new Document(super root);
0678: root.addAttribute(new Attribute("xml:id",
0679: Namespace.XML_NAMESPACE, "p1"));
0680: root.appendChild(new Element("child312"));
0681:
0682: String expected = "<child312 xml:id=\"p1\"></child312>";
0683:
0684: ByteArrayOutputStream out = new ByteArrayOutputStream();
0685: try {
0686: Canonicalizer serializer = new Canonicalizer(out, false);
0687: Nodes result = doc.query("/*/*/child312");
0688: serializer.write(result);
0689: } finally {
0690: out.close();
0691: }
0692:
0693: String actual = new String(out.toByteArray(), "UTF-8");
0694: assertEquals(expected, actual);
0695:
0696: }
0697:
0698: public void testXMLNamespaceAttributeInheritanceThroughMultipleLevelsWithSkippedMiddle()
0699: throws IOException {
0700:
0701: Element super root = new Element("superroot");
0702: Element root = new Element("root");
0703: super root.appendChild(root);
0704: super root.addAttribute(new Attribute("xml:id",
0705: Namespace.XML_NAMESPACE, "p0"));
0706: Document doc = new Document(super root);
0707: root.addAttribute(new Attribute("xml:id",
0708: Namespace.XML_NAMESPACE, "p1"));
0709: root.appendChild(new Element("child312"));
0710:
0711: String expected = "<superroot xml:id=\"p0\"><child312 xml:id=\"p1\"></child312></superroot>";
0712:
0713: ByteArrayOutputStream out = new ByteArrayOutputStream();
0714: try {
0715: Canonicalizer serializer = new Canonicalizer(out, false);
0716: serializer.write(doc
0717: .query("/* | //child312 | /*/@* | //child312/@*"));
0718: } finally {
0719: out.close();
0720: }
0721:
0722: String actual = new String(out.toByteArray(), "UTF-8");
0723: assertEquals(expected, actual);
0724:
0725: }
0726:
0727: public void testXMLNamespaceAttributeInheritanceNearestIsInSubset()
0728: throws IOException {
0729:
0730: Element super root = new Element("superroot");
0731: Element root = new Element("root");
0732: super root.appendChild(root);
0733: super root.addAttribute(new Attribute("xml:id",
0734: Namespace.XML_NAMESPACE, "p0"));
0735: Document doc = new Document(super root);
0736: root.appendChild(new Element("child312"));
0737:
0738: String expected = "<superroot xml:id=\"p0\"><child312></child312></superroot>";
0739:
0740: ByteArrayOutputStream out = new ByteArrayOutputStream();
0741: try {
0742: Canonicalizer serializer = new Canonicalizer(out, false);
0743: serializer.write(doc
0744: .query("/* | //child312 | /*/@* | //child312/@*"));
0745: } finally {
0746: out.close();
0747: }
0748:
0749: String actual = new String(out.toByteArray(), "UTF-8");
0750: assertEquals(expected, actual);
0751:
0752: }
0753:
0754: public void testXMLNamespaceAttributeNotOverridden()
0755: throws IOException {
0756:
0757: Element root = new Element("root");
0758: Document doc = new Document(root);
0759: Element child = new Element("child312");
0760:
0761: root.addAttribute(new Attribute("xml:id",
0762: Namespace.XML_NAMESPACE, "p1"));
0763: child.addAttribute(new Attribute("xml:id",
0764: Namespace.XML_NAMESPACE, "p2"));
0765:
0766: root.appendChild(child);
0767:
0768: String expected = "<child312 xml:id=\"p2\"></child312>";
0769:
0770: ByteArrayOutputStream out = new ByteArrayOutputStream();
0771: try {
0772: Canonicalizer serializer = new Canonicalizer(out, false);
0773: serializer.write(doc.query("/*/child312 | /*/*/@*"));
0774: } finally {
0775: out.close();
0776: }
0777:
0778: String actual = new String(out.toByteArray(), "UTF-8");
0779: assertEquals(expected, actual);
0780:
0781: }
0782:
0783: public void testXMLNamespaceAttributeNotOverridden2()
0784: throws IOException {
0785:
0786: Element root = new Element("root");
0787: Document doc = new Document(root);
0788: Element child = new Element("child312");
0789:
0790: root.addAttribute(new Attribute("xml:id",
0791: Namespace.XML_NAMESPACE, "p1"));
0792: child.addAttribute(new Attribute("xml:id",
0793: Namespace.XML_NAMESPACE, "p2"));
0794:
0795: root.appendChild(child);
0796:
0797: String expected = "<child312></child312>";
0798:
0799: ByteArrayOutputStream out = new ByteArrayOutputStream();
0800: try {
0801: Canonicalizer serializer = new Canonicalizer(out, false);
0802: serializer.write(doc.query("/*/child312 "));
0803: } finally {
0804: out.close();
0805: }
0806:
0807: String actual = new String(out.toByteArray(), "UTF-8");
0808: assertEquals(expected, actual);
0809:
0810: }
0811:
0812: public void testXMLNamespaceAttributeNotInheritedWithExclusiveCanonicalization()
0813: throws IOException {
0814:
0815: Element root = new Element("root");
0816: Document doc = new Document(root);
0817: root.addAttribute(new Attribute("xml:id",
0818: Namespace.XML_NAMESPACE, "p1"));
0819: root.appendChild(new Element("child312"));
0820:
0821: String expected = "<child312></child312>";
0822:
0823: ByteArrayOutputStream out = new ByteArrayOutputStream();
0824: try {
0825: Canonicalizer serializer = new Canonicalizer(out,
0826: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
0827: serializer.write(doc.query("/*/child312"));
0828: } finally {
0829: out.close();
0830: }
0831:
0832: String actual = new String(out.toByteArray(), "UTF-8");
0833: assertEquals(expected, actual);
0834:
0835: }
0836:
0837: public void testXMLNSAttributeNotInheritedWithExclusiveCanonicalization()
0838: throws IOException {
0839:
0840: Element root = new Element("root", "http://www.example.org/");
0841: Document doc = new Document(root);
0842: root.appendChild(new Element("child312",
0843: "http://www.example.org/"));
0844:
0845: String expected = "<child312 xmlns=\"http://www.example.org/\"></child312>";
0846:
0847: ByteArrayOutputStream out = new ByteArrayOutputStream();
0848: try {
0849: Canonicalizer serializer = new Canonicalizer(out,
0850: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
0851: XPathContext context = new XPathContext("pre",
0852: "http://www.example.org/");
0853: serializer
0854: .write(doc
0855: .query(
0856: "/*/pre:child312 | /*/pre:child312/namespace::node()",
0857: context));
0858: } finally {
0859: out.close();
0860: }
0861:
0862: String actual = new String(out.toByteArray(), "UTF-8");
0863: assertEquals(expected, actual);
0864:
0865: }
0866:
0867: public void testXMLNSPrefixAttributeInheritedWithExclusiveCanonicalization()
0868: throws IOException {
0869:
0870: Element root = new Element("pre:root",
0871: "http://www.example.org/");
0872: Document doc = new Document(root);
0873: root.appendChild(new Element("pre:child312",
0874: "http://www.example.org/"));
0875:
0876: String expected = "<pre:child312 xmlns:pre=\"http://www.example.org/\"></pre:child312>";
0877:
0878: ByteArrayOutputStream out = new ByteArrayOutputStream();
0879: try {
0880: XPathContext context = new XPathContext("pre",
0881: "http://www.example.org/");
0882: Canonicalizer serializer = new Canonicalizer(out,
0883: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
0884: serializer
0885: .write(doc
0886: .query(
0887: "/*/pre:child312 | /*/pre:child312/namespace::node()",
0888: context));
0889: } finally {
0890: out.close();
0891: }
0892:
0893: String actual = new String(out.toByteArray(), "UTF-8");
0894: assertEquals(expected, actual);
0895:
0896: }
0897:
0898: public void testXMLNSEqualsEmptyString() throws IOException {
0899:
0900: Element root = new Element("root", "http://www.ietf.org");
0901: Document doc = new Document(root);
0902: root.appendChild(new Element("child"));
0903:
0904: String expected = "<root xmlns=\"http://www.ietf.org\"><child xmlns=\"\"></child></root>";
0905:
0906: ByteArrayOutputStream out = new ByteArrayOutputStream();
0907: try {
0908: Canonicalizer serializer = new Canonicalizer(out, false);
0909: serializer.write(doc);
0910: } finally {
0911: out.close();
0912: }
0913:
0914: String actual = new String(out.toByteArray(), "UTF-8");
0915: assertEquals(expected, actual);
0916:
0917: }
0918:
0919: // from section 3.7 of spec
0920: public void testDocumentSubsetCanonicalization()
0921: throws ParsingException, IOException {
0922:
0923: String input = "<!DOCTYPE doc [\n"
0924: + "<!ATTLIST e2 xml:space (default|preserve) 'preserve'>\n"
0925: + "<!ATTLIST e3 id ID #IMPLIED>\n"
0926: + "]>\n"
0927: + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n"
0928: + " <e1>\n" + " <e2 xmlns=\"\">\n"
0929: + " <e3 id=\"E3\"/>\n" + " </e2>\n"
0930: + " </e1>\n" + "</doc>";
0931:
0932: Document doc = builder.build(input, null);
0933: XPathContext context = new XPathContext("ietf",
0934: "http://www.ietf.org");
0935: String xpath = "(//. | //@* | //namespace::*)\n"
0936: + "[\n"
0937: + "self::ietf:e1 or (parent::ietf:e1 and not(self::text() or self::e2))"
0938: + " or\n"
0939: + " count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n"
0940: + "]";
0941:
0942: String expected = "<e1 xmlns=\"http://www.ietf.org\" "
0943: + "xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";
0944:
0945: ByteArrayOutputStream out = new ByteArrayOutputStream();
0946: try {
0947: Canonicalizer serializer = new Canonicalizer(out, false);
0948: serializer.write(doc.query(xpath, context));
0949: } finally {
0950: out.close();
0951: }
0952:
0953: String actual = new String(out.toByteArray(), "UTF-8");
0954: assertEquals(expected, actual);
0955:
0956: }
0957:
0958: public void testCanonicalizeEmptyDocumentSubset()
0959: throws ParsingException, IOException {
0960:
0961: String input = "<!DOCTYPE doc [\n"
0962: + "<!ATTLIST e2 xml:space (default|preserve) 'preserve'>\n"
0963: + "<!ATTLIST e3 id ID #IMPLIED>\n"
0964: + "]>\n"
0965: + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n"
0966: + " <e1>\n" + " <e2 xmlns=\"\">\n"
0967: + " <e3 id=\"E3\"/>\n" + " </e2>\n"
0968: + " </e1>\n" + "</doc>";
0969:
0970: Document doc = builder.build(input, null);
0971: XPathContext context = new XPathContext("ietf",
0972: "http://www.ietf.org");
0973: String xpath = "//aaa";
0974: ByteArrayOutputStream out = new ByteArrayOutputStream();
0975: try {
0976: Canonicalizer serializer = new Canonicalizer(out, false);
0977: serializer.write(doc.query(xpath, context));
0978: } finally {
0979: out.close();
0980: }
0981:
0982: String actual = new String(out.toByteArray(), "UTF-8");
0983: assertEquals("", actual);
0984:
0985: }
0986:
0987: public void testDocumentSubsetCanonicalizationSkippingProcessingInstructions()
0988: throws ParsingException, IOException {
0989:
0990: String input = "<!DOCTYPE doc [\n"
0991: + "<!ATTLIST e3 id ID #IMPLIED>\n"
0992: + "]>\n<?test?>"
0993: + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n"
0994: + " <e1><?test?>\n" + " <e2 xmlns=\"\">\n"
0995: + " <e3 id=\"E3\"/>\n" + " </e2>\n"
0996: + " </e1>\n" + "</doc><?test?>";
0997:
0998: String expected = "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n"
0999: + " <e1>\n"
1000: + " <e2 xmlns=\"\">\n"
1001: + " <e3 id=\"E3\"></e3>\n"
1002: + " </e2>\n"
1003: + " </e1>\n" + "</doc>";
1004:
1005: Document doc = builder.build(input, null);
1006: XPathContext context = new XPathContext("ietf",
1007: "http://www.ietf.org");
1008: String xpath = "//* | //text() | //@* | //namespace::*";
1009:
1010: ByteArrayOutputStream out = new ByteArrayOutputStream();
1011: try {
1012: Canonicalizer serializer = new Canonicalizer(out, false);
1013: serializer.write(doc.query(xpath, context));
1014: } finally {
1015: out.close();
1016: }
1017:
1018: String actual = new String(out.toByteArray(), "UTF-8");
1019: assertEquals(expected, actual);
1020:
1021: }
1022:
1023: public void testDocumentSubsetCanonicalizationWithoutSelectingPrologAndEpilog()
1024: throws ParsingException, IOException {
1025:
1026: String input = "<!-- prolog -->\n"
1027: + "<!DOCTYPE doc [\n"
1028: + "<!ATTLIST e2 xml:space (default|preserve) 'preserve'>\n"
1029: + "<!ATTLIST e3 id ID #IMPLIED>\n"
1030: + "]>\n"
1031: + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n"
1032: + " <e1>\n" + " <e2 xmlns=\"\">\n"
1033: + " <e3 id=\"E3\"/>\n" + " </e2>\n"
1034: + " </e1>\n" + "</doc><!-- epilog -->";
1035:
1036: Document doc = builder.build(input, null);
1037: XPathContext context = new XPathContext("ietf",
1038: "http://www.ietf.org");
1039: String xpath = "(/*//. | //@* | //namespace::*)\n"
1040: + "[\n"
1041: + "self::ietf:e1 or (parent::ietf:e1 and not(self::text() or self::e2))"
1042: + " or\n"
1043: + " count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n"
1044: + "]";
1045:
1046: String expected = "<e1 xmlns=\"http://www.ietf.org\" "
1047: + "xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";
1048:
1049: ByteArrayOutputStream out = new ByteArrayOutputStream();
1050: try {
1051: Canonicalizer serializer = new Canonicalizer(out,
1052: Canonicalizer.CANONICAL_XML_WITH_COMMENTS);
1053: serializer.write(doc.query(xpath, context));
1054: } finally {
1055: out.close();
1056: }
1057:
1058: String actual = new String(out.toByteArray(), "UTF-8");
1059: assertEquals(expected, actual);
1060:
1061: }
1062:
1063: public void testEmptyDefaultNamespace() throws ParsingException,
1064: IOException {
1065:
1066: String input = "<doc xmlns=\"http://www.ietf.org\">"
1067: + "<e2 xmlns=\"\"></e2>" + "</doc>";
1068:
1069: Document doc = builder.build(input, null);
1070: String xpath = "(//* | //namespace::*)";
1071:
1072: String expected = input;
1073:
1074: ByteArrayOutputStream out = new ByteArrayOutputStream();
1075: try {
1076: Canonicalizer serializer = new Canonicalizer(out, false);
1077: serializer.write(doc.query(xpath));
1078: } finally {
1079: out.close();
1080: }
1081:
1082: String actual = new String(out.toByteArray(), "UTF-8");
1083: assertEquals(expected, actual);
1084:
1085: }
1086:
1087: public void testDocumentSubsetCanonicalizationSimple()
1088: throws ParsingException, IOException {
1089:
1090: String input = "<!DOCTYPE doc [\n"
1091: + "<!ATTLIST e2 xml:space (default|preserve) 'preserve'>\n"
1092: + "<!ATTLIST e3 id ID #IMPLIED>\n"
1093: + "]>\n"
1094: + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n"
1095: + " <e1>\n" + " <e2 xmlns=\"\">\n"
1096: + " <e3 id=\"E3\"/>\n" + " </e2>\n"
1097: + " </e1>\n" + "</doc>";
1098:
1099: Document doc = builder.build(input, null);
1100: XPathContext context = new XPathContext("ietf",
1101: "http://www.ietf.org");
1102: String xpath = "(/* | /*/namespace::*)\n";
1103:
1104: String expected = "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"></doc>";
1105:
1106: ByteArrayOutputStream out = new ByteArrayOutputStream();
1107: try {
1108: Canonicalizer serializer = new Canonicalizer(out, false);
1109: serializer.write(doc.query(xpath, context));
1110: } finally {
1111: out.close();
1112: }
1113:
1114: String actual = new String(out.toByteArray(), "UTF-8");
1115: assertEquals(expected, actual);
1116:
1117: }
1118:
1119: public void testCanonicalizeDocumentSubsetIncludingRoot()
1120: throws ParsingException, IOException {
1121:
1122: String input = "<doc />";
1123:
1124: Document doc = builder.build(input, null);
1125:
1126: String expected = "<doc></doc>";
1127:
1128: ByteArrayOutputStream out = new ByteArrayOutputStream();
1129: try {
1130: Canonicalizer serializer = new Canonicalizer(out, false);
1131: Nodes subset = doc.query("//.");
1132: serializer.write(subset);
1133: } finally {
1134: out.close();
1135: }
1136:
1137: String actual = new String(out.toByteArray(), "UTF-8");
1138: assertEquals(expected, actual);
1139:
1140: }
1141:
1142: public void testCanonicalizeDocumentSubsetThatOnlyContainsRoot()
1143: throws IOException {
1144:
1145: Document doc = new Document(new Element("root"));
1146:
1147: ByteArrayOutputStream out = new ByteArrayOutputStream();
1148: try {
1149: Canonicalizer serializer = new Canonicalizer(out, false);
1150: Nodes subset = doc.query("/");
1151: serializer.write(subset);
1152: } finally {
1153: out.close();
1154: }
1155:
1156: String actual = new String(out.toByteArray(), "UTF-8");
1157: assertEquals("", actual);
1158:
1159: }
1160:
1161: public void testDocumentSubsetCanonicalizationNamespaceInheritance()
1162: throws ParsingException, IOException {
1163:
1164: String input = "<!DOCTYPE doc [\n"
1165: + "<!ATTLIST e2 xml:space (default|preserve) 'preserve'>\n"
1166: + "<!ATTLIST e3 id ID #IMPLIED>\n"
1167: + "]>\n"
1168: + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n"
1169: + " <e1>\n" + " <e2 xmlns=\"\">\n"
1170: + " <e3 id=\"E3\"/>\n" + " </e2>\n"
1171: + " </e1>\n" + "</doc>";
1172:
1173: Document doc = builder.build(input, null);
1174: XPathContext context = new XPathContext("ietf",
1175: "http://www.ietf.org");
1176: String xpath = "(/*/* | /*/*/namespace::*)\n";
1177:
1178: String expected = "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"></e1>";
1179:
1180: ByteArrayOutputStream out = new ByteArrayOutputStream();
1181: try {
1182: Canonicalizer serializer = new Canonicalizer(out, false);
1183: serializer.write(doc.query(xpath, context));
1184: } finally {
1185: out.close();
1186: }
1187:
1188: String actual = new String(out.toByteArray(), "UTF-8");
1189: assertEquals(expected, actual);
1190:
1191: }
1192:
1193: public void testRelativeNamespaceURIsForbidden()
1194: throws ParsingException, IOException {
1195:
1196: try {
1197: String data = "<test xmlns=\"relative\">data</test>";
1198: Document doc = builder.build(data, "http://www.ex.org/");
1199: ByteArrayOutputStream out = new ByteArrayOutputStream();
1200: Canonicalizer serializer = new Canonicalizer(out, false);
1201: serializer.write(doc);
1202: fail("Canonicalized document with relative namespace URI");
1203: } catch (ParsingException success) {
1204: assertNotNull(success.getMessage());
1205: }
1206:
1207: }
1208:
1209: private static class XMLFilter implements FilenameFilter {
1210:
1211: public boolean accept(File directory, String name) {
1212: if (name.endsWith(".xml"))
1213: return true;
1214: return false;
1215: }
1216:
1217: }
1218:
1219: public void testNFCFromISO88591() throws ParsingException,
1220: IOException {
1221: isoNormalizationTest("ISO-8859-1");
1222: }
1223:
1224: public void testNFCFromISO88592() throws ParsingException,
1225: IOException {
1226: isoNormalizationTest("ISO-8859-2");
1227: }
1228:
1229: public void testNFCFromISO88593() throws ParsingException,
1230: IOException {
1231: isoNormalizationTest("ISO-8859-3");
1232: }
1233:
1234: public void testNFCFromISO88594() throws ParsingException,
1235: IOException {
1236: isoNormalizationTest("ISO-8859-4");
1237: }
1238:
1239: public void testNFCFromISO88595() throws ParsingException,
1240: IOException {
1241: isoNormalizationTest("ISO-8859-5");
1242: }
1243:
1244: public void testNFCFromISO88596() throws ParsingException,
1245: IOException {
1246:
1247: // This test fails in 1.2.2 due to an apparent bug in the
1248: // conversion of the characters '1' and '0' to bytes in
1249: // ISO-8859-6
1250: isoNormalizationTest("ISO-8859-6");
1251:
1252: }
1253:
1254: public void testNFCFromISO88597() throws ParsingException,
1255: IOException {
1256: isoNormalizationTest("ISO-8859-7");
1257: }
1258:
1259: public void testNFCFromISO88598() throws ParsingException,
1260: IOException {
1261: isoNormalizationTest("ISO-8859-8");
1262: }
1263:
1264: public void testNFCFromISO88599() throws ParsingException,
1265: IOException {
1266: isoNormalizationTest("ISO-8859-9");
1267: }
1268:
1269: public void testNFCFromISO885913() throws ParsingException,
1270: IOException {
1271:
1272: if (version >= 1.3) {
1273: // iSO-8859-6 not supported in Java 1.2
1274: isoNormalizationTest("ISO-8859-13");
1275: }
1276:
1277: }
1278:
1279: public void testNFCFromISO885915() throws ParsingException,
1280: IOException {
1281: isoNormalizationTest("ISO-8859-15");
1282: }
1283:
1284: // 14 and 16 aren't tested because Java doesn't support them yet
1285: private void isoNormalizationTest(String encoding)
1286: throws ParsingException, IOException {
1287:
1288: String prolog = "<?xml version='1.0' encoding='" + encoding
1289: + "'?>\r\n<root>";
1290:
1291: byte[] prologData = prolog.getBytes(encoding);
1292:
1293: String epilog = "</root>";
1294: byte[] epilogData = epilog.getBytes(encoding);
1295: byte[] data = new byte[prologData.length + epilogData.length
1296: + 255 - 160 + 1];
1297: System.arraycopy(prologData, 0, data, 0, prologData.length);
1298: System.arraycopy(epilogData, 0, data, data.length
1299: - epilogData.length, epilogData.length);
1300: for (int i = 160; i <= 255; i++) {
1301: data[prologData.length + (i - 160)] = (byte) i;
1302: }
1303: InputStream in = new ByteArrayInputStream(data);
1304: Document doc = builder.build(in);
1305:
1306: // make a Unicode normalized version of the same document
1307: ByteArrayOutputStream out = new ByteArrayOutputStream();
1308: Serializer serializer = new Serializer(out);
1309: serializer.setUnicodeNormalizationFormC(true);
1310: serializer.write(doc);
1311: byte[] temp = out.toByteArray();
1312: in = new ByteArrayInputStream(temp);
1313: Document nfcDoc = builder.build(in);
1314:
1315: assertEquals("Parser doesn't use NFC when converting from "
1316: + encoding, doc, nfcDoc);
1317:
1318: }
1319:
1320: public void testEBCDIC() throws ParsingException, IOException {
1321:
1322: String encoding = "IBM037";
1323: String prolog = "<?xml version='1.0' encoding='" + encoding
1324: + "'?>\r\n<root>";
1325: byte[] prologData = prolog.getBytes(encoding);
1326: String epilog = "</root>";
1327: byte[] epilogData = epilog.getBytes(encoding);
1328: byte[] data = new byte[prologData.length + epilogData.length
1329: + 255 - 160 + 1];
1330: System.arraycopy(prologData, 0, data, 0, prologData.length);
1331: System.arraycopy(epilogData, 0, data, data.length
1332: - epilogData.length, epilogData.length);
1333: StringBuffer buffer = new StringBuffer(255 - 160 + 1);
1334: for (int i = 160; i <= 255; i++) {
1335: buffer.append((char) i);
1336: }
1337: byte[] temp = buffer.toString().getBytes(encoding);
1338: System.arraycopy(temp, 0, data, prologData.length, temp.length);
1339: InputStream in = new ByteArrayInputStream(data);
1340: Document doc = builder.build(in);
1341:
1342: // make a Unicode normalized version of the same document
1343: ByteArrayOutputStream out = new ByteArrayOutputStream();
1344: Serializer serializer = new Serializer(out);
1345: serializer.setUnicodeNormalizationFormC(true);
1346: serializer.write(doc);
1347: temp = out.toByteArray();
1348: in = new ByteArrayInputStream(temp);
1349: Document nfcDoc = builder.build(in);
1350:
1351: // String normalizedResult = Normalizer.normalize(rawResult, Normalizer.NFC);
1352: assertEquals("Parser doesn't use NFC when converting from "
1353: + encoding, doc, nfcDoc);
1354:
1355: }
1356:
1357: // make sure null pointer exception doesn't cause any output
1358: public void testNullDocument() throws IOException {
1359:
1360: ByteArrayOutputStream out = new ByteArrayOutputStream();
1361: Canonicalizer canonicalizer = new Canonicalizer(out);
1362: try {
1363: canonicalizer.write((Document) null);
1364: fail("Wrote null document");
1365: } catch (NullPointerException success) {
1366: // success
1367: }
1368: byte[] result = out.toByteArray();
1369: assertEquals(0, result.length);
1370:
1371: }
1372:
1373: public void testWhiteSpaceTrimmingInNonCDATAAttribute()
1374: throws IOException {
1375:
1376: Attribute attribute = new Attribute("name",
1377: " value1 value2 ");
1378: attribute.setType(Attribute.Type.NMTOKENS);
1379: Element root = new Element("root");
1380: root.addAttribute(attribute);
1381: Document doc = new Document(root);
1382: ByteArrayOutputStream out = new ByteArrayOutputStream();
1383: Canonicalizer canonicalizer = new Canonicalizer(out);
1384: canonicalizer.write(doc);
1385: out.close();
1386: String result = new String(out.toByteArray(), "UTF8");
1387: assertEquals("<root name=\"value1 value2\"></root>", result);
1388:
1389: }
1390:
1391: // compare to output generated by Apache XML Security code
1392: public void testXMLConformanceTestSuiteDocuments()
1393: throws ParsingException, IOException {
1394:
1395: File masterList = new File(canonical, "xmlconf");
1396: masterList = new File(masterList, "xmlconf.xml");
1397: if (masterList.exists()) {
1398: Document xmlconf = builder.build(masterList);
1399: Elements testcases = xmlconf.getRootElement()
1400: .getChildElements("TESTCASES");
1401: processTestCases(testcases);
1402: }
1403:
1404: }
1405:
1406: // xmlconf/xmltest/valid/sa/097.xml appears to be screwed up by a lot
1407: // of parsers
1408: private void processTestCases(Elements testcases)
1409: throws ParsingException, IOException {
1410:
1411: for (int i = 0; i < testcases.size(); i++) {
1412: Element testcase = testcases.get(i);
1413: Elements tests = testcase.getChildElements("TEST");
1414: processTests(tests);
1415: Elements level2 = testcase.getChildElements("TESTCASES");
1416: // need to be recursive to handle recursive IBM test cases
1417: processTestCases(level2);
1418: }
1419:
1420: }
1421:
1422: private void processTests(Elements tests) throws ParsingException,
1423: IOException {
1424:
1425: Element parent = new Element("e");
1426: Element child = new Element("a");
1427: parent.appendChild(child);
1428:
1429: int size = tests.size();
1430: for (int i = 0; i < size; i++) {
1431: Element test = tests.get(i);
1432: String namespace = test.getAttributeValue("NAMESPACE");
1433: if ("no".equals(namespace))
1434: continue;
1435: String type = test.getAttributeValue("TYPE");
1436: if ("not-wf".equals(type))
1437: continue;
1438: String uri = test.getAttributeValue("URI");
1439: String base = test.getBaseURI();
1440: // Hack because URIUtil isn't public; and I don't want to
1441: // depend on 1.4 only java.net.URI
1442: parent.setBaseURI(base);
1443:
1444: child.addAttribute(new Attribute("xml:base",
1445: "http://www.w3.org/XML/1998/namespace", uri));
1446: String resolvedURI = child.getBaseURI();
1447:
1448: Document doc = builder.build(resolvedURI);
1449: ByteArrayOutputStream out = new ByteArrayOutputStream();
1450: try {
1451: Canonicalizer serializer = new Canonicalizer(out);
1452: serializer.write(doc);
1453: } finally {
1454: out.close();
1455: }
1456: byte[] actual = out.toByteArray();
1457:
1458: File input = new File(resolvedURI.substring(5) + ".can");
1459: assertEquals(resolvedURI, input.length(), actual.length);
1460: byte[] expected = new byte[actual.length];
1461: DataInputStream in = new DataInputStream(
1462: new BufferedInputStream(new FileInputStream(input)));
1463: try {
1464: in.readFully(expected);
1465: } finally {
1466: in.close();
1467: }
1468: assertEquals(expected, actual);
1469:
1470: }
1471:
1472: }
1473:
1474: public void testExclusive() throws IOException {
1475:
1476: Element pdu = new Element("n0:pdu", "http://a.example");
1477: Element elem1 = new Element("n1:elem1", "http://b.example");
1478: elem1.appendChild("content");
1479: pdu.appendChild(elem1);
1480:
1481: String expected = "<n1:elem1 xmlns:n1=\"http://b.example\">content</n1:elem1>";
1482: ByteArrayOutputStream out = new ByteArrayOutputStream();
1483: Canonicalizer canonicalizer = new Canonicalizer(
1484: out,
1485: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
1486:
1487: XPathContext context = new XPathContext("n1",
1488: "http://b.example");
1489: Document doc = new Document(pdu);
1490: canonicalizer
1491: .write(doc
1492: .query(
1493: "(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]",
1494: context));
1495:
1496: byte[] result = out.toByteArray();
1497: out.close();
1498: String s = new String(out.toByteArray(), "UTF8");
1499: assertEquals(expected, s);
1500:
1501: }
1502:
1503: public static void testAustB() throws IOException {
1504:
1505: Element e1 = new Element("a:a", "urn:a");
1506: Element e2 = new Element("b");
1507: e1.appendChild(e2);
1508:
1509: ByteArrayOutputStream out = new ByteArrayOutputStream();
1510: Canonicalizer c = new Canonicalizer(out,
1511: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
1512: c.write(e1);
1513: String s = out.toString("UTF8");
1514: assertEquals("<a:a xmlns:a=\"urn:a\"><b></b></a:a>", s);
1515:
1516: }
1517:
1518: public static void testAustB2() throws IOException {
1519:
1520: Element e1 = new Element("a:a", "urn:a");
1521: Element e2 = new Element("b");
1522: e1.appendChild(e2);
1523:
1524: ByteArrayOutputStream out = new ByteArrayOutputStream();
1525: Canonicalizer c = new Canonicalizer(out);
1526: c.write(e1);
1527: String s = out.toString("UTF8");
1528: assertEquals("<a:a xmlns:a=\"urn:a\"><b></b></a:a>", s);
1529:
1530: }
1531:
1532: public static void testAust() throws Exception {
1533:
1534: Element e1 = new Element("a:a", "urn:a");
1535: Element e2 = new Element("a:b", "urn:a");
1536: e1.appendChild(e2);
1537: ByteArrayOutputStream out = new ByteArrayOutputStream();
1538: Canonicalizer c = new Canonicalizer(out,
1539: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
1540: c.write(e2);
1541: String s = out.toString("UTF8");
1542: assertEquals("<a:b xmlns:a=\"urn:a\"></a:b>", s);
1543:
1544: }
1545:
1546: public static void testAust4() throws Exception {
1547:
1548: Element e1 = new Element("a:a", "urn:a");
1549: Element e2 = new Element("a:b", "urn:a");
1550: e1.appendChild(e2);
1551: ByteArrayOutputStream out = new ByteArrayOutputStream();
1552: Canonicalizer canonicalizer = new Canonicalizer(
1553: out,
1554: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
1555: XPathContext context = new XPathContext("a", "urn:a");
1556: Document doc = new Document(e1);
1557: canonicalizer.write(doc.query(
1558: "(//. | //@* | //namespace::*)[ancestor-or-self::a:b]",
1559: context));
1560:
1561: String s = out.toString("UTF8");
1562: assertEquals("<a:b xmlns:a=\"urn:a\"></a:b>", s);
1563:
1564: }
1565:
1566: public static void testAust5() throws Exception {
1567:
1568: Element e1 = new Element("a:a", "urn:a");
1569: Element e2 = new Element("a:b", "urn:a");
1570: e1.appendChild(e2);
1571: ByteArrayOutputStream out = new ByteArrayOutputStream();
1572: Canonicalizer canonicalizer = new Canonicalizer(
1573: out,
1574: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
1575: Document doc = new Document(e1);
1576: Nodes set = new Nodes(e2);
1577: canonicalizer.write(set);
1578:
1579: String s = out.toString("UTF8");
1580: // The namespace was not explicitly included in
1581: // the set so it should not be output.
1582: assertEquals("<a:b></a:b>", s);
1583:
1584: }
1585:
1586: public static void testAust3() throws Exception {
1587:
1588: Element e2 = new Element("a:b", "urn:a");
1589: ByteArrayOutputStream os = new ByteArrayOutputStream();
1590: new Canonicalizer(os,
1591: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION).write(e2);
1592: String s = os.toString("UTF8");
1593: assertEquals("<a:b xmlns:a=\"urn:a\"></a:b>", s);
1594:
1595: }
1596:
1597: public static void testAust2() throws Exception {
1598:
1599: Element e1 = new Element("a:a", "urn:a");
1600: Element e2 = new Element("a:b", "urn:a");
1601: e1.appendChild(e2);
1602: ByteArrayOutputStream os = new ByteArrayOutputStream();
1603: new Canonicalizer(os, Canonicalizer.CANONICAL_XML).write(e2);
1604: String s = os.toString("UTF8");
1605: assertEquals("<a:b xmlns:a=\"urn:a\"></a:b>", s);
1606:
1607: }
1608:
1609: public void testExclusiveWithNamespacedAttributes()
1610: throws IOException {
1611:
1612: Element pdu = new Element("n0:pdu", "http://a.example");
1613: Element elem1 = new Element("n1:elem1", "http://b.example");
1614: elem1.appendChild("content");
1615: pdu.appendChild(elem1);
1616: elem1.addAttribute(new Attribute("pre:foo",
1617: "http://www.example.org/", "value"));
1618:
1619: String expected = "<n1:elem1 xmlns:n1=\"http://b.example\" "
1620: + "xmlns:pre=\"http://www.example.org/\" pre:foo=\"value\">content</n1:elem1>";
1621: ByteArrayOutputStream out = new ByteArrayOutputStream();
1622: Canonicalizer canonicalizer = new Canonicalizer(
1623: out,
1624: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
1625:
1626: XPathContext context = new XPathContext("n1",
1627: "http://b.example");
1628: Document doc = new Document(pdu);
1629: canonicalizer
1630: .write(doc
1631: .query(
1632: "(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]",
1633: context));
1634:
1635: byte[] result = out.toByteArray();
1636: out.close();
1637: String s = new String(out.toByteArray(), "UTF8");
1638: assertEquals(expected, s);
1639:
1640: }
1641:
1642: /* <root xml:lang="en"><a><b>test</b></a></root>
1643:
1644: Choose the document subset selected by /root//node()
1645:
1646: and expect to see
1647:
1648: <a xml:lang="en"><b>test</b></a> */
1649: public void testInheritanceOfXMLLang() throws IOException {
1650:
1651: Element root = new Element("root");
1652: root.addAttribute(new Attribute("xml:lang",
1653: Namespace.XML_NAMESPACE, "en"));
1654: Element a = new Element("a");
1655: Element b = new Element("b");
1656: b.appendChild("test");
1657: a.appendChild(b);
1658: root.appendChild(a);
1659:
1660: String expected = "<a xml:lang=\"en\"><b>test</b></a>";
1661: ByteArrayOutputStream out = new ByteArrayOutputStream();
1662: Canonicalizer canonicalizer = new Canonicalizer(out,
1663: Canonicalizer.CANONICAL_XML);
1664:
1665: Document doc = new Document(root);
1666: canonicalizer.write(doc.query("/root//node()"));
1667:
1668: byte[] result = out.toByteArray();
1669: out.close();
1670: String s = new String(out.toByteArray(), "UTF8");
1671: assertEquals(expected, s);
1672:
1673: }
1674:
1675: public void testExclusiveWithInclusiveNamespaces()
1676: throws IOException {
1677:
1678: Element pdu = new Element("n0:pdu", "http://a.example");
1679: Element elem1 = new Element("n1:elem1", "http://b.example");
1680: elem1.appendChild("content");
1681: pdu.appendChild(elem1);
1682:
1683: String expected = "<n1:elem1 xmlns:n0=\"http://a.example\""
1684: + " xmlns:n1=\"http://b.example\">content</n1:elem1>";
1685: ByteArrayOutputStream out = new ByteArrayOutputStream();
1686: Canonicalizer canonicalizer = new Canonicalizer(
1687: out,
1688: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
1689:
1690: XPathContext context = new XPathContext("n1",
1691: "http://b.example");
1692: Document doc = new Document(pdu);
1693: canonicalizer.setInclusiveNamespacePrefixList("n0");
1694: Nodes subset = doc
1695: .query(
1696: "(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]",
1697: context);
1698: canonicalizer.write(subset);
1699:
1700: byte[] result = out.toByteArray();
1701: out.close();
1702: String s = new String(out.toByteArray(), "UTF8");
1703: assertEquals(expected, s);
1704:
1705: }
1706:
1707: public void testClearInclusiveNamespacePrefixes()
1708: throws IOException {
1709:
1710: Element pdu = new Element("n0:pdu", "http://a.example");
1711: Element elem1 = new Element("n1:elem1", "http://b.example");
1712: elem1.appendChild("content");
1713: pdu.appendChild(elem1);
1714:
1715: String expected = "<n1:elem1"
1716: + " xmlns:n1=\"http://b.example\">content</n1:elem1>";
1717: ByteArrayOutputStream out = new ByteArrayOutputStream();
1718: Canonicalizer canonicalizer = new Canonicalizer(
1719: out,
1720: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
1721:
1722: XPathContext context = new XPathContext("n1",
1723: "http://b.example");
1724: Document doc = new Document(pdu);
1725: canonicalizer.setInclusiveNamespacePrefixList("n0");
1726: canonicalizer.setInclusiveNamespacePrefixList(null);
1727: Nodes subset = doc
1728: .query(
1729: "(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]",
1730: context);
1731: canonicalizer.write(subset);
1732:
1733: byte[] result = out.toByteArray();
1734: out.close();
1735: String s = new String(out.toByteArray(), "UTF8");
1736: assertEquals(expected, s);
1737:
1738: }
1739:
1740: public void testExclusive22a() throws ParsingException, IOException {
1741:
1742: Builder builder = new Builder();
1743: String input = "<n0:local xmlns:n0='foo:bar' xmlns:n3='ftp://example.org'>"
1744: + "<n1:elem2 xmlns:n1=\"http://example.net\" xml:lang=\"en\">"
1745: + "<n3:stuff xmlns:n3=\"ftp://example.org\"/></n1:elem2></n0:local>";
1746: Document doc = builder.build(input, null);
1747:
1748: String expected = "<n1:elem2 xmlns:n1=\"http://example.net\" xml:lang=\"en\">"
1749: + "<n3:stuff xmlns:n3=\"ftp://example.org\"></n3:stuff></n1:elem2>";
1750: ByteArrayOutputStream out = new ByteArrayOutputStream();
1751: Canonicalizer canonicalizer = new Canonicalizer(
1752: out,
1753: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
1754:
1755: XPathContext context = new XPathContext("n1",
1756: "http://example.net");
1757: canonicalizer
1758: .write(doc
1759: .query(
1760: " (//. | //@* | //namespace::*)[ancestor-or-self::n1:elem2]",
1761: context));
1762:
1763: byte[] result = out.toByteArray();
1764: out.close();
1765: String s = new String(out.toByteArray(), "UTF8");
1766: assertEquals(expected, s);
1767:
1768: }
1769:
1770: public void testExclusive22b() throws ParsingException, IOException {
1771:
1772: Builder builder = new Builder();
1773: String input = "<n2:pdu xmlns:n1='http://example.com' "
1774: + "xmlns:n2='http://foo.example' xml:lang='fr' xml:space='retain'>"
1775: + "<n1:elem2 xmlns:n1='http://example.net' xml:lang='en'>"
1776: + "<n3:stuff xmlns:n3='ftp://example.org'/></n1:elem2></n2:pdu>";
1777: Document doc = builder.build(input, null);
1778:
1779: String expected = "<n1:elem2 xmlns:n1=\"http://example.net\" xml:lang=\"en\">"
1780: + "<n3:stuff xmlns:n3=\"ftp://example.org\"></n3:stuff></n1:elem2>";
1781: ByteArrayOutputStream out = new ByteArrayOutputStream();
1782: Canonicalizer canonicalizer = new Canonicalizer(
1783: out,
1784: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
1785:
1786: XPathContext context = new XPathContext("n1",
1787: "http://example.net");
1788: canonicalizer
1789: .write(doc
1790: .query(
1791: " (//. | //@* | //namespace::*)[ancestor-or-self::n1:elem2]",
1792: context));
1793:
1794: byte[] result = out.toByteArray();
1795: out.close();
1796: String s = new String(out.toByteArray(), "UTF8");
1797: assertEquals(expected, s);
1798:
1799: }
1800:
1801: // compare to output generated by Apache XML Security code
1802: public void testExclusiveXMLConformanceTestSuiteDocuments()
1803: throws ParsingException, IOException {
1804:
1805: File masterList = new File(canonical, "xmlconf");
1806: masterList = new File(masterList, "xmlconf.xml");
1807: if (masterList.exists()) {
1808: Document xmlconf = builder.build(masterList);
1809: Elements testcases = xmlconf.getRootElement()
1810: .getChildElements("TESTCASES");
1811: processExclusiveTestCases(testcases);
1812: }
1813:
1814: }
1815:
1816: // xmlconf/xmltest/valid/sa/097.xml appears to be screwed up by a lot
1817: // of parsers
1818: private void processExclusiveTestCases(Elements testcases)
1819: throws ParsingException, IOException {
1820:
1821: for (int i = 0; i < testcases.size(); i++) {
1822: Element testcase = testcases.get(i);
1823: Elements tests = testcase.getChildElements("TEST");
1824: processExclusiveTests(tests);
1825: Elements level2 = testcase.getChildElements("TESTCASES");
1826: // need to be recursive to handle recursive IBM test cases
1827: processExclusiveTestCases(level2);
1828: }
1829:
1830: }
1831:
1832: private void processExclusiveTests(Elements tests)
1833: throws ParsingException, IOException {
1834:
1835: for (int i = 0; i < tests.size(); i++) {
1836: Element test = tests.get(i);
1837: String namespace = test.getAttributeValue("NAMESPACE");
1838: if ("no".equals(namespace))
1839: continue;
1840: String type = test.getAttributeValue("TYPE");
1841: if ("not-wf".equals(type))
1842: continue;
1843: String uri = test.getAttributeValue("URI");
1844: String base = test.getBaseURI();
1845: // Hack because URIUtil isn't public; and I don't want to
1846: // depend on 1.4 only java.net.URI
1847: Element parent = new Element("e");
1848: parent.setBaseURI(base);
1849: Element child = new Element("a");
1850: child.addAttribute(new Attribute("xml:base",
1851: "http://www.w3.org/XML/1998/namespace", uri));
1852: parent.appendChild(child);
1853: String resolvedURI = child.getBaseURI();
1854:
1855: Document doc = builder.build(resolvedURI);
1856: ByteArrayOutputStream out = new ByteArrayOutputStream();
1857: try {
1858: Canonicalizer serializer = new Canonicalizer(
1859: out,
1860: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
1861: serializer.write(doc);
1862: } finally {
1863: out.close();
1864: }
1865: byte[] actual = out.toByteArray();
1866:
1867: File input = new File(resolvedURI.substring(5) + ".exc");
1868: byte[] expected = new byte[(int) input.length()];
1869: DataInputStream in = new DataInputStream(
1870: new BufferedInputStream(new FileInputStream(input)));
1871: try {
1872: in.readFully(expected);
1873: } finally {
1874: in.close();
1875: }
1876:
1877: assertEquals(resolvedURI, new String(expected, "UTF-8"),
1878: new String(actual, "UTF-8"));
1879:
1880: }
1881:
1882: }
1883:
1884: public void testCanonicalizeAttribute() throws IOException {
1885:
1886: Attribute att = new Attribute("pre:foo",
1887: "http://www.example.org", "value");
1888: ByteArrayOutputStream out = new ByteArrayOutputStream();
1889: try {
1890: Canonicalizer serializer = new Canonicalizer(out);
1891: serializer.write(att);
1892: } finally {
1893: out.close();
1894: }
1895: byte[] actual = out.toByteArray();
1896: byte[] expected = " pre:foo=\"value\"".getBytes("UTF-8");
1897: assertEquals(expected, actual);
1898:
1899: }
1900:
1901: public void testCanonicalizeNamespace() throws IOException {
1902:
1903: Element element = new Element("pre:foo",
1904: "http://www.example.org");
1905: Nodes namespaces = element.query("namespace::pre");
1906: Namespace ns = (Namespace) namespaces.get(0);
1907: ByteArrayOutputStream out = new ByteArrayOutputStream();
1908: try {
1909: Canonicalizer serializer = new Canonicalizer(out);
1910: serializer.write(ns);
1911: } finally {
1912: out.close();
1913: }
1914: byte[] actual = out.toByteArray();
1915: byte[] expected = " xmlns:pre=\"http://www.example.org\""
1916: .getBytes("UTF-8");
1917: assertEquals(expected, actual);
1918:
1919: }
1920:
1921: public void testCanonicalizeDefaultNamespace() throws IOException {
1922:
1923: Element element = new Element("foo", "http://www.example.org");
1924: Nodes namespaces = element.query("namespace::*");
1925: Namespace ns = (Namespace) namespaces.get(0);
1926: if (ns.getPrefix().equals("xml"))
1927: ns = (Namespace) namespaces.get(1);
1928: ByteArrayOutputStream out = new ByteArrayOutputStream();
1929: try {
1930: Canonicalizer serializer = new Canonicalizer(out);
1931: serializer.write(ns);
1932: } finally {
1933: out.close();
1934: }
1935: byte[] actual = out.toByteArray();
1936: byte[] expected = " xmlns=\"http://www.example.org\""
1937: .getBytes("UTF-8");
1938: assertEquals(expected, actual);
1939:
1940: }
1941:
1942: public void testCanonicalizeXMLNamespace() throws IOException {
1943:
1944: Element element = new Element("foo");
1945: Nodes namespaces = element.query("namespace::*");
1946: Namespace ns = (Namespace) namespaces.get(0);
1947: ByteArrayOutputStream out = new ByteArrayOutputStream();
1948: try {
1949: Canonicalizer serializer = new Canonicalizer(out);
1950: serializer.write(ns);
1951: } finally {
1952: out.close();
1953: }
1954: byte[] actual = out.toByteArray();
1955: byte[] expected = " xmlns:xml=\"http://www.w3.org/XML/1998/namespace\""
1956: .getBytes("UTF-8");
1957: assertEquals(expected, actual);
1958:
1959: }
1960:
1961: public void testCanonicalizeElement() throws IOException {
1962:
1963: Element element = new Element("pre:foo",
1964: "http://www.example.org");
1965: element.appendChild(" value \n value");
1966: ByteArrayOutputStream out = new ByteArrayOutputStream();
1967: try {
1968: Canonicalizer serializer = new Canonicalizer(out);
1969: serializer.write(element);
1970: } finally {
1971: out.close();
1972: }
1973: byte[] actual = out.toByteArray();
1974: byte[] expected = "<pre:foo xmlns:pre=\"http://www.example.org\"> value \n value</pre:foo>"
1975: .getBytes("UTF-8");
1976: assertEquals(expected, actual);
1977:
1978: }
1979:
1980: public void testDontPutElementInDocument() throws IOException {
1981:
1982: Element element = new Element("pre:foo",
1983: "http://www.example.org");
1984: assertNull(element.getDocument());
1985: ByteArrayOutputStream out = new ByteArrayOutputStream();
1986: try {
1987: Canonicalizer serializer = new Canonicalizer(out);
1988: serializer.write(element);
1989: assertNull(element.getDocument());
1990: } finally {
1991: out.close();
1992: }
1993:
1994: }
1995:
1996: public void testCanonicalizeElementInDocument() throws IOException {
1997:
1998: Element root = new Element("root");
1999: Document doc = new Document(root);
2000: Element element = new Element("pre:foo",
2001: "http://www.example.org");
2002: root.appendChild(element);
2003: element.appendChild(" value \n value");
2004: ByteArrayOutputStream out = new ByteArrayOutputStream();
2005: try {
2006: Canonicalizer serializer = new Canonicalizer(out);
2007: serializer.write(element);
2008: } finally {
2009: out.close();
2010: }
2011: byte[] actual = out.toByteArray();
2012: byte[] expected = "<pre:foo xmlns:pre=\"http://www.example.org\"> value \n value</pre:foo>"
2013: .getBytes("UTF-8");
2014: assertEquals(expected, actual);
2015:
2016: }
2017:
2018: public void testNoInfiniteLoopWhenWritingADocumentlessElement()
2019: throws IOException {
2020:
2021: Element root = new Element("root");
2022: Element a = new Element("a");
2023: root.appendChild(a);
2024: Element b = new Element("b");
2025: a.appendChild(b);
2026:
2027: ByteArrayOutputStream out = new ByteArrayOutputStream();
2028: try {
2029: Canonicalizer serializer = new Canonicalizer(out);
2030: serializer.write(b);
2031: } finally {
2032: out.close();
2033: }
2034: byte[] actual = out.toByteArray();
2035: byte[] expected = "<b></b>".getBytes("UTF-8");
2036: assertEquals(expected, actual);
2037:
2038: }
2039:
2040: public void testExclusiveCanonicalizeElementInDocument()
2041: throws IOException {
2042:
2043: Element root = new Element("root");
2044: Document doc = new Document(root);
2045: Element element = new Element("pre:foo",
2046: "http://www.example.org");
2047: root.appendChild(element);
2048: element.appendChild(" value \n value");
2049: ByteArrayOutputStream out = new ByteArrayOutputStream();
2050: try {
2051: Canonicalizer serializer = new Canonicalizer(out,
2052: Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
2053: serializer.write(element);
2054: } finally {
2055: out.close();
2056: }
2057: byte[] actual = out.toByteArray();
2058: byte[] expected = "<pre:foo xmlns:pre=\"http://www.example.org\"> value \n value</pre:foo>"
2059: .getBytes("UTF-8");
2060: assertEquals(expected, actual);
2061:
2062: }
2063:
2064: public void testCanonicalizeDocumentTypeDeclaration()
2065: throws IOException {
2066:
2067: DocType doctype = new DocType("root", "http://www.example.org");
2068: ByteArrayOutputStream out = new ByteArrayOutputStream();
2069: try {
2070: Canonicalizer serializer = new Canonicalizer(out);
2071: serializer.write(doctype);
2072: } finally {
2073: out.close();
2074: }
2075: byte[] actual = out.toByteArray();
2076: assertEquals(0, actual.length);
2077:
2078: }
2079:
2080: public void testCanonicalizeProcessingInstruction()
2081: throws IOException {
2082:
2083: ProcessingInstruction pi = new ProcessingInstruction("target",
2084: "value \n value");
2085: ByteArrayOutputStream out = new ByteArrayOutputStream();
2086: try {
2087: Canonicalizer serializer = new Canonicalizer(out);
2088: serializer.write(pi);
2089: } finally {
2090: out.close();
2091: }
2092: byte[] actual = out.toByteArray();
2093: byte[] expected = "<?target value \n value?>".getBytes("UTF-8");
2094: assertEquals(expected, actual);
2095:
2096: }
2097:
2098: public void testCanonicalizeText() throws IOException {
2099:
2100: Text c = new Text(" pre:foo \n ");
2101: ByteArrayOutputStream out = new ByteArrayOutputStream();
2102: try {
2103: Canonicalizer serializer = new Canonicalizer(out);
2104: serializer.write(c);
2105: } finally {
2106: out.close();
2107: }
2108: byte[] actual = out.toByteArray();
2109: byte[] expected = " pre:foo \n ".getBytes("UTF-8");
2110: assertEquals(expected, actual);
2111:
2112: }
2113:
2114: public void testCanonicalizeComment() throws IOException {
2115:
2116: Comment c = new Comment("pre:foo");
2117: ByteArrayOutputStream out = new ByteArrayOutputStream();
2118: try {
2119: Canonicalizer serializer = new Canonicalizer(out);
2120: serializer.write(c);
2121: } finally {
2122: out.close();
2123: }
2124: byte[] actual = out.toByteArray();
2125: byte[] expected = "<!--pre:foo-->".getBytes("UTF-8");
2126: assertEquals(expected, actual);
2127:
2128: }
2129:
2130: public void testUnsupportedAlgorithm() throws IOException {
2131:
2132: ByteArrayOutputStream out = new ByteArrayOutputStream();
2133: try {
2134: new Canonicalizer(out, "http://www.example.org/canonical");
2135: fail("Allowed unrecognized algorithm");
2136: } catch (CanonicalizationException success) {
2137: assertNotNull(success.getMessage());
2138: } finally {
2139: out.close();
2140: }
2141:
2142: }
2143:
2144: public void testCanonicalizeDetachedNodes() throws IOException {
2145:
2146: ByteArrayOutputStream out = new ByteArrayOutputStream();
2147: Element e = new Element("test");
2148: Nodes nodes = new Nodes(e);
2149: Canonicalizer serializer = new Canonicalizer(out);
2150: try {
2151: serializer.write(nodes);
2152: fail("Canonicalized detached node");
2153: } catch (CanonicalizationException success) {
2154: assertNotNull(success.getMessage());
2155: } finally {
2156: out.close();
2157: }
2158:
2159: }
2160:
2161: public void testCanonicalizeNodesFromTwoDocuments()
2162: throws IOException {
2163:
2164: ByteArrayOutputStream out = new ByteArrayOutputStream();
2165: Element e1 = new Element("test");
2166: new Document(e1);
2167: Element e2 = new Element("test");
2168: new Document(e2);
2169: Nodes nodes = new Nodes(e1);
2170: nodes.append(e2);
2171: Canonicalizer serializer = new Canonicalizer(out);
2172: try {
2173: serializer.write(nodes);
2174: fail("Canonicalized multiple document nodes");
2175: } catch (CanonicalizationException success) {
2176: assertNotNull(success.getMessage());
2177: } finally {
2178: out.close();
2179: }
2180:
2181: }
2182:
2183: /**
2184: * <p>
2185: * Asserts that two byte arrays are equal. If the two arrays are
2186: * not equal a <code>ComparisonFailure</code> is thrown. Two
2187: * arrays are equal if and only if they have the same length,
2188: * and each item in the expected array is equal to the
2189: * corresponding item in the actual array.
2190: * </p>
2191: *
2192: * @param expected the byte array the test should produce
2193: * @param actual the byte array the test does produce
2194: */
2195: private void assertEquals(byte[] expected, byte[] actual) {
2196:
2197: if (expected == null && actual == null) {
2198: return;
2199: }
2200: // what if one is null and the other isn't????
2201: assertEquals(expected.length, actual.length);
2202: for (int i = 0; i < actual.length; i++) {
2203: assertEquals(expected[i], actual[i]);
2204: }
2205:
2206: }
2207:
2208: // This class forces IOExceptions when writing
2209: private static class UnwriteableOutputStream extends OutputStream {
2210:
2211: public void write(int b) throws IOException {
2212: throw new IOException();
2213: }
2214:
2215: }
2216:
2217: public void testDetachElementWhenExceptionIsThrown() {
2218:
2219: Element e = new Element("a");
2220: Canonicalizer canonicalizer = new Canonicalizer(
2221: new UnwriteableOutputStream());
2222: try {
2223: canonicalizer.write(e);
2224: } catch (IOException ex) {
2225: }
2226: assertNull(e.getParent());
2227:
2228: }
2229:
2230: }
|