nokogiri 1.11.0.rc3-java → 1.11.4-java

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (187) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -0
  3. data/LICENSE-DEPENDENCIES.md +1015 -947
  4. data/LICENSE.md +1 -1
  5. data/README.md +168 -91
  6. data/dependencies.yml +12 -12
  7. data/ext/java/nokogiri/EncodingHandler.java +76 -89
  8. data/ext/java/nokogiri/HtmlDocument.java +135 -144
  9. data/ext/java/nokogiri/HtmlElementDescription.java +102 -117
  10. data/ext/java/nokogiri/HtmlEntityLookup.java +33 -60
  11. data/ext/java/nokogiri/HtmlSaxParserContext.java +218 -222
  12. data/ext/java/nokogiri/HtmlSaxPushParser.java +162 -169
  13. data/ext/java/nokogiri/NokogiriService.java +595 -556
  14. data/ext/java/nokogiri/XmlAttr.java +118 -126
  15. data/ext/java/nokogiri/XmlAttributeDecl.java +95 -106
  16. data/ext/java/nokogiri/XmlCdata.java +35 -58
  17. data/ext/java/nokogiri/XmlComment.java +46 -67
  18. data/ext/java/nokogiri/XmlDocument.java +645 -572
  19. data/ext/java/nokogiri/XmlDocumentFragment.java +125 -137
  20. data/ext/java/nokogiri/XmlDtd.java +448 -414
  21. data/ext/java/nokogiri/XmlElement.java +23 -48
  22. data/ext/java/nokogiri/XmlElementContent.java +343 -316
  23. data/ext/java/nokogiri/XmlElementDecl.java +124 -125
  24. data/ext/java/nokogiri/XmlEntityDecl.java +119 -127
  25. data/ext/java/nokogiri/XmlEntityReference.java +49 -72
  26. data/ext/java/nokogiri/XmlNamespace.java +175 -175
  27. data/ext/java/nokogiri/XmlNode.java +1843 -1622
  28. data/ext/java/nokogiri/XmlNodeSet.java +361 -331
  29. data/ext/java/nokogiri/XmlProcessingInstruction.java +47 -69
  30. data/ext/java/nokogiri/XmlReader.java +513 -450
  31. data/ext/java/nokogiri/XmlRelaxng.java +89 -101
  32. data/ext/java/nokogiri/XmlSaxParserContext.java +328 -310
  33. data/ext/java/nokogiri/XmlSaxPushParser.java +227 -220
  34. data/ext/java/nokogiri/XmlSchema.java +335 -242
  35. data/ext/java/nokogiri/XmlSyntaxError.java +113 -119
  36. data/ext/java/nokogiri/XmlText.java +55 -76
  37. data/ext/java/nokogiri/XmlXpathContext.java +242 -210
  38. data/ext/java/nokogiri/XsltStylesheet.java +280 -269
  39. data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
  40. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +201 -190
  41. data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +17 -10
  42. data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +43 -16
  43. data/ext/java/nokogiri/internals/NokogiriDomParser.java +63 -80
  44. data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
  45. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +27 -52
  46. data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -286
  47. data/ext/java/nokogiri/internals/NokogiriHelpers.java +736 -652
  48. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +184 -173
  49. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +79 -89
  50. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +64 -79
  51. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +84 -99
  52. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +48 -65
  53. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +119 -78
  54. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -54
  55. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +23 -46
  56. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +55 -72
  57. data/ext/java/nokogiri/internals/ParserContext.java +206 -211
  58. data/ext/java/nokogiri/internals/ReaderNode.java +478 -403
  59. data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -739
  60. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +31 -54
  61. data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
  62. data/ext/java/nokogiri/internals/XmlDeclHandler.java +3 -34
  63. data/ext/java/nokogiri/internals/XmlDomParserContext.java +206 -207
  64. data/ext/java/nokogiri/internals/XmlSaxParser.java +22 -47
  65. data/ext/java/nokogiri/internals/c14n/AttrCompare.java +71 -68
  66. data/ext/java/nokogiri/internals/c14n/C14nHelper.java +137 -118
  67. data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +27 -21
  68. data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +74 -61
  69. data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +230 -205
  70. data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +572 -547
  71. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +17 -10
  72. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +17 -10
  73. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +323 -302
  74. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +232 -219
  75. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +22 -15
  76. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +23 -16
  77. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +23 -16
  78. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +22 -15
  79. data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +575 -545
  80. data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +141 -120
  81. data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +39 -38
  82. data/ext/java/nokogiri/internals/c14n/Constants.java +13 -10
  83. data/ext/java/nokogiri/internals/c14n/ElementProxy.java +279 -247
  84. data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +66 -53
  85. data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +44 -37
  86. data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +135 -120
  87. data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +59 -48
  88. data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +384 -334
  89. data/ext/java/nokogiri/internals/c14n/NodeFilter.java +25 -24
  90. data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +151 -140
  91. data/ext/java/nokogiri/internals/c14n/XMLUtils.java +456 -423
  92. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1466 -1500
  93. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +626 -574
  94. data/ext/nokogiri/depend +37 -358
  95. data/ext/nokogiri/extconf.rb +581 -374
  96. data/ext/nokogiri/html_document.c +78 -82
  97. data/ext/nokogiri/html_element_description.c +84 -71
  98. data/ext/nokogiri/html_entity_lookup.c +21 -16
  99. data/ext/nokogiri/html_sax_parser_context.c +69 -66
  100. data/ext/nokogiri/html_sax_push_parser.c +42 -34
  101. data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
  102. data/ext/nokogiri/nokogiri.c +192 -93
  103. data/ext/nokogiri/test_global_handlers.c +40 -0
  104. data/ext/nokogiri/xml_attr.c +15 -15
  105. data/ext/nokogiri/xml_attribute_decl.c +18 -18
  106. data/ext/nokogiri/xml_cdata.c +13 -18
  107. data/ext/nokogiri/xml_comment.c +19 -26
  108. data/ext/nokogiri/xml_document.c +246 -188
  109. data/ext/nokogiri/xml_document_fragment.c +13 -15
  110. data/ext/nokogiri/xml_dtd.c +54 -48
  111. data/ext/nokogiri/xml_element_content.c +30 -27
  112. data/ext/nokogiri/xml_element_decl.c +22 -22
  113. data/ext/nokogiri/xml_encoding_handler.c +17 -11
  114. data/ext/nokogiri/xml_entity_decl.c +32 -30
  115. data/ext/nokogiri/xml_entity_reference.c +16 -18
  116. data/ext/nokogiri/xml_namespace.c +56 -49
  117. data/ext/nokogiri/xml_node.c +371 -320
  118. data/ext/nokogiri/xml_node_set.c +168 -156
  119. data/ext/nokogiri/xml_processing_instruction.c +17 -19
  120. data/ext/nokogiri/xml_reader.c +191 -157
  121. data/ext/nokogiri/xml_relax_ng.c +52 -28
  122. data/ext/nokogiri/xml_sax_parser.c +118 -118
  123. data/ext/nokogiri/xml_sax_parser_context.c +103 -86
  124. data/ext/nokogiri/xml_sax_push_parser.c +36 -27
  125. data/ext/nokogiri/xml_schema.c +95 -47
  126. data/ext/nokogiri/xml_syntax_error.c +42 -21
  127. data/ext/nokogiri/xml_text.c +13 -17
  128. data/ext/nokogiri/xml_xpath_context.c +206 -123
  129. data/ext/nokogiri/xslt_stylesheet.c +158 -161
  130. data/lib/nokogiri.rb +3 -7
  131. data/lib/nokogiri/css/parser.rb +3 -3
  132. data/lib/nokogiri/css/parser.y +2 -2
  133. data/lib/nokogiri/css/xpath_visitor.rb +70 -42
  134. data/lib/nokogiri/extension.rb +26 -0
  135. data/lib/nokogiri/html/document.rb +12 -26
  136. data/lib/nokogiri/html/document_fragment.rb +15 -15
  137. data/lib/nokogiri/nokogiri.jar +0 -0
  138. data/lib/nokogiri/version.rb +2 -149
  139. data/lib/nokogiri/version/constant.rb +5 -0
  140. data/lib/nokogiri/version/info.rb +205 -0
  141. data/lib/nokogiri/xml/document.rb +91 -35
  142. data/lib/nokogiri/xml/document_fragment.rb +4 -6
  143. data/lib/nokogiri/xml/node.rb +89 -69
  144. data/lib/nokogiri/xml/parse_options.rb +6 -0
  145. data/lib/nokogiri/xml/reader.rb +2 -9
  146. data/lib/nokogiri/xml/relax_ng.rb +6 -2
  147. data/lib/nokogiri/xml/schema.rb +12 -4
  148. data/lib/nokogiri/xml/searchable.rb +3 -1
  149. data/lib/nokogiri/xml/xpath.rb +1 -3
  150. data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
  151. metadata +86 -177
  152. data/ext/nokogiri/html_document.h +0 -10
  153. data/ext/nokogiri/html_element_description.h +0 -10
  154. data/ext/nokogiri/html_entity_lookup.h +0 -8
  155. data/ext/nokogiri/html_sax_parser_context.h +0 -11
  156. data/ext/nokogiri/html_sax_push_parser.h +0 -9
  157. data/ext/nokogiri/nokogiri.h +0 -134
  158. data/ext/nokogiri/xml_attr.h +0 -9
  159. data/ext/nokogiri/xml_attribute_decl.h +0 -9
  160. data/ext/nokogiri/xml_cdata.h +0 -9
  161. data/ext/nokogiri/xml_comment.h +0 -9
  162. data/ext/nokogiri/xml_document.h +0 -23
  163. data/ext/nokogiri/xml_document_fragment.h +0 -10
  164. data/ext/nokogiri/xml_dtd.h +0 -10
  165. data/ext/nokogiri/xml_element_content.h +0 -10
  166. data/ext/nokogiri/xml_element_decl.h +0 -9
  167. data/ext/nokogiri/xml_encoding_handler.h +0 -8
  168. data/ext/nokogiri/xml_entity_decl.h +0 -10
  169. data/ext/nokogiri/xml_entity_reference.h +0 -9
  170. data/ext/nokogiri/xml_io.c +0 -63
  171. data/ext/nokogiri/xml_io.h +0 -11
  172. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
  173. data/ext/nokogiri/xml_libxml2_hacks.h +0 -12
  174. data/ext/nokogiri/xml_namespace.h +0 -14
  175. data/ext/nokogiri/xml_node.h +0 -13
  176. data/ext/nokogiri/xml_node_set.h +0 -12
  177. data/ext/nokogiri/xml_processing_instruction.h +0 -9
  178. data/ext/nokogiri/xml_reader.h +0 -10
  179. data/ext/nokogiri/xml_relax_ng.h +0 -9
  180. data/ext/nokogiri/xml_sax_parser.h +0 -39
  181. data/ext/nokogiri/xml_sax_parser_context.h +0 -10
  182. data/ext/nokogiri/xml_sax_push_parser.h +0 -9
  183. data/ext/nokogiri/xml_schema.h +0 -9
  184. data/ext/nokogiri/xml_syntax_error.h +0 -13
  185. data/ext/nokogiri/xml_text.h +0 -9
  186. data/ext/nokogiri/xml_xpath_context.h +0 -10
  187. data/ext/nokogiri/xslt_stylesheet.h +0 -14
@@ -19,22 +19,29 @@
19
19
  package nokogiri.internals.c14n;
20
20
 
21
21
 
22
- public class Canonicalizer20010315ExclOmitComments extends Canonicalizer20010315Excl {
22
+ public class Canonicalizer20010315ExclOmitComments extends Canonicalizer20010315Excl
23
+ {
23
24
 
24
- /**
25
- *
26
- */
27
- public Canonicalizer20010315ExclOmitComments() {
28
- super(false);
29
- }
25
+ /**
26
+ *
27
+ */
28
+ public
29
+ Canonicalizer20010315ExclOmitComments()
30
+ {
31
+ super(false);
32
+ }
30
33
 
31
- /** @inheritDoc */
32
- public final String engineGetURI() {
33
- return Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS;
34
- }
34
+ /** @inheritDoc */
35
+ public final String
36
+ engineGetURI()
37
+ {
38
+ return Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS;
39
+ }
35
40
 
36
- /** @inheritDoc */
37
- public final boolean engineGetIncludeComments() {
38
- return false;
39
- }
41
+ /** @inheritDoc */
42
+ public final boolean
43
+ engineGetIncludeComments()
44
+ {
45
+ return false;
46
+ }
40
47
  }
@@ -22,23 +22,30 @@ package nokogiri.internals.c14n;
22
22
  /**
23
23
  * Class Canonicalizer20010315ExclWithComments
24
24
  */
25
- public class Canonicalizer20010315ExclWithComments extends Canonicalizer20010315Excl {
25
+ public class Canonicalizer20010315ExclWithComments extends Canonicalizer20010315Excl
26
+ {
26
27
 
27
- /**
28
- * Constructor Canonicalizer20010315ExclWithComments
29
- *
30
- */
31
- public Canonicalizer20010315ExclWithComments() {
32
- super(true);
33
- }
28
+ /**
29
+ * Constructor Canonicalizer20010315ExclWithComments
30
+ *
31
+ */
32
+ public
33
+ Canonicalizer20010315ExclWithComments()
34
+ {
35
+ super(true);
36
+ }
34
37
 
35
- /** @inheritDoc */
36
- public final String engineGetURI() {
37
- return Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS;
38
- }
38
+ /** @inheritDoc */
39
+ public final String
40
+ engineGetURI()
41
+ {
42
+ return Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS;
43
+ }
39
44
 
40
- /** @inheritDoc */
41
- public final boolean engineGetIncludeComments() {
42
- return true;
43
- }
45
+ /** @inheritDoc */
46
+ public final boolean
47
+ engineGetIncludeComments()
48
+ {
49
+ return true;
50
+ }
44
51
  }
@@ -22,23 +22,30 @@ package nokogiri.internals.c14n;
22
22
  /**
23
23
  * @author Christian Geuer-Pollmann
24
24
  */
25
- public class Canonicalizer20010315OmitComments extends Canonicalizer20010315 {
25
+ public class Canonicalizer20010315OmitComments extends Canonicalizer20010315
26
+ {
26
27
 
27
- /**
28
- * Constructor Canonicalizer20010315WithXPathOmitComments
29
- *
30
- */
31
- public Canonicalizer20010315OmitComments() {
32
- super(false);
33
- }
28
+ /**
29
+ * Constructor Canonicalizer20010315WithXPathOmitComments
30
+ *
31
+ */
32
+ public
33
+ Canonicalizer20010315OmitComments()
34
+ {
35
+ super(false);
36
+ }
34
37
 
35
- /** @inheritDoc */
36
- public final String engineGetURI() {
37
- return Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
38
- }
38
+ /** @inheritDoc */
39
+ public final String
40
+ engineGetURI()
41
+ {
42
+ return Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
43
+ }
39
44
 
40
- /** @inheritDoc */
41
- public final boolean engineGetIncludeComments() {
42
- return false;
43
- }
45
+ /** @inheritDoc */
46
+ public final boolean
47
+ engineGetIncludeComments()
48
+ {
49
+ return false;
50
+ }
44
51
  }
@@ -22,22 +22,29 @@ package nokogiri.internals.c14n;
22
22
  /**
23
23
  * @author Christian Geuer-Pollmann
24
24
  */
25
- public class Canonicalizer20010315WithComments extends Canonicalizer20010315 {
25
+ public class Canonicalizer20010315WithComments extends Canonicalizer20010315
26
+ {
26
27
 
27
- /**
28
- * Constructor Canonicalizer20010315WithXPathWithComments
29
- */
30
- public Canonicalizer20010315WithComments() {
31
- super(true);
32
- }
28
+ /**
29
+ * Constructor Canonicalizer20010315WithXPathWithComments
30
+ */
31
+ public
32
+ Canonicalizer20010315WithComments()
33
+ {
34
+ super(true);
35
+ }
33
36
 
34
- /** @inheritDoc */
35
- public final String engineGetURI() {
36
- return Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS;
37
- }
37
+ /** @inheritDoc */
38
+ public final String
39
+ engineGetURI()
40
+ {
41
+ return Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS;
42
+ }
38
43
 
39
- /** @inheritDoc */
40
- public final boolean engineGetIncludeComments() {
41
- return true;
42
- }
44
+ /** @inheritDoc */
45
+ public final boolean
46
+ engineGetIncludeComments()
47
+ {
48
+ return true;
49
+ }
43
50
  }
@@ -46,585 +46,615 @@ import org.w3c.dom.ProcessingInstruction;
46
46
  *
47
47
  * @author Christian Geuer-Pollmann <geuerp@apache.org>
48
48
  */
49
- public abstract class CanonicalizerBase extends CanonicalizerSpi {
50
- public static final String XML = "xml";
51
- public static final String XMLNS = "xmlns";
52
-
53
- protected static final AttrCompare COMPARE = new AttrCompare();
54
- protected static final Attr nullNode;
55
-
56
- private static final byte[] END_PI = {'?','>'};
57
- private static final byte[] BEGIN_PI = {'<','?'};
58
- private static final byte[] END_COMM = {'-','-','>'};
59
- private static final byte[] BEGIN_COMM = {'<','!','-','-'};
60
- private static final byte[] XA = {'&','#','x','A',';'};
61
- private static final byte[] X9 = {'&','#','x','9',';'};
62
- private static final byte[] QUOT = {'&','q','u','o','t',';'};
63
- private static final byte[] XD = {'&','#','x','D',';'};
64
- private static final byte[] GT = {'&','g','t',';'};
65
- private static final byte[] LT = {'&','l','t',';'};
66
- private static final byte[] END_TAG = {'<','/'};
67
- private static final byte[] AMP = {'&','a','m','p',';'};
68
- private static final byte[] equalsStr = {'=','\"'};
69
-
70
- protected static final int NODE_BEFORE_DOCUMENT_ELEMENT = -1;
71
- protected static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0;
72
- protected static final int NODE_AFTER_DOCUMENT_ELEMENT = 1;
73
-
74
- static {
75
- // The null xmlns definition.
76
- try {
77
- DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
78
- nullNode = documentBuilder.newDocument().createAttributeNS(Constants.NamespaceSpecNS, XMLNS);
79
- nullNode.setValue("");
80
- } catch (Exception e) {
81
- throw new RuntimeException("Unable to create nullNode: " + e);
82
- }
83
- }
84
-
85
- private List<NodeFilter> nodeFilter;
86
-
87
- private final boolean includeComments;
88
- //private Set<Node> xpathNodeSet;
89
- /**
90
- * The node to be skipped/excluded from the DOM tree
91
- * in subtree canonicalizations.
92
- */
93
- private Node excludeNode;
94
- private OutputStream writer = new ByteArrayOutputStream();
95
-
96
- /**
97
- * Constructor CanonicalizerBase
98
- *
99
- * @param includeComments
100
- */
101
- public CanonicalizerBase(boolean includeComments) {
102
- this.includeComments = includeComments;
49
+ public abstract class CanonicalizerBase extends CanonicalizerSpi
50
+ {
51
+ public static final String XML = "xml";
52
+ public static final String XMLNS = "xmlns";
53
+
54
+ protected static final AttrCompare COMPARE = new AttrCompare();
55
+ protected static final Attr nullNode;
56
+
57
+ private static final byte[] END_PI = {'?', '>'};
58
+ private static final byte[] BEGIN_PI = {'<', '?'};
59
+ private static final byte[] END_COMM = {'-', '-', '>'};
60
+ private static final byte[] BEGIN_COMM = {'<', '!', '-', '-'};
61
+ private static final byte[] XA = {'&', '#', 'x', 'A', ';'};
62
+ private static final byte[] X9 = {'&', '#', 'x', '9', ';'};
63
+ private static final byte[] QUOT = {'&', 'q', 'u', 'o', 't', ';'};
64
+ private static final byte[] XD = {'&', '#', 'x', 'D', ';'};
65
+ private static final byte[] GT = {'&', 'g', 't', ';'};
66
+ private static final byte[] LT = {'&', 'l', 't', ';'};
67
+ private static final byte[] END_TAG = {'<', '/'};
68
+ private static final byte[] AMP = {'&', 'a', 'm', 'p', ';'};
69
+ private static final byte[] equalsStr = {'=', '\"'};
70
+
71
+ protected static final int NODE_BEFORE_DOCUMENT_ELEMENT = -1;
72
+ protected static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0;
73
+ protected static final int NODE_AFTER_DOCUMENT_ELEMENT = 1;
74
+
75
+ static
76
+ {
77
+ // The null xmlns definition.
78
+ try {
79
+ DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
80
+ nullNode = documentBuilder.newDocument().createAttributeNS(Constants.NamespaceSpecNS, XMLNS);
81
+ nullNode.setValue("");
82
+ } catch (Exception e) {
83
+ throw new RuntimeException("Unable to create nullNode: " + e);
103
84
  }
104
-
105
- /**
106
- * Method engineCanonicalizeSubTree
107
- * @inheritDoc
108
- * @param rootNode
109
- * @throws CanonicalizationException
110
- */
111
- @Override
112
- public byte[] engineCanonicalizeSubTree(Node rootNode, CanonicalFilter filter)
113
- throws CanonicalizationException {
114
- return engineCanonicalizeSubTree(rootNode, (Node)null, filter);
85
+ }
86
+
87
+ private List<NodeFilter> nodeFilter;
88
+
89
+ private final boolean includeComments;
90
+ //private Set<Node> xpathNodeSet;
91
+ /**
92
+ * The node to be skipped/excluded from the DOM tree
93
+ * in subtree canonicalizations.
94
+ */
95
+ private Node excludeNode;
96
+ private OutputStream writer = new ByteArrayOutputStream();
97
+
98
+ /**
99
+ * Constructor CanonicalizerBase
100
+ *
101
+ * @param includeComments
102
+ */
103
+ public
104
+ CanonicalizerBase(boolean includeComments)
105
+ {
106
+ this.includeComments = includeComments;
107
+ }
108
+
109
+ /**
110
+ * Method engineCanonicalizeSubTree
111
+ * @inheritDoc
112
+ * @param rootNode
113
+ * @throws CanonicalizationException
114
+ */
115
+ @Override
116
+ public byte[]
117
+ engineCanonicalizeSubTree(Node rootNode, CanonicalFilter filter)
118
+ throws CanonicalizationException
119
+ {
120
+ return engineCanonicalizeSubTree(rootNode, (Node)null, filter);
121
+ }
122
+
123
+ /**
124
+ * @param writer The writer to set.
125
+ */
126
+ @Override
127
+ public void
128
+ setWriter(OutputStream writer)
129
+ {
130
+ this.writer = writer;
131
+ }
132
+
133
+ /**
134
+ * Canonicalizes a Subtree node.
135
+ *
136
+ * @param rootNode
137
+ * the root of the subtree to canonicalize
138
+ * @param excludeNode
139
+ * a node to be excluded from the canonicalize operation
140
+ * @return The canonicalize stream.
141
+ * @throws CanonicalizationException
142
+ */
143
+ protected byte[]
144
+ engineCanonicalizeSubTree(Node rootNode, Node excludeNode, CanonicalFilter filter)
145
+ throws CanonicalizationException
146
+ {
147
+ this.excludeNode = excludeNode;
148
+ try {
149
+ NameSpaceSymbTable ns = new NameSpaceSymbTable();
150
+ int nodeLevel = NODE_BEFORE_DOCUMENT_ELEMENT;
151
+ if (rootNode != null && Node.ELEMENT_NODE == rootNode.getNodeType()) {
152
+ //Fills the nssymbtable with the definitions of the parent of the root subnode
153
+ getParentNameSpaces((Element)rootNode, ns);
154
+ nodeLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
155
+ }
156
+ this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel, filter);
157
+ this.writer.flush();
158
+ if (this.writer instanceof ByteArrayOutputStream) {
159
+ byte[] result = ((ByteArrayOutputStream)this.writer).toByteArray();
160
+ if (reset) {
161
+ ((ByteArrayOutputStream)this.writer).reset();
162
+ } else {
163
+ this.writer.close();
164
+ }
165
+ return result;
166
+ } else {
167
+ this.writer.close();
168
+ }
169
+ return null;
170
+
171
+ } catch (UnsupportedEncodingException ex) {
172
+ throw new CanonicalizationException("empty", ex);
173
+ } catch (IOException ex) {
174
+ throw new CanonicalizationException("empty", ex);
115
175
  }
116
-
117
- /**
118
- * @param writer The writer to set.
119
- */
120
- @Override
121
- public void setWriter(OutputStream writer) {
122
- this.writer = writer;
176
+ }
177
+
178
+
179
+ /**
180
+ * Method canonicalizeSubTree, this function is a recursive one.
181
+ *
182
+ * @param currentNode
183
+ * @param ns
184
+ * @param endnode
185
+ * @throws CanonicalizationException
186
+ * @throws IOException
187
+ */
188
+ protected final void
189
+ canonicalizeSubTree(
190
+ Node currentNode, NameSpaceSymbTable ns, Node endnode, int documentLevel, CanonicalFilter filter
191
+ ) throws CanonicalizationException, IOException
192
+ {
193
+ if (isVisibleInt(currentNode) == -1) {
194
+ return;
123
195
  }
124
-
125
- /**
126
- * Canonicalizes a Subtree node.
127
- *
128
- * @param rootNode
129
- * the root of the subtree to canonicalize
130
- * @param excludeNode
131
- * a node to be excluded from the canonicalize operation
132
- * @return The canonicalize stream.
133
- * @throws CanonicalizationException
134
- */
135
- protected byte[] engineCanonicalizeSubTree(Node rootNode, Node excludeNode, CanonicalFilter filter)
136
- throws CanonicalizationException {
137
- this.excludeNode = excludeNode;
138
- try {
139
- NameSpaceSymbTable ns = new NameSpaceSymbTable();
140
- int nodeLevel = NODE_BEFORE_DOCUMENT_ELEMENT;
141
- if (rootNode != null && Node.ELEMENT_NODE == rootNode.getNodeType()) {
142
- //Fills the nssymbtable with the definitions of the parent of the root subnode
143
- getParentNameSpaces((Element)rootNode, ns);
144
- nodeLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
145
- }
146
- this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel, filter);
147
- this.writer.flush();
148
- if (this.writer instanceof ByteArrayOutputStream) {
149
- byte[] result = ((ByteArrayOutputStream)this.writer).toByteArray();
150
- if (reset) {
151
- ((ByteArrayOutputStream)this.writer).reset();
152
- } else {
153
- this.writer.close();
154
- }
155
- return result;
156
- } else {
157
- this.writer.close();
158
- }
159
- return null;
160
-
161
- } catch (UnsupportedEncodingException ex) {
162
- throw new CanonicalizationException("empty", ex);
163
- } catch (IOException ex) {
164
- throw new CanonicalizationException("empty", ex);
196
+ Node sibling = null;
197
+ Node parentNode = null;
198
+ final OutputStream writer = this.writer;
199
+ final Node excludeNode = this.excludeNode;
200
+ final boolean includeComments = this.includeComments;
201
+ Map<String, byte[]> cache = new HashMap<String, byte[]>();
202
+ do {
203
+ switch (currentNode.getNodeType()) {
204
+
205
+ case Node.ENTITY_NODE :
206
+ case Node.NOTATION_NODE :
207
+ case Node.ATTRIBUTE_NODE :
208
+ // illegal node type during traversal
209
+ throw new CanonicalizationException("empty");
210
+
211
+ case Node.DOCUMENT_FRAGMENT_NODE :
212
+ case Node.DOCUMENT_NODE :
213
+ ns.outputNodePush();
214
+ sibling = currentNode.getFirstChild();
215
+ break;
216
+
217
+ case Node.COMMENT_NODE :
218
+ if (includeComments) {
219
+ outputCommentToWriter((Comment) currentNode, writer, documentLevel);
165
220
  }
166
- }
221
+ break;
167
222
 
223
+ case Node.PROCESSING_INSTRUCTION_NODE :
224
+ outputPItoWriter((ProcessingInstruction) currentNode, writer, documentLevel);
225
+ break;
168
226
 
169
- /**
170
- * Method canonicalizeSubTree, this function is a recursive one.
171
- *
172
- * @param currentNode
173
- * @param ns
174
- * @param endnode
175
- * @throws CanonicalizationException
176
- * @throws IOException
177
- */
178
- protected final void canonicalizeSubTree(
179
- Node currentNode, NameSpaceSymbTable ns, Node endnode, int documentLevel, CanonicalFilter filter
180
- ) throws CanonicalizationException, IOException {
181
- if (isVisibleInt(currentNode) == -1) {
182
- return;
183
- }
184
- Node sibling = null;
185
- Node parentNode = null;
186
- final OutputStream writer = this.writer;
187
- final Node excludeNode = this.excludeNode;
188
- final boolean includeComments = this.includeComments;
189
- Map<String, byte[]> cache = new HashMap<String, byte[]>();
190
- do {
191
- switch (currentNode.getNodeType()) {
192
-
193
- case Node.ENTITY_NODE :
194
- case Node.NOTATION_NODE :
195
- case Node.ATTRIBUTE_NODE :
196
- // illegal node type during traversal
197
- throw new CanonicalizationException("empty");
198
-
199
- case Node.DOCUMENT_FRAGMENT_NODE :
200
- case Node.DOCUMENT_NODE :
201
- ns.outputNodePush();
202
- sibling = currentNode.getFirstChild();
203
- break;
204
-
205
- case Node.COMMENT_NODE :
206
- if (includeComments) {
207
- outputCommentToWriter((Comment) currentNode, writer, documentLevel);
208
- }
209
- break;
210
-
211
- case Node.PROCESSING_INSTRUCTION_NODE :
212
- outputPItoWriter((ProcessingInstruction) currentNode, writer, documentLevel);
213
- break;
214
-
215
- case Node.TEXT_NODE :
216
- case Node.CDATA_SECTION_NODE :
217
- outputTextToWriter(currentNode.getNodeValue(), writer);
218
- break;
219
-
220
- case Node.ELEMENT_NODE :
221
- documentLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
222
- if (currentNode == excludeNode) {
223
- break;
224
- }
225
- if (filter != null && !filter.includeNodes(currentNode, parentNode)) {
226
- break;
227
- }
228
-
229
- Element currentElement = (Element)currentNode;
230
- //Add a level to the nssymbtable. So latter can be pop-back.
231
- ns.outputNodePush();
232
- writer.write('<');
233
- String name = currentElement.getTagName();
234
- UtfHelpper.writeByte(name, writer, cache);
235
-
236
- Iterator<Attr> attrs = this.handleAttributesSubtree(currentElement, ns);
237
- if (attrs != null) {
238
- //we output all Attrs which are available
239
- while (attrs.hasNext()) {
240
- Attr attr = attrs.next();
241
- outputAttrToWriter(attr.getNodeName(), attr.getNodeValue(), writer, cache);
242
- }
243
- }
244
- writer.write('>');
245
- sibling = currentNode.getFirstChild();
246
- if (sibling == null) {
247
- writer.write(END_TAG);
248
- UtfHelpper.writeStringToUtf8(name, writer);
249
- writer.write('>');
250
- //We finished with this level, pop to the previous definitions.
251
- ns.outputNodePop();
252
- if (parentNode != null) {
253
- sibling = currentNode.getNextSibling();
254
- }
255
- } else {
256
- parentNode = currentElement;
257
- }
258
- break;
259
- case Node.DOCUMENT_TYPE_NODE :
260
- default :
261
- break;
262
- }
263
- while (sibling == null && parentNode != null) {
264
- writer.write(END_TAG);
265
- UtfHelpper.writeByte(((Element)parentNode).getTagName(), writer, cache);
266
- writer.write('>');
267
- //We finished with this level, pop to the previous definitions.
268
- ns.outputNodePop();
269
- if (parentNode == endnode) {
270
- return;
271
- }
272
- sibling = parentNode.getNextSibling();
273
- parentNode = parentNode.getParentNode();
274
- if (parentNode == null || Node.ELEMENT_NODE != parentNode.getNodeType()) {
275
- documentLevel = NODE_AFTER_DOCUMENT_ELEMENT;
276
- parentNode = null;
277
- }
278
- }
279
- if (sibling == null) {
280
- return;
281
- }
282
- currentNode = sibling;
283
- sibling = currentNode.getNextSibling();
284
- } while(true);
285
- }
227
+ case Node.TEXT_NODE :
228
+ case Node.CDATA_SECTION_NODE :
229
+ outputTextToWriter(currentNode.getNodeValue(), writer);
230
+ break;
286
231
 
287
- protected int isVisibleDO(Node currentNode, int level) {
288
- if (nodeFilter != null) {
289
- Iterator<NodeFilter> it = nodeFilter.iterator();
290
- while (it.hasNext()) {
291
- int i = (it.next()).isNodeIncludeDO(currentNode, level);
292
- if (i != 1) {
293
- return i;
294
- }
295
- }
232
+ case Node.ELEMENT_NODE :
233
+ documentLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
234
+ if (currentNode == excludeNode) {
235
+ break;
296
236
  }
297
- //if ((this.xpathNodeSet != null) && !this.xpathNodeSet.contains(currentNode)) {
298
- // return 0;
299
- //}
300
- return 1;
301
- }
302
-
303
- protected int isVisibleInt(Node currentNode) {
304
- if (nodeFilter != null) {
305
- Iterator<NodeFilter> it = nodeFilter.iterator();
306
- while (it.hasNext()) {
307
- int i = (it.next()).isNodeInclude(currentNode);
308
- if (i != 1) {
309
- return i;
310
- }
311
- }
237
+ if (filter != null && !filter.includeNodes(currentNode, parentNode)) {
238
+ break;
312
239
  }
313
- //if ((this.xpathNodeSet != null) && !this.xpathNodeSet.contains(currentNode)) {
314
- // return 0;
315
- //}
316
- return 1;
317
- }
318
240
 
319
- protected boolean isVisible(Node currentNode) {
320
- if (nodeFilter != null) {
321
- Iterator<NodeFilter> it = nodeFilter.iterator();
322
- while (it.hasNext()) {
323
- if (it.next().isNodeInclude(currentNode) != 1) {
324
- return false;
325
- }
326
- }
327
- }
328
- //if ((this.xpathNodeSet != null) && !this.xpathNodeSet.contains(currentNode)) {
329
- // return false;
330
- //}
331
- return true;
332
- }
241
+ Element currentElement = (Element)currentNode;
242
+ //Add a level to the nssymbtable. So latter can be pop-back.
243
+ ns.outputNodePush();
244
+ writer.write('<');
245
+ String name = currentElement.getTagName();
246
+ UtfHelpper.writeByte(name, writer, cache);
333
247
 
334
- protected void handleParent(Element e, NameSpaceSymbTable ns) {
335
- if (!e.hasAttributes() && e.getNamespaceURI() == null) {
336
- return;
248
+ Iterator<Attr> attrs = this.handleAttributesSubtree(currentElement, ns);
249
+ if (attrs != null) {
250
+ //we output all Attrs which are available
251
+ while (attrs.hasNext()) {
252
+ Attr attr = attrs.next();
253
+ outputAttrToWriter(attr.getNodeName(), attr.getNodeValue(), writer, cache);
254
+ }
337
255
  }
338
- NamedNodeMap attrs = e.getAttributes();
339
- int attrsLength = attrs.getLength();
340
- for (int i = 0; i < attrsLength; i++) {
341
- Attr attribute = (Attr) attrs.item(i);
342
- String NName = attribute.getLocalName();
343
- String NValue = attribute.getNodeValue();
344
-
345
- if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())
346
- && (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue))) {
347
- ns.addMapping(NName, NValue, attribute);
348
- }
256
+ writer.write('>');
257
+ sibling = currentNode.getFirstChild();
258
+ if (sibling == null) {
259
+ writer.write(END_TAG);
260
+ UtfHelpper.writeStringToUtf8(name, writer);
261
+ writer.write('>');
262
+ //We finished with this level, pop to the previous definitions.
263
+ ns.outputNodePop();
264
+ if (parentNode != null) {
265
+ sibling = currentNode.getNextSibling();
266
+ }
267
+ } else {
268
+ parentNode = currentElement;
349
269
  }
350
- if (e.getNamespaceURI() != null) {
351
- String NName = e.getPrefix();
352
- String NValue = e.getNamespaceURI();
353
- String Name;
354
- if (NName == null || NName.equals("")) {
355
- NName = XMLNS;
356
- Name = XMLNS;
357
- } else {
358
- Name = XMLNS + ":" + NName;
359
- }
360
- Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name);
361
- n.setValue(NValue);
362
- ns.addMapping(NName, NValue, n);
270
+ break;
271
+ case Node.DOCUMENT_TYPE_NODE :
272
+ default :
273
+ break;
274
+ }
275
+ while (sibling == null && parentNode != null) {
276
+ writer.write(END_TAG);
277
+ UtfHelpper.writeByte(((Element)parentNode).getTagName(), writer, cache);
278
+ writer.write('>');
279
+ //We finished with this level, pop to the previous definitions.
280
+ ns.outputNodePop();
281
+ if (parentNode == endnode) {
282
+ return;
363
283
  }
364
- }
365
-
366
- /**
367
- * Adds to ns the definitions from the parent elements of el
368
- * @param el
369
- * @param ns
370
- */
371
- protected final void getParentNameSpaces(Element el, NameSpaceSymbTable ns) {
372
- Node n1 = el.getParentNode();
373
- if (n1 == null || Node.ELEMENT_NODE != n1.getNodeType()) {
374
- return;
284
+ sibling = parentNode.getNextSibling();
285
+ parentNode = parentNode.getParentNode();
286
+ if (parentNode == null || Node.ELEMENT_NODE != parentNode.getNodeType()) {
287
+ documentLevel = NODE_AFTER_DOCUMENT_ELEMENT;
288
+ parentNode = null;
375
289
  }
376
- //Obtain all the parents of the element
377
- List<Element> parents = new ArrayList<Element>();
378
- Node parent = n1;
379
- while (parent != null && Node.ELEMENT_NODE == parent.getNodeType()) {
380
- parents.add((Element)parent);
381
- parent = parent.getParentNode();
290
+ }
291
+ if (sibling == null) {
292
+ return;
293
+ }
294
+ currentNode = sibling;
295
+ sibling = currentNode.getNextSibling();
296
+ } while (true);
297
+ }
298
+
299
+ protected int
300
+ isVisibleDO(Node currentNode, int level)
301
+ {
302
+ if (nodeFilter != null) {
303
+ Iterator<NodeFilter> it = nodeFilter.iterator();
304
+ while (it.hasNext()) {
305
+ int i = (it.next()).isNodeIncludeDO(currentNode, level);
306
+ if (i != 1) {
307
+ return i;
382
308
  }
383
- //Visit them in reverse order.
384
- ListIterator<Element> it = parents.listIterator(parents.size());
385
- while (it.hasPrevious()) {
386
- Element ele = it.previous();
387
- handleParent(ele, ns);
309
+ }
310
+ }
311
+ //if ((this.xpathNodeSet != null) && !this.xpathNodeSet.contains(currentNode)) {
312
+ // return 0;
313
+ //}
314
+ return 1;
315
+ }
316
+
317
+ protected int
318
+ isVisibleInt(Node currentNode)
319
+ {
320
+ if (nodeFilter != null) {
321
+ Iterator<NodeFilter> it = nodeFilter.iterator();
322
+ while (it.hasNext()) {
323
+ int i = (it.next()).isNodeInclude(currentNode);
324
+ if (i != 1) {
325
+ return i;
388
326
  }
389
- parents.clear();
390
- Attr nsprefix;
391
- if (((nsprefix = ns.getMappingWithoutRendered(XMLNS)) != null)
392
- && "".equals(nsprefix.getValue())) {
393
- ns.addMappingAndRender(XMLNS, "", nullNode);
327
+ }
328
+ }
329
+ //if ((this.xpathNodeSet != null) && !this.xpathNodeSet.contains(currentNode)) {
330
+ // return 0;
331
+ //}
332
+ return 1;
333
+ }
334
+
335
+ protected boolean
336
+ isVisible(Node currentNode)
337
+ {
338
+ if (nodeFilter != null) {
339
+ Iterator<NodeFilter> it = nodeFilter.iterator();
340
+ while (it.hasNext()) {
341
+ if (it.next().isNodeInclude(currentNode) != 1) {
342
+ return false;
394
343
  }
344
+ }
395
345
  }
396
-
397
- /**
398
- * Obtain the attributes to output for this node in XPathNodeSet c14n.
399
- *
400
- * @param element
401
- * @param ns
402
- * @return the attributes nodes to output.
403
- * @throws CanonicalizationException
404
- */
405
- abstract Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns)
406
- throws CanonicalizationException;
407
-
408
- /**
409
- * Obtain the attributes to output for this node in a Subtree c14n.
410
- *
411
- * @param element
412
- * @param ns
413
- * @return the attributes nodes to output.
414
- * @throws CanonicalizationException
415
- */
416
- abstract Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
417
- throws CanonicalizationException;
418
-
419
- //abstract void circumventBugIfNeeded(XMLSignatureInput input)
420
- // throws CanonicalizationException, ParserConfigurationException, IOException, SAXException;
421
-
422
- /**
423
- * Outputs an Attribute to the internal Writer.
424
- *
425
- * The string value of the node is modified by replacing
426
- * <UL>
427
- * <LI>all ampersands (&) with <CODE>&amp;amp;</CODE></LI>
428
- * <LI>all open angle brackets (<) with <CODE>&amp;lt;</CODE></LI>
429
- * <LI>all quotation mark characters with <CODE>&amp;quot;</CODE></LI>
430
- * <LI>and the whitespace characters <CODE>#x9</CODE>, #xA, and #xD, with character
431
- * references. The character references are written in uppercase
432
- * hexadecimal with no leading zeroes (for example, <CODE>#xD</CODE> is represented
433
- * by the character reference <CODE>&amp;#xD;</CODE>)</LI>
434
- * </UL>
435
- *
436
- * @param name
437
- * @param value
438
- * @param writer
439
- * @throws IOException
440
- */
441
- protected static final void outputAttrToWriter(
442
- final String name, final String value,
443
- final OutputStream writer, final Map<String, byte[]> cache
444
- ) throws IOException {
445
- writer.write(' ');
446
- UtfHelpper.writeByte(name, writer, cache);
447
- writer.write(equalsStr);
448
- byte[] toWrite;
449
- final int length = value.length();
450
- int i = 0;
451
- while (i < length) {
452
- char c = value.charAt(i++);
453
-
454
- switch (c) {
455
-
456
- case '&' :
457
- toWrite = AMP;
458
- break;
459
-
460
- case '<' :
461
- toWrite = LT;
462
- break;
463
-
464
- case '"' :
465
- toWrite = QUOT;
466
- break;
467
-
468
- case 0x09 : // '\t'
469
- toWrite = X9;
470
- break;
471
-
472
- case 0x0A : // '\n'
473
- toWrite = XA;
474
- break;
475
-
476
- case 0x0D : // '\r'
477
- toWrite = XD;
478
- break;
479
-
480
- default :
481
- if (c < 0x80) {
482
- writer.write(c);
483
- } else {
484
- UtfHelpper.writeCharToUtf8(c, writer);
485
- }
486
- continue;
487
- }
488
- writer.write(toWrite);
346
+ //if ((this.xpathNodeSet != null) && !this.xpathNodeSet.contains(currentNode)) {
347
+ // return false;
348
+ //}
349
+ return true;
350
+ }
351
+
352
+ protected void
353
+ handleParent(Element e, NameSpaceSymbTable ns)
354
+ {
355
+ if (!e.hasAttributes() && e.getNamespaceURI() == null) {
356
+ return;
357
+ }
358
+ NamedNodeMap attrs = e.getAttributes();
359
+ int attrsLength = attrs.getLength();
360
+ for (int i = 0; i < attrsLength; i++) {
361
+ Attr attribute = (Attr) attrs.item(i);
362
+ String NName = attribute.getLocalName();
363
+ String NValue = attribute.getNodeValue();
364
+
365
+ if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())
366
+ && (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue))) {
367
+ ns.addMapping(NName, NValue, attribute);
368
+ }
369
+ }
370
+ if (e.getNamespaceURI() != null) {
371
+ String NName = e.getPrefix();
372
+ String NValue = e.getNamespaceURI();
373
+ String Name;
374
+ if (NName == null || NName.equals("")) {
375
+ NName = XMLNS;
376
+ Name = XMLNS;
377
+ } else {
378
+ Name = XMLNS + ":" + NName;
379
+ }
380
+ Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name);
381
+ n.setValue(NValue);
382
+ ns.addMapping(NName, NValue, n);
383
+ }
384
+ }
385
+
386
+ /**
387
+ * Adds to ns the definitions from the parent elements of el
388
+ * @param el
389
+ * @param ns
390
+ */
391
+ protected final void
392
+ getParentNameSpaces(Element el, NameSpaceSymbTable ns)
393
+ {
394
+ Node n1 = el.getParentNode();
395
+ if (n1 == null || Node.ELEMENT_NODE != n1.getNodeType()) {
396
+ return;
397
+ }
398
+ //Obtain all the parents of the element
399
+ List<Element> parents = new ArrayList<Element>();
400
+ Node parent = n1;
401
+ while (parent != null && Node.ELEMENT_NODE == parent.getNodeType()) {
402
+ parents.add((Element)parent);
403
+ parent = parent.getParentNode();
404
+ }
405
+ //Visit them in reverse order.
406
+ ListIterator<Element> it = parents.listIterator(parents.size());
407
+ while (it.hasPrevious()) {
408
+ Element ele = it.previous();
409
+ handleParent(ele, ns);
410
+ }
411
+ parents.clear();
412
+ Attr nsprefix;
413
+ if (((nsprefix = ns.getMappingWithoutRendered(XMLNS)) != null)
414
+ && "".equals(nsprefix.getValue())) {
415
+ ns.addMappingAndRender(XMLNS, "", nullNode);
416
+ }
417
+ }
418
+
419
+ /**
420
+ * Obtain the attributes to output for this node in XPathNodeSet c14n.
421
+ *
422
+ * @param element
423
+ * @param ns
424
+ * @return the attributes nodes to output.
425
+ * @throws CanonicalizationException
426
+ */
427
+ abstract Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns)
428
+ throws CanonicalizationException;
429
+
430
+ /**
431
+ * Obtain the attributes to output for this node in a Subtree c14n.
432
+ *
433
+ * @param element
434
+ * @param ns
435
+ * @return the attributes nodes to output.
436
+ * @throws CanonicalizationException
437
+ */
438
+ abstract Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
439
+ throws CanonicalizationException;
440
+
441
+ //abstract void circumventBugIfNeeded(XMLSignatureInput input)
442
+ // throws CanonicalizationException, ParserConfigurationException, IOException, SAXException;
443
+
444
+ /**
445
+ * Outputs an Attribute to the internal Writer.
446
+ *
447
+ * The string value of the node is modified by replacing
448
+ * <UL>
449
+ * <LI>all ampersands (&) with <CODE>&amp;amp;</CODE></LI>
450
+ * <LI>all open angle brackets (<) with <CODE>&amp;lt;</CODE></LI>
451
+ * <LI>all quotation mark characters with <CODE>&amp;quot;</CODE></LI>
452
+ * <LI>and the whitespace characters <CODE>#x9</CODE>, #xA, and #xD, with character
453
+ * references. The character references are written in uppercase
454
+ * hexadecimal with no leading zeroes (for example, <CODE>#xD</CODE> is represented
455
+ * by the character reference <CODE>&amp;#xD;</CODE>)</LI>
456
+ * </UL>
457
+ *
458
+ * @param name
459
+ * @param value
460
+ * @param writer
461
+ * @throws IOException
462
+ */
463
+ protected static final void
464
+ outputAttrToWriter(
465
+ final String name, final String value,
466
+ final OutputStream writer, final Map<String, byte[]> cache
467
+ ) throws IOException
468
+ {
469
+ writer.write(' ');
470
+ UtfHelpper.writeByte(name, writer, cache);
471
+ writer.write(equalsStr);
472
+ byte[] toWrite;
473
+ final int length = value.length();
474
+ int i = 0;
475
+ while (i < length) {
476
+ char c = value.charAt(i++);
477
+
478
+ switch (c) {
479
+
480
+ case '&' :
481
+ toWrite = AMP;
482
+ break;
483
+
484
+ case '<' :
485
+ toWrite = LT;
486
+ break;
487
+
488
+ case '"' :
489
+ toWrite = QUOT;
490
+ break;
491
+
492
+ case 0x09 : // '\t'
493
+ toWrite = X9;
494
+ break;
495
+
496
+ case 0x0A : // '\n'
497
+ toWrite = XA;
498
+ break;
499
+
500
+ case 0x0D : // '\r'
501
+ toWrite = XD;
502
+ break;
503
+
504
+ default :
505
+ if (c < 0x80) {
506
+ writer.write(c);
507
+ } else {
508
+ UtfHelpper.writeCharToUtf8(c, writer);
489
509
  }
490
-
491
- writer.write('\"');
510
+ continue;
511
+ }
512
+ writer.write(toWrite);
492
513
  }
493
514
 
494
- /**
495
- * Outputs a PI to the internal Writer.
496
- *
497
- * @param currentPI
498
- * @param writer where to write the things
499
- * @throws IOException
500
- */
501
- protected void outputPItoWriter(
502
- ProcessingInstruction currentPI, OutputStream writer, int position
503
- ) throws IOException {
504
- if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
505
- writer.write('\n');
506
- }
507
- writer.write(BEGIN_PI);
508
-
509
- final String target = currentPI.getTarget();
510
- int length = target.length();
511
-
512
- for (int i = 0; i < length; i++) {
513
- char c = target.charAt(i);
514
- if (c == 0x0D) {
515
- writer.write(XD);
516
- } else {
517
- if (c < 0x80) {
518
- writer.write(c);
519
- } else {
520
- UtfHelpper.writeCharToUtf8(c, writer);
521
- }
522
- }
515
+ writer.write('\"');
516
+ }
517
+
518
+ /**
519
+ * Outputs a PI to the internal Writer.
520
+ *
521
+ * @param currentPI
522
+ * @param writer where to write the things
523
+ * @throws IOException
524
+ */
525
+ protected void
526
+ outputPItoWriter(
527
+ ProcessingInstruction currentPI, OutputStream writer, int position
528
+ ) throws IOException
529
+ {
530
+ if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
531
+ writer.write('\n');
532
+ }
533
+ writer.write(BEGIN_PI);
534
+
535
+ final String target = currentPI.getTarget();
536
+ int length = target.length();
537
+
538
+ for (int i = 0; i < length; i++) {
539
+ char c = target.charAt(i);
540
+ if (c == 0x0D) {
541
+ writer.write(XD);
542
+ } else {
543
+ if (c < 0x80) {
544
+ writer.write(c);
545
+ } else {
546
+ UtfHelpper.writeCharToUtf8(c, writer);
523
547
  }
548
+ }
549
+ }
524
550
 
525
- final String data = currentPI.getData();
551
+ final String data = currentPI.getData();
526
552
 
527
- length = data.length();
553
+ length = data.length();
528
554
 
529
- if (length > 0) {
530
- writer.write(' ');
555
+ if (length > 0) {
556
+ writer.write(' ');
531
557
 
532
- for (int i = 0; i < length; i++) {
533
- char c = data.charAt(i);
534
- if (c == 0x0D) {
535
- writer.write(XD);
536
- } else {
537
- UtfHelpper.writeCharToUtf8(c, writer);
538
- }
539
- }
540
- }
541
-
542
- writer.write(END_PI);
543
- if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
544
- writer.write('\n');
558
+ for (int i = 0; i < length; i++) {
559
+ char c = data.charAt(i);
560
+ if (c == 0x0D) {
561
+ writer.write(XD);
562
+ } else {
563
+ UtfHelpper.writeCharToUtf8(c, writer);
545
564
  }
565
+ }
546
566
  }
547
567
 
548
- /**
549
- * Method outputCommentToWriter
550
- *
551
- * @param currentComment
552
- * @param writer writer where to write the things
553
- * @throws IOException
554
- */
555
- protected void outputCommentToWriter(
556
- Comment currentComment, OutputStream writer, int position
557
- ) throws IOException {
558
- if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
559
- writer.write('\n');
560
- }
561
- writer.write(BEGIN_COMM);
562
-
563
- final String data = currentComment.getData();
564
- final int length = data.length();
565
-
566
- for (int i = 0; i < length; i++) {
567
- char c = data.charAt(i);
568
- if (c == 0x0D) {
569
- writer.write(XD);
570
- } else {
571
- if (c < 0x80) {
572
- writer.write(c);
573
- } else {
574
- UtfHelpper.writeCharToUtf8(c, writer);
575
- }
576
- }
577
- }
578
-
579
- writer.write(END_COMM);
580
- if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
581
- writer.write('\n');
568
+ writer.write(END_PI);
569
+ if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
570
+ writer.write('\n');
571
+ }
572
+ }
573
+
574
+ /**
575
+ * Method outputCommentToWriter
576
+ *
577
+ * @param currentComment
578
+ * @param writer writer where to write the things
579
+ * @throws IOException
580
+ */
581
+ protected void
582
+ outputCommentToWriter(
583
+ Comment currentComment, OutputStream writer, int position
584
+ ) throws IOException
585
+ {
586
+ if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
587
+ writer.write('\n');
588
+ }
589
+ writer.write(BEGIN_COMM);
590
+
591
+ final String data = currentComment.getData();
592
+ final int length = data.length();
593
+
594
+ for (int i = 0; i < length; i++) {
595
+ char c = data.charAt(i);
596
+ if (c == 0x0D) {
597
+ writer.write(XD);
598
+ } else {
599
+ if (c < 0x80) {
600
+ writer.write(c);
601
+ } else {
602
+ UtfHelpper.writeCharToUtf8(c, writer);
582
603
  }
604
+ }
583
605
  }
584
606
 
585
- /**
586
- * Outputs a Text of CDATA section to the internal Writer.
587
- *
588
- * @param text
589
- * @param writer writer where to write the things
590
- * @throws IOException
591
- */
592
- protected static final void outputTextToWriter(
593
- final String text, final OutputStream writer
594
- ) throws IOException {
595
- final int length = text.length();
596
- byte[] toWrite;
597
- for (int i = 0; i < length; i++) {
598
- char c = text.charAt(i);
599
-
600
- switch (c) {
601
-
602
- case '&' :
603
- toWrite = AMP;
604
- break;
605
-
606
- case '<' :
607
- toWrite = LT;
608
- break;
609
-
610
- case '>' :
611
- toWrite = GT;
612
- break;
613
-
614
- case 0xD :
615
- toWrite = XD;
616
- break;
617
-
618
- default :
619
- if (c < 0x80) {
620
- writer.write(c);
621
- } else {
622
- UtfHelpper.writeCharToUtf8(c, writer);
623
- }
624
- continue;
625
- }
626
- writer.write(toWrite);
607
+ writer.write(END_COMM);
608
+ if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
609
+ writer.write('\n');
610
+ }
611
+ }
612
+
613
+ /**
614
+ * Outputs a Text of CDATA section to the internal Writer.
615
+ *
616
+ * @param text
617
+ * @param writer writer where to write the things
618
+ * @throws IOException
619
+ */
620
+ protected static final void
621
+ outputTextToWriter(
622
+ final String text, final OutputStream writer
623
+ ) throws IOException
624
+ {
625
+ final int length = text.length();
626
+ byte[] toWrite;
627
+ for (int i = 0; i < length; i++) {
628
+ char c = text.charAt(i);
629
+
630
+ switch (c) {
631
+
632
+ case '&' :
633
+ toWrite = AMP;
634
+ break;
635
+
636
+ case '<' :
637
+ toWrite = LT;
638
+ break;
639
+
640
+ case '>' :
641
+ toWrite = GT;
642
+ break;
643
+
644
+ case 0xD :
645
+ toWrite = XD;
646
+ break;
647
+
648
+ default :
649
+ if (c < 0x80) {
650
+ writer.write(c);
651
+ } else {
652
+ UtfHelpper.writeCharToUtf8(c, writer);
627
653
  }
654
+ continue;
655
+ }
656
+ writer.write(toWrite);
628
657
  }
658
+ }
629
659
 
630
660
  }