nokogiri 1.6.1-java → 1.6.2-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.
- checksums.yaml +4 -4
- data/.editorconfig +17 -0
- data/.travis.yml +6 -6
- data/CHANGELOG.ja.rdoc +61 -8
- data/CHANGELOG.rdoc +58 -3
- data/Gemfile +3 -3
- data/Manifest.txt +57 -1
- data/README.ja.rdoc +22 -17
- data/README.rdoc +23 -18
- data/ROADMAP.md +1 -2
- data/Rakefile +162 -58
- data/build_all +56 -31
- data/dependencies.yml +3 -3
- data/ext/java/nokogiri/NokogiriService.java +9 -5
- data/ext/java/nokogiri/XmlDocument.java +95 -54
- data/ext/java/nokogiri/XmlNode.java +93 -42
- data/ext/java/nokogiri/XmlReader.java +1 -1
- data/ext/java/nokogiri/XmlSaxParserContext.java +33 -0
- data/ext/java/nokogiri/XmlSchema.java +4 -2
- data/ext/java/nokogiri/XmlXpathContext.java +118 -76
- data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +20 -0
- data/ext/java/nokogiri/internals/NokogiriHandler.java +3 -10
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +40 -23
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +59 -54
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +1 -1
- data/ext/java/nokogiri/internals/ParserContext.java +1 -4
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +6 -2
- data/ext/java/nokogiri/internals/c14n/AttrCompare.java +119 -0
- data/ext/java/nokogiri/internals/c14n/C14nHelper.java +159 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +37 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +93 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +252 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +639 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +38 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +38 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +368 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +295 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +40 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +44 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +44 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +43 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +630 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +173 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +76 -0
- data/ext/java/nokogiri/internals/c14n/Constants.java +42 -0
- data/ext/java/nokogiri/internals/c14n/ElementProxy.java +293 -0
- data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +93 -0
- data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +79 -0
- data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +165 -0
- data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +76 -0
- data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +402 -0
- data/ext/java/nokogiri/internals/c14n/NodeFilter.java +51 -0
- data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +179 -0
- data/ext/java/nokogiri/internals/c14n/XMLUtils.java +507 -0
- data/ext/nokogiri/extconf.rb +429 -128
- data/ext/nokogiri/html_document.c +2 -2
- data/ext/nokogiri/nokogiri.c +6 -1
- data/ext/nokogiri/xml_document.c +5 -4
- data/ext/nokogiri/xml_node.c +76 -7
- data/ext/nokogiri/xml_reader.c +1 -1
- data/ext/nokogiri/xml_sax_parser_context.c +40 -0
- data/ext/nokogiri/xml_syntax_error.c +10 -5
- data/ext/nokogiri/xml_syntax_error.h +1 -1
- data/ext/nokogiri/xml_xpath_context.c +2 -14
- data/ext/nokogiri/xslt_stylesheet.c +1 -1
- data/lib/nokogiri.rb +31 -22
- data/lib/nokogiri/css/node.rb +0 -50
- data/lib/nokogiri/css/parser.rb +213 -218
- data/lib/nokogiri/css/parser.y +21 -30
- data/lib/nokogiri/css/xpath_visitor.rb +62 -14
- data/lib/nokogiri/html/document.rb +97 -18
- data/lib/nokogiri/html/sax/parser.rb +2 -2
- data/lib/nokogiri/nokogiri.jar +0 -0
- data/lib/nokogiri/version.rb +1 -1
- data/lib/nokogiri/xml/builder.rb +1 -1
- data/lib/nokogiri/xml/document.rb +2 -2
- data/lib/nokogiri/xml/dtd.rb +10 -0
- data/lib/nokogiri/xml/node.rb +26 -1
- data/lib/nokogiri/xml/sax/parser.rb +1 -1
- data/ports/patches/libxml2/0001-Fix-parser-local-buffers-size-problems.patch +265 -0
- data/ports/patches/libxml2/0002-Fix-entities-local-buffers-size-problems.patch +102 -0
- data/ports/patches/libxml2/0003-Fix-an-error-in-previous-commit.patch +26 -0
- data/ports/patches/libxml2/0004-Fix-potential-out-of-bound-access.patch +26 -0
- data/ports/patches/libxml2/0005-Detect-excessive-entities-expansion-upon-replacement.patch +158 -0
- data/ports/patches/libxml2/0006-Do-not-fetch-external-parsed-entities.patch +78 -0
- data/ports/patches/libxml2/0007-Enforce-XML_PARSER_EOF-state-handling-through-the-pa.patch +480 -0
- data/ports/patches/libxml2/0008-Improve-handling-of-xmlStopParser.patch +315 -0
- data/ports/patches/libxml2/0009-Fix-a-couple-of-return-without-value.patch +37 -0
- data/ports/patches/libxslt/0001-Adding-doc-update-related-to-1.1.28.patch +222 -0
- data/ports/patches/libxslt/0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch +53 -0
- data/ports/patches/libxslt/0003-Initialize-pseudo-random-number-generator-with-curre.patch +60 -0
- data/ports/patches/libxslt/0004-EXSLT-function-str-replace-is-broken-as-is.patch +42 -0
- data/ports/patches/libxslt/0006-Fix-str-padding-to-work-with-UTF-8-strings.patch +164 -0
- data/ports/patches/libxslt/0007-Separate-function-for-predicate-matching-in-patterns.patch +587 -0
- data/ports/patches/libxslt/0008-Fix-direct-pattern-matching.patch +80 -0
- data/ports/patches/libxslt/0009-Fix-certain-patterns-with-predicates.patch +185 -0
- data/ports/patches/libxslt/0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch +126 -0
- data/ports/patches/libxslt/0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch +25 -0
- data/ports/patches/libxslt/0014-Fix-for-bug-436589.patch +43 -0
- data/ports/patches/libxslt/0015-Fix-mkdir-for-mingw.patch +41 -0
- data/suppressions/README.txt +1 -0
- data/suppressions/nokogiri_ree-1.8.7.358.supp +61 -0
- data/suppressions/nokogiri_ruby-1.8.7.370.supp +0 -0
- data/suppressions/nokogiri_ruby-1.9.2.320.supp +28 -0
- data/suppressions/nokogiri_ruby-1.9.3.327.supp +28 -0
- data/test/css/test_nthiness.rb +65 -2
- data/test/css/test_parser.rb +27 -10
- data/test/css/test_tokenizer.rb +1 -1
- data/test/css/test_xpath_visitor.rb +6 -1
- data/test/files/atom.xml +344 -0
- data/test/files/shift_jis_no_charset.html +9 -0
- data/test/helper.rb +10 -0
- data/test/html/test_document.rb +74 -7
- data/test/html/test_document_encoding.rb +10 -0
- data/test/html/test_document_fragment.rb +9 -3
- data/test/namespaces/test_namespaces_aliased_default.rb +24 -0
- data/test/namespaces/test_namespaces_in_cloned_doc.rb +31 -0
- data/test/namespaces/test_namespaces_preservation.rb +31 -0
- data/test/test_nokogiri.rb +6 -0
- data/test/test_reader.rb +7 -4
- data/test/test_xslt_transforms.rb +25 -0
- data/test/xml/sax/test_parser.rb +16 -0
- data/test/xml/sax/test_parser_context.rb +9 -0
- data/test/xml/test_builder.rb +9 -0
- data/test/xml/test_c14n.rb +12 -2
- data/test/xml/test_document.rb +66 -0
- data/test/xml/test_document_fragment.rb +5 -0
- data/test/xml/test_dtd.rb +84 -0
- data/test/xml/test_entity_reference.rb +3 -3
- data/test/xml/test_node.rb +21 -3
- data/test/xml/test_node_attributes.rb +17 -0
- data/test/xml/test_schema.rb +26 -0
- data/test/xml/test_text.rb +15 -0
- data/test/xml/test_xpath.rb +87 -0
- data/test_all +3 -3
- metadata +119 -68
- data/tasks/cross_compile.rb +0 -134
@@ -0,0 +1,37 @@
|
|
1
|
+
package nokogiri.internals.c14n;
|
2
|
+
|
3
|
+
import nokogiri.XmlNode;
|
4
|
+
import nokogiri.internals.NokogiriHelpers;
|
5
|
+
|
6
|
+
import org.jruby.runtime.Block;
|
7
|
+
import org.jruby.runtime.ThreadContext;
|
8
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
9
|
+
import org.w3c.dom.Node;
|
10
|
+
|
11
|
+
public class CanonicalFilter {
|
12
|
+
private final Block block;
|
13
|
+
private final ThreadContext context;
|
14
|
+
|
15
|
+
public CanonicalFilter(ThreadContext context, Block block) {
|
16
|
+
this.context = context;
|
17
|
+
this.block = block;
|
18
|
+
}
|
19
|
+
|
20
|
+
public boolean includeNodes(Node currentNode, Node parentNode) {
|
21
|
+
if (block == null || !block.isGiven())
|
22
|
+
return true;
|
23
|
+
|
24
|
+
IRubyObject current = NokogiriHelpers.getCachedNodeOrCreate(context.getRuntime(), currentNode);
|
25
|
+
IRubyObject parent = NokogiriHelpers.getCachedNodeOrCreate(context.getRuntime(), parentNode);
|
26
|
+
|
27
|
+
if (parent.isNil()) {
|
28
|
+
IRubyObject doc = ((XmlNode) current).document(context);
|
29
|
+
boolean returnValue = block.call(context, current, doc).isTrue();
|
30
|
+
block.call(context, doc, context.nil);
|
31
|
+
return returnValue;
|
32
|
+
}
|
33
|
+
|
34
|
+
return block.call(context, current, parent).isTrue();
|
35
|
+
}
|
36
|
+
|
37
|
+
}
|
@@ -0,0 +1,93 @@
|
|
1
|
+
/**
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
4
|
+
* distributed with this work for additional information
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
7
|
+
* "License"); you may not use this file except in compliance
|
8
|
+
* with the License. You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
13
|
+
* software distributed under the License is distributed on an
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
* KIND, either express or implied. See the License for the
|
16
|
+
* specific language governing permissions and limitations
|
17
|
+
* under the License.
|
18
|
+
*/
|
19
|
+
package nokogiri.internals.c14n;
|
20
|
+
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Class CanonicalizationException
|
24
|
+
*
|
25
|
+
* @author Christian Geuer-Pollmann
|
26
|
+
*/
|
27
|
+
public class CanonicalizationException extends Exception {
|
28
|
+
|
29
|
+
/**
|
30
|
+
*
|
31
|
+
*/
|
32
|
+
private static final long serialVersionUID = 1L;
|
33
|
+
|
34
|
+
/**
|
35
|
+
* Constructor CanonicalizationException
|
36
|
+
*
|
37
|
+
*/
|
38
|
+
public CanonicalizationException() {
|
39
|
+
super();
|
40
|
+
}
|
41
|
+
|
42
|
+
/**
|
43
|
+
* Constructor CanonicalizationException
|
44
|
+
*
|
45
|
+
* @param message
|
46
|
+
*/
|
47
|
+
public CanonicalizationException(String message) {
|
48
|
+
super(message);
|
49
|
+
}
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Constructor CanonicalizationException
|
53
|
+
*
|
54
|
+
* @param message
|
55
|
+
* @param rootCause
|
56
|
+
*/
|
57
|
+
public CanonicalizationException(Exception rootCause) {
|
58
|
+
super(rootCause);
|
59
|
+
}
|
60
|
+
|
61
|
+
/**
|
62
|
+
* Constructor CanonicalizationException
|
63
|
+
*
|
64
|
+
* @param msgID
|
65
|
+
* @param exArgs
|
66
|
+
*/
|
67
|
+
public CanonicalizationException(String message, Object... exArgs) {
|
68
|
+
super(C14nHelper.getErrorMessage(message, exArgs));
|
69
|
+
}
|
70
|
+
|
71
|
+
/**
|
72
|
+
* Constructor CanonicalizationException
|
73
|
+
*
|
74
|
+
* @param message
|
75
|
+
* @param rootCause
|
76
|
+
*/
|
77
|
+
public CanonicalizationException(String message, Exception rootCause) {
|
78
|
+
super(message, rootCause);
|
79
|
+
}
|
80
|
+
|
81
|
+
/**
|
82
|
+
* Constructor CanonicalizationException
|
83
|
+
*
|
84
|
+
* @param msgID
|
85
|
+
* @param exArgs
|
86
|
+
* @param originalException
|
87
|
+
*/
|
88
|
+
public CanonicalizationException(String message, Exception rootCause, Object... exArgs) {
|
89
|
+
super(C14nHelper.getErrorMessage(message, exArgs), rootCause);
|
90
|
+
}
|
91
|
+
|
92
|
+
|
93
|
+
}
|
@@ -0,0 +1,252 @@
|
|
1
|
+
/**
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
4
|
+
* distributed with this work for additional information
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
7
|
+
* "License"); you may not use this file except in compliance
|
8
|
+
* with the License. You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
13
|
+
* software distributed under the License is distributed on an
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
* KIND, either express or implied. See the License for the
|
16
|
+
* specific language governing permissions and limitations
|
17
|
+
* under the License.
|
18
|
+
*/
|
19
|
+
package nokogiri.internals.c14n;
|
20
|
+
|
21
|
+
import java.io.OutputStream;
|
22
|
+
import java.util.Map;
|
23
|
+
import java.util.concurrent.ConcurrentHashMap;
|
24
|
+
|
25
|
+
import org.w3c.dom.Node;
|
26
|
+
|
27
|
+
/**
|
28
|
+
*
|
29
|
+
* @author Christian Geuer-Pollmann
|
30
|
+
*/
|
31
|
+
public class Canonicalizer {
|
32
|
+
|
33
|
+
/** The output encoding of canonicalized data */
|
34
|
+
public static final String ENCODING = "UTF8";
|
35
|
+
|
36
|
+
/**
|
37
|
+
* XPath Expression for selecting every node and continuous comments joined
|
38
|
+
* in only one node
|
39
|
+
*/
|
40
|
+
public static final String XPATH_C14N_WITH_COMMENTS_SINGLE_NODE =
|
41
|
+
"(.//. | .//@* | .//namespace::*)";
|
42
|
+
|
43
|
+
/**
|
44
|
+
* The URL defined in XML-SEC Rec for inclusive c14n <b>without</b> comments.
|
45
|
+
*/
|
46
|
+
public static final String ALGO_ID_C14N_OMIT_COMMENTS =
|
47
|
+
"http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
|
48
|
+
/**
|
49
|
+
* The URL defined in XML-SEC Rec for inclusive c14n <b>with</b> comments.
|
50
|
+
*/
|
51
|
+
public static final String ALGO_ID_C14N_WITH_COMMENTS =
|
52
|
+
ALGO_ID_C14N_OMIT_COMMENTS + "#WithComments";
|
53
|
+
/**
|
54
|
+
* The URL defined in XML-SEC Rec for exclusive c14n <b>without</b> comments.
|
55
|
+
*/
|
56
|
+
public static final String ALGO_ID_C14N_EXCL_OMIT_COMMENTS =
|
57
|
+
"http://www.w3.org/2001/10/xml-exc-c14n#";
|
58
|
+
/**
|
59
|
+
* The URL defined in XML-SEC Rec for exclusive c14n <b>with</b> comments.
|
60
|
+
*/
|
61
|
+
public static final String ALGO_ID_C14N_EXCL_WITH_COMMENTS =
|
62
|
+
ALGO_ID_C14N_EXCL_OMIT_COMMENTS + "WithComments";
|
63
|
+
/**
|
64
|
+
* The URI for inclusive c14n 1.1 <b>without</b> comments.
|
65
|
+
*/
|
66
|
+
public static final String ALGO_ID_C14N11_OMIT_COMMENTS =
|
67
|
+
"http://www.w3.org/2006/12/xml-c14n11";
|
68
|
+
/**
|
69
|
+
* The URI for inclusive c14n 1.1 <b>with</b> comments.
|
70
|
+
*/
|
71
|
+
public static final String ALGO_ID_C14N11_WITH_COMMENTS =
|
72
|
+
ALGO_ID_C14N11_OMIT_COMMENTS + "#WithComments";
|
73
|
+
/**
|
74
|
+
* Non-standard algorithm to serialize the physical representation for XML Encryption
|
75
|
+
*/
|
76
|
+
public static final String ALGO_ID_C14N_PHYSICAL =
|
77
|
+
"http://santuario.apache.org/c14n/physical";
|
78
|
+
|
79
|
+
private static Map<String, Class<? extends CanonicalizerSpi>> canonicalizerHash = null;
|
80
|
+
|
81
|
+
private final CanonicalizerSpi canonicalizerSpi;
|
82
|
+
|
83
|
+
/**
|
84
|
+
* Constructor Canonicalizer
|
85
|
+
*
|
86
|
+
* @param algorithmURI
|
87
|
+
* @throws InvalidCanonicalizerException
|
88
|
+
*/
|
89
|
+
private Canonicalizer(String algorithmURI) throws CanonicalizationException {
|
90
|
+
try {
|
91
|
+
Class<? extends CanonicalizerSpi> implementingClass =
|
92
|
+
canonicalizerHash.get(algorithmURI);
|
93
|
+
|
94
|
+
canonicalizerSpi = implementingClass.newInstance();
|
95
|
+
canonicalizerSpi.reset = true;
|
96
|
+
} catch (Exception e) {
|
97
|
+
Object exArgs[] = { algorithmURI };
|
98
|
+
throw new CanonicalizationException(
|
99
|
+
"signature.Canonicalizer.UnknownCanonicalizer", exArgs, e
|
100
|
+
);
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* Method getInstance
|
106
|
+
*
|
107
|
+
* @param algorithmURI
|
108
|
+
* @return a Canonicalizer instance ready for the job
|
109
|
+
* @throws InvalidCanonicalizerException
|
110
|
+
*/
|
111
|
+
public static final Canonicalizer getInstance(String algorithmURI)
|
112
|
+
throws CanonicalizationException {
|
113
|
+
if (canonicalizerHash == null) {
|
114
|
+
canonicalizerHash = new ConcurrentHashMap<String, Class<? extends CanonicalizerSpi>>();
|
115
|
+
Canonicalizer.registerDefaultAlgorithms();
|
116
|
+
}
|
117
|
+
return new Canonicalizer(algorithmURI);
|
118
|
+
}
|
119
|
+
|
120
|
+
/**
|
121
|
+
* Method register
|
122
|
+
*
|
123
|
+
* @param algorithmURI
|
124
|
+
* @param implementingClass
|
125
|
+
* @throws CanonicalizationException
|
126
|
+
*/
|
127
|
+
@SuppressWarnings("unchecked")
|
128
|
+
public static void register(String algorithmURI, String implementingClass)
|
129
|
+
throws CanonicalizationException, ClassNotFoundException {
|
130
|
+
// check whether URI is already registered
|
131
|
+
Class<? extends CanonicalizerSpi> registeredClass =
|
132
|
+
canonicalizerHash.get(algorithmURI);
|
133
|
+
|
134
|
+
if (registeredClass != null) {
|
135
|
+
Object exArgs[] = { algorithmURI, registeredClass };
|
136
|
+
throw new CanonicalizationException("algorithm.alreadyRegistered", exArgs);
|
137
|
+
}
|
138
|
+
|
139
|
+
canonicalizerHash.put(
|
140
|
+
algorithmURI, (Class<? extends CanonicalizerSpi>)Class.forName(implementingClass)
|
141
|
+
);
|
142
|
+
}
|
143
|
+
|
144
|
+
/**
|
145
|
+
* Method register
|
146
|
+
*
|
147
|
+
* @param algorithmURI
|
148
|
+
* @param implementingClass
|
149
|
+
* @throws CanonicalizationException
|
150
|
+
*/
|
151
|
+
public static void register(String algorithmURI, Class<? extends CanonicalizerSpi> implementingClass)
|
152
|
+
throws CanonicalizationException, ClassNotFoundException {
|
153
|
+
// check whether URI is already registered
|
154
|
+
Class<? extends CanonicalizerSpi> registeredClass = canonicalizerHash.get(algorithmURI);
|
155
|
+
|
156
|
+
if (registeredClass != null) {
|
157
|
+
Object exArgs[] = { algorithmURI, registeredClass };
|
158
|
+
throw new CanonicalizationException("algorithm.alreadyRegistered", exArgs);
|
159
|
+
}
|
160
|
+
|
161
|
+
canonicalizerHash.put(algorithmURI, implementingClass);
|
162
|
+
}
|
163
|
+
|
164
|
+
/**
|
165
|
+
* This method registers the default algorithms.
|
166
|
+
*/
|
167
|
+
private static void registerDefaultAlgorithms() {
|
168
|
+
canonicalizerHash.put(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS,
|
169
|
+
Canonicalizer20010315OmitComments.class);
|
170
|
+
canonicalizerHash.put(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS,
|
171
|
+
Canonicalizer20010315WithComments.class);
|
172
|
+
canonicalizerHash.put(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS,
|
173
|
+
Canonicalizer20010315ExclOmitComments.class);
|
174
|
+
canonicalizerHash.put(Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS,
|
175
|
+
Canonicalizer20010315ExclWithComments.class);
|
176
|
+
canonicalizerHash.put(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS,
|
177
|
+
Canonicalizer11_OmitComments.class);
|
178
|
+
canonicalizerHash.put(Canonicalizer.ALGO_ID_C14N11_WITH_COMMENTS,
|
179
|
+
Canonicalizer11_WithComments.class);
|
180
|
+
canonicalizerHash.put(Canonicalizer.ALGO_ID_C14N_PHYSICAL,
|
181
|
+
CanonicalizerPhysical.class);
|
182
|
+
}
|
183
|
+
|
184
|
+
/**
|
185
|
+
* Method getURI
|
186
|
+
*
|
187
|
+
* @return the URI defined for this c14n instance.
|
188
|
+
*/
|
189
|
+
public final String getURI() {
|
190
|
+
return canonicalizerSpi.engineGetURI();
|
191
|
+
}
|
192
|
+
|
193
|
+
/**
|
194
|
+
* Method getIncludeComments
|
195
|
+
*
|
196
|
+
* @return true if the c14n respect the comments.
|
197
|
+
*/
|
198
|
+
public boolean getIncludeComments() {
|
199
|
+
return canonicalizerSpi.engineGetIncludeComments();
|
200
|
+
}
|
201
|
+
|
202
|
+
/**
|
203
|
+
* Canonicalizes the subtree rooted by <CODE>node</CODE>.
|
204
|
+
*
|
205
|
+
* @param node The node to canonicalize
|
206
|
+
* @return the result of the c14n.
|
207
|
+
*
|
208
|
+
* @throws CanonicalizationException
|
209
|
+
*/
|
210
|
+
public byte[] canonicalizeSubtree(Node node, CanonicalFilter filter) throws CanonicalizationException {
|
211
|
+
return canonicalizerSpi.engineCanonicalizeSubTree(node, filter);
|
212
|
+
}
|
213
|
+
|
214
|
+
/**
|
215
|
+
* Canonicalizes the subtree rooted by <CODE>node</CODE>.
|
216
|
+
*
|
217
|
+
* @param node
|
218
|
+
* @param inclusiveNamespaces
|
219
|
+
* @return the result of the c14n.
|
220
|
+
* @throws CanonicalizationException
|
221
|
+
*/
|
222
|
+
public byte[] canonicalizeSubtree(Node node, String inclusiveNamespaces, CanonicalFilter filter)
|
223
|
+
throws CanonicalizationException {
|
224
|
+
return canonicalizerSpi.engineCanonicalizeSubTree(node, inclusiveNamespaces, filter);
|
225
|
+
}
|
226
|
+
|
227
|
+
/**
|
228
|
+
* Sets the writer where the canonicalization ends. ByteArrayOutputStream
|
229
|
+
* if none is set.
|
230
|
+
* @param os
|
231
|
+
*/
|
232
|
+
public void setWriter(OutputStream os) {
|
233
|
+
canonicalizerSpi.setWriter(os);
|
234
|
+
}
|
235
|
+
|
236
|
+
/**
|
237
|
+
* Returns the name of the implementing {@link CanonicalizerSpi} class
|
238
|
+
*
|
239
|
+
* @return the name of the implementing {@link CanonicalizerSpi} class
|
240
|
+
*/
|
241
|
+
public String getImplementingCanonicalizerClass() {
|
242
|
+
return canonicalizerSpi.getClass().getName();
|
243
|
+
}
|
244
|
+
|
245
|
+
/**
|
246
|
+
* Set the canonicalizer behaviour to not reset.
|
247
|
+
*/
|
248
|
+
public void notReset() {
|
249
|
+
canonicalizerSpi.reset = false;
|
250
|
+
}
|
251
|
+
|
252
|
+
}
|
@@ -0,0 +1,639 @@
|
|
1
|
+
/**
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
4
|
+
* distributed with this work for additional information
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
7
|
+
* "License"); you may not use this file except in compliance
|
8
|
+
* with the License. You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
13
|
+
* software distributed under the License is distributed on an
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
* KIND, either express or implied. See the License for the
|
16
|
+
* specific language governing permissions and limitations
|
17
|
+
* under the License.
|
18
|
+
*/
|
19
|
+
package nokogiri.internals.c14n;
|
20
|
+
|
21
|
+
import java.net.URI;
|
22
|
+
import java.net.URISyntaxException;
|
23
|
+
import java.util.ArrayList;
|
24
|
+
import java.util.Collection;
|
25
|
+
import java.util.HashMap;
|
26
|
+
import java.util.Iterator;
|
27
|
+
import java.util.List;
|
28
|
+
import java.util.Map;
|
29
|
+
import java.util.SortedSet;
|
30
|
+
import java.util.TreeSet;
|
31
|
+
|
32
|
+
|
33
|
+
import org.w3c.dom.Attr;
|
34
|
+
import org.w3c.dom.Element;
|
35
|
+
import org.w3c.dom.NamedNodeMap;
|
36
|
+
import org.w3c.dom.Node;
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Implements <A HREF="http://www.w3.org/TR/2008/PR-xml-c14n11-20080129/">
|
40
|
+
* Canonical XML Version 1.1</A>, a W3C Proposed Recommendation from 29
|
41
|
+
* January 2008.
|
42
|
+
*
|
43
|
+
* @author Sean Mullan
|
44
|
+
* @author Raul Benito
|
45
|
+
*/
|
46
|
+
public abstract class Canonicalizer11 extends CanonicalizerBase {
|
47
|
+
|
48
|
+
private static final String XMLNS_URI = Constants.NamespaceSpecNS;
|
49
|
+
private static final String XML_LANG_URI = Constants.XML_LANG_SPACE_SpecNS;
|
50
|
+
private final SortedSet<Attr> result = new TreeSet<Attr>(COMPARE);
|
51
|
+
|
52
|
+
private boolean firstCall = true;
|
53
|
+
|
54
|
+
private static class XmlAttrStack {
|
55
|
+
static class XmlsStackElement {
|
56
|
+
int level;
|
57
|
+
boolean rendered = false;
|
58
|
+
List<Attr> nodes = new ArrayList<Attr>();
|
59
|
+
};
|
60
|
+
|
61
|
+
int currentLevel = 0;
|
62
|
+
int lastlevel = 0;
|
63
|
+
XmlsStackElement cur;
|
64
|
+
List<XmlsStackElement> levels = new ArrayList<XmlsStackElement>();
|
65
|
+
|
66
|
+
void push(int level) {
|
67
|
+
currentLevel = level;
|
68
|
+
if (currentLevel == -1) {
|
69
|
+
return;
|
70
|
+
}
|
71
|
+
cur = null;
|
72
|
+
while (lastlevel >= currentLevel) {
|
73
|
+
levels.remove(levels.size() - 1);
|
74
|
+
int newSize = levels.size();
|
75
|
+
if (newSize == 0) {
|
76
|
+
lastlevel = 0;
|
77
|
+
return;
|
78
|
+
}
|
79
|
+
lastlevel = (levels.get(newSize - 1)).level;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
void addXmlnsAttr(Attr n) {
|
84
|
+
if (cur == null) {
|
85
|
+
cur = new XmlsStackElement();
|
86
|
+
cur.level = currentLevel;
|
87
|
+
levels.add(cur);
|
88
|
+
lastlevel = currentLevel;
|
89
|
+
}
|
90
|
+
cur.nodes.add(n);
|
91
|
+
}
|
92
|
+
|
93
|
+
void getXmlnsAttr(Collection<Attr> col) throws CanonicalizationException {
|
94
|
+
int size = levels.size() - 1;
|
95
|
+
if (cur == null) {
|
96
|
+
cur = new XmlsStackElement();
|
97
|
+
cur.level = currentLevel;
|
98
|
+
lastlevel = currentLevel;
|
99
|
+
levels.add(cur);
|
100
|
+
}
|
101
|
+
boolean parentRendered = false;
|
102
|
+
XmlsStackElement e = null;
|
103
|
+
if (size == -1) {
|
104
|
+
parentRendered = true;
|
105
|
+
} else {
|
106
|
+
e = levels.get(size);
|
107
|
+
if (e.rendered && e.level + 1 == currentLevel) {
|
108
|
+
parentRendered = true;
|
109
|
+
}
|
110
|
+
}
|
111
|
+
if (parentRendered) {
|
112
|
+
col.addAll(cur.nodes);
|
113
|
+
cur.rendered = true;
|
114
|
+
return;
|
115
|
+
}
|
116
|
+
|
117
|
+
Map<String, Attr> loa = new HashMap<String, Attr>();
|
118
|
+
List<Attr> baseAttrs = new ArrayList<Attr>();
|
119
|
+
boolean successiveOmitted = true;
|
120
|
+
for (; size >= 0; size--) {
|
121
|
+
e = levels.get(size);
|
122
|
+
if (e.rendered) {
|
123
|
+
successiveOmitted = false;
|
124
|
+
}
|
125
|
+
Iterator<Attr> it = e.nodes.iterator();
|
126
|
+
while (it.hasNext() && successiveOmitted) {
|
127
|
+
Attr n = it.next();
|
128
|
+
if (n.getLocalName().equals("base") && !e.rendered) {
|
129
|
+
baseAttrs.add(n);
|
130
|
+
} else if (!loa.containsKey(n.getName())) {
|
131
|
+
loa.put(n.getName(), n);
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}
|
135
|
+
if (!baseAttrs.isEmpty()) {
|
136
|
+
Iterator<Attr> it = col.iterator();
|
137
|
+
String base = null;
|
138
|
+
Attr baseAttr = null;
|
139
|
+
while (it.hasNext()) {
|
140
|
+
Attr n = it.next();
|
141
|
+
if (n.getLocalName().equals("base")) {
|
142
|
+
base = n.getValue();
|
143
|
+
baseAttr = n;
|
144
|
+
break;
|
145
|
+
}
|
146
|
+
}
|
147
|
+
it = baseAttrs.iterator();
|
148
|
+
while (it.hasNext()) {
|
149
|
+
Attr n = it.next();
|
150
|
+
if (base == null) {
|
151
|
+
base = n.getValue();
|
152
|
+
baseAttr = n;
|
153
|
+
} else {
|
154
|
+
try {
|
155
|
+
base = joinURI(n.getValue(), base);
|
156
|
+
} catch (URISyntaxException e1) {
|
157
|
+
throw new CanonicalizationException(e1);
|
158
|
+
}
|
159
|
+
}
|
160
|
+
}
|
161
|
+
if (base != null && base.length() != 0) {
|
162
|
+
baseAttr.setValue(base);
|
163
|
+
col.add(baseAttr);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
cur.rendered = true;
|
168
|
+
col.addAll(loa.values());
|
169
|
+
}
|
170
|
+
};
|
171
|
+
|
172
|
+
private final XmlAttrStack xmlattrStack = new XmlAttrStack();
|
173
|
+
|
174
|
+
/**
|
175
|
+
* Constructor Canonicalizer11
|
176
|
+
*
|
177
|
+
* @param includeComments
|
178
|
+
*/
|
179
|
+
public Canonicalizer11(boolean includeComments) {
|
180
|
+
super(includeComments);
|
181
|
+
}
|
182
|
+
|
183
|
+
/**
|
184
|
+
* Always throws a CanonicalizationException because this is inclusive c14n.
|
185
|
+
*
|
186
|
+
* @param rootNode
|
187
|
+
* @param inclusiveNamespaces
|
188
|
+
* @return none it always fails
|
189
|
+
* @throws CanonicalizationException
|
190
|
+
*/
|
191
|
+
public byte[] engineCanonicalizeSubTree(
|
192
|
+
Node rootNode, String inclusiveNamespaces, CanonicalFilter filter
|
193
|
+
) throws CanonicalizationException {
|
194
|
+
throw new CanonicalizationException("c14n.Canonicalizer.UnsupportedOperation");
|
195
|
+
}
|
196
|
+
|
197
|
+
/**
|
198
|
+
* Returns the Attr[]s to be output for the given element.
|
199
|
+
* <br>
|
200
|
+
* The code of this method is a copy of {@link #handleAttributes(Element,
|
201
|
+
* NameSpaceSymbTable)},
|
202
|
+
* whereas it takes into account that subtree-c14n is -- well --
|
203
|
+
* subtree-based.
|
204
|
+
* So if the element in question isRoot of c14n, it's parent is not in the
|
205
|
+
* node set, as well as all other ancestors.
|
206
|
+
*
|
207
|
+
* @param element
|
208
|
+
* @param ns
|
209
|
+
* @return the Attr[]s to be output
|
210
|
+
* @throws CanonicalizationException
|
211
|
+
* @throws URISyntaxException
|
212
|
+
*/
|
213
|
+
@Override
|
214
|
+
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
|
215
|
+
throws CanonicalizationException {
|
216
|
+
if (!element.hasAttributes() && !firstCall) {
|
217
|
+
return null;
|
218
|
+
}
|
219
|
+
// result will contain the attrs which have to be output
|
220
|
+
final SortedSet<Attr> result = this.result;
|
221
|
+
result.clear();
|
222
|
+
|
223
|
+
if (element.hasAttributes()) {
|
224
|
+
NamedNodeMap attrs = element.getAttributes();
|
225
|
+
int attrsLength = attrs.getLength();
|
226
|
+
|
227
|
+
for (int i = 0; i < attrsLength; i++) {
|
228
|
+
Attr attribute = (Attr) attrs.item(i);
|
229
|
+
String NUri = attribute.getNamespaceURI();
|
230
|
+
String NName = attribute.getLocalName();
|
231
|
+
String NValue = attribute.getValue();
|
232
|
+
|
233
|
+
if (!XMLNS_URI.equals(NUri)) {
|
234
|
+
// It's not a namespace attr node. Add to the result and continue.
|
235
|
+
result.add(attribute);
|
236
|
+
} else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
|
237
|
+
// The default mapping for xml must not be output.
|
238
|
+
Node n = ns.addMappingAndRender(NName, NValue, attribute);
|
239
|
+
|
240
|
+
if (n != null) {
|
241
|
+
// Render the ns definition
|
242
|
+
result.add((Attr)n);
|
243
|
+
if (C14nHelper.namespaceIsRelative(attribute)) {
|
244
|
+
Object exArgs[] = {element.getTagName(), NName, attribute.getNodeValue()};
|
245
|
+
throw new CanonicalizationException(
|
246
|
+
"c14n.Canonicalizer.RelativeNamespace", exArgs
|
247
|
+
);
|
248
|
+
}
|
249
|
+
}
|
250
|
+
}
|
251
|
+
}
|
252
|
+
}
|
253
|
+
|
254
|
+
if (firstCall) {
|
255
|
+
// It is the first node of the subtree
|
256
|
+
// Obtain all the namespaces defined in the parents, and added to the output.
|
257
|
+
ns.getUnrenderedNodes(result);
|
258
|
+
// output the attributes in the xml namespace.
|
259
|
+
xmlattrStack.getXmlnsAttr(result);
|
260
|
+
firstCall = false;
|
261
|
+
}
|
262
|
+
|
263
|
+
return result.iterator();
|
264
|
+
}
|
265
|
+
|
266
|
+
/**
|
267
|
+
* Returns the Attr[]s to be output for the given element.
|
268
|
+
* <br>
|
269
|
+
* IMPORTANT: This method expects to work on a modified DOM tree, i.e. a
|
270
|
+
* DOM which has been prepared using
|
271
|
+
* {@link nokogiri.internals.c14n.security.utils.XMLUtils#circumventBug2650(
|
272
|
+
* org.w3c.dom.Document)}.
|
273
|
+
*
|
274
|
+
* @param element
|
275
|
+
* @param ns
|
276
|
+
* @return the Attr[]s to be output
|
277
|
+
* @throws CanonicalizationException
|
278
|
+
*/
|
279
|
+
@Override
|
280
|
+
protected Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns)
|
281
|
+
throws CanonicalizationException {
|
282
|
+
// result will contain the attrs which have to be output
|
283
|
+
xmlattrStack.push(ns.getLevel());
|
284
|
+
boolean isRealVisible = isVisibleDO(element, ns.getLevel()) == 1;
|
285
|
+
final SortedSet<Attr> result = this.result;
|
286
|
+
result.clear();
|
287
|
+
|
288
|
+
if (element.hasAttributes()) {
|
289
|
+
NamedNodeMap attrs = element.getAttributes();
|
290
|
+
int attrsLength = attrs.getLength();
|
291
|
+
|
292
|
+
for (int i = 0; i < attrsLength; i++) {
|
293
|
+
Attr attribute = (Attr) attrs.item(i);
|
294
|
+
String NUri = attribute.getNamespaceURI();
|
295
|
+
String NName = attribute.getLocalName();
|
296
|
+
String NValue = attribute.getValue();
|
297
|
+
|
298
|
+
if (!XMLNS_URI.equals(NUri)) {
|
299
|
+
//A non namespace definition node.
|
300
|
+
if (XML_LANG_URI.equals(NUri)) {
|
301
|
+
if (NName.equals("id")) {
|
302
|
+
if (isRealVisible) {
|
303
|
+
// treat xml:id like any other attribute
|
304
|
+
// (emit it, but don't inherit it)
|
305
|
+
result.add(attribute);
|
306
|
+
}
|
307
|
+
} else {
|
308
|
+
xmlattrStack.addXmlnsAttr(attribute);
|
309
|
+
}
|
310
|
+
} else if (isRealVisible) {
|
311
|
+
//The node is visible add the attribute to the list of output attributes.
|
312
|
+
result.add(attribute);
|
313
|
+
}
|
314
|
+
} else if (!XML.equals(NName) || !XML_LANG_URI.equals(NValue)) {
|
315
|
+
/* except omit namespace node with local name xml, which defines
|
316
|
+
* the xml prefix, if its string value is
|
317
|
+
* http://www.w3.org/XML/1998/namespace.
|
318
|
+
*/
|
319
|
+
// add the prefix binding to the ns symb table.
|
320
|
+
if (isVisible(attribute)) {
|
321
|
+
if (isRealVisible || !ns.removeMappingIfRender(NName)) {
|
322
|
+
// The xpath select this node output it if needed.
|
323
|
+
Node n = ns.addMappingAndRender(NName, NValue, attribute);
|
324
|
+
if (n != null) {
|
325
|
+
result.add((Attr)n);
|
326
|
+
if (C14nHelper.namespaceIsRelative(attribute)) {
|
327
|
+
Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() };
|
328
|
+
throw new CanonicalizationException(
|
329
|
+
"c14n.Canonicalizer.RelativeNamespace", exArgs
|
330
|
+
);
|
331
|
+
}
|
332
|
+
}
|
333
|
+
}
|
334
|
+
} else {
|
335
|
+
if (isRealVisible && !XMLNS.equals(NName)) {
|
336
|
+
ns.removeMapping(NName);
|
337
|
+
} else {
|
338
|
+
ns.addMapping(NName, NValue, attribute);
|
339
|
+
}
|
340
|
+
}
|
341
|
+
}
|
342
|
+
}
|
343
|
+
}
|
344
|
+
|
345
|
+
if (isRealVisible) {
|
346
|
+
//The element is visible, handle the xmlns definition
|
347
|
+
Attr xmlns = element.getAttributeNodeNS(XMLNS_URI, XMLNS);
|
348
|
+
Node n = null;
|
349
|
+
if (xmlns == null) {
|
350
|
+
//No xmlns def just get the already defined.
|
351
|
+
n = ns.getMapping(XMLNS);
|
352
|
+
} else if (!isVisible(xmlns)) {
|
353
|
+
//There is a definition but the xmlns is not selected by the xpath.
|
354
|
+
//then xmlns=""
|
355
|
+
n = ns.addMappingAndRender(XMLNS, "", nullNode);
|
356
|
+
}
|
357
|
+
//output the xmlns def if needed.
|
358
|
+
if (n != null) {
|
359
|
+
result.add((Attr)n);
|
360
|
+
}
|
361
|
+
//Float all xml:* attributes of the unselected parent elements to this one.
|
362
|
+
xmlattrStack.getXmlnsAttr(result);
|
363
|
+
ns.getUnrenderedNodes(result);
|
364
|
+
}
|
365
|
+
|
366
|
+
return result.iterator();
|
367
|
+
}
|
368
|
+
|
369
|
+
@Override
|
370
|
+
protected void handleParent(Element e, NameSpaceSymbTable ns) {
|
371
|
+
if (!e.hasAttributes() && e.getNamespaceURI() == null) {
|
372
|
+
return;
|
373
|
+
}
|
374
|
+
xmlattrStack.push(-1);
|
375
|
+
NamedNodeMap attrs = e.getAttributes();
|
376
|
+
int attrsLength = attrs.getLength();
|
377
|
+
for (int i = 0; i < attrsLength; i++) {
|
378
|
+
Attr attribute = (Attr) attrs.item(i);
|
379
|
+
String NName = attribute.getLocalName();
|
380
|
+
String NValue = attribute.getNodeValue();
|
381
|
+
|
382
|
+
if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())) {
|
383
|
+
if (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
|
384
|
+
ns.addMapping(NName, NValue, attribute);
|
385
|
+
}
|
386
|
+
} else if (!"id".equals(NName) && XML_LANG_URI.equals(attribute.getNamespaceURI())) {
|
387
|
+
xmlattrStack.addXmlnsAttr(attribute);
|
388
|
+
}
|
389
|
+
}
|
390
|
+
if (e.getNamespaceURI() != null) {
|
391
|
+
String NName = e.getPrefix();
|
392
|
+
String NValue = e.getNamespaceURI();
|
393
|
+
String Name;
|
394
|
+
if (NName == null || NName.equals("")) {
|
395
|
+
NName = "xmlns";
|
396
|
+
Name = "xmlns";
|
397
|
+
} else {
|
398
|
+
Name = "xmlns:" + NName;
|
399
|
+
}
|
400
|
+
Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name);
|
401
|
+
n.setValue(NValue);
|
402
|
+
ns.addMapping(NName, NValue, n);
|
403
|
+
}
|
404
|
+
}
|
405
|
+
|
406
|
+
private static String joinURI(String baseURI, String relativeURI) throws URISyntaxException {
|
407
|
+
String bscheme = null;
|
408
|
+
String bauthority = null;
|
409
|
+
String bpath = "";
|
410
|
+
String bquery = null;
|
411
|
+
|
412
|
+
// pre-parse the baseURI
|
413
|
+
if (baseURI != null) {
|
414
|
+
if (baseURI.endsWith("..")) {
|
415
|
+
baseURI = baseURI + "/";
|
416
|
+
}
|
417
|
+
URI base = new URI(baseURI);
|
418
|
+
bscheme = base.getScheme();
|
419
|
+
bauthority = base.getAuthority();
|
420
|
+
bpath = base.getPath();
|
421
|
+
bquery = base.getQuery();
|
422
|
+
}
|
423
|
+
|
424
|
+
URI r = new URI(relativeURI);
|
425
|
+
String rscheme = r.getScheme();
|
426
|
+
String rauthority = r.getAuthority();
|
427
|
+
String rpath = r.getPath();
|
428
|
+
String rquery = r.getQuery();
|
429
|
+
|
430
|
+
String tscheme, tauthority, tpath, tquery;
|
431
|
+
if (rscheme != null && rscheme.equals(bscheme)) {
|
432
|
+
rscheme = null;
|
433
|
+
}
|
434
|
+
if (rscheme != null) {
|
435
|
+
tscheme = rscheme;
|
436
|
+
tauthority = rauthority;
|
437
|
+
tpath = removeDotSegments(rpath);
|
438
|
+
tquery = rquery;
|
439
|
+
} else {
|
440
|
+
if (rauthority != null) {
|
441
|
+
tauthority = rauthority;
|
442
|
+
tpath = removeDotSegments(rpath);
|
443
|
+
tquery = rquery;
|
444
|
+
} else {
|
445
|
+
if (rpath.length() == 0) {
|
446
|
+
tpath = bpath;
|
447
|
+
if (rquery != null) {
|
448
|
+
tquery = rquery;
|
449
|
+
} else {
|
450
|
+
tquery = bquery;
|
451
|
+
}
|
452
|
+
} else {
|
453
|
+
if (rpath.startsWith("/")) {
|
454
|
+
tpath = removeDotSegments(rpath);
|
455
|
+
} else {
|
456
|
+
if (bauthority != null && bpath.length() == 0) {
|
457
|
+
tpath = "/" + rpath;
|
458
|
+
} else {
|
459
|
+
int last = bpath.lastIndexOf('/');
|
460
|
+
if (last == -1) {
|
461
|
+
tpath = rpath;
|
462
|
+
} else {
|
463
|
+
tpath = bpath.substring(0, last+1) + rpath;
|
464
|
+
}
|
465
|
+
}
|
466
|
+
tpath = removeDotSegments(tpath);
|
467
|
+
}
|
468
|
+
tquery = rquery;
|
469
|
+
}
|
470
|
+
tauthority = bauthority;
|
471
|
+
}
|
472
|
+
tscheme = bscheme;
|
473
|
+
}
|
474
|
+
return new URI(tscheme, tauthority, tpath, tquery, null).toString();
|
475
|
+
}
|
476
|
+
|
477
|
+
private static String removeDotSegments(String path) {
|
478
|
+
|
479
|
+
// 1. The input buffer is initialized with the now-appended path
|
480
|
+
// components then replace occurrences of "//" in the input buffer
|
481
|
+
// with "/" until no more occurrences of "//" are in the input buffer.
|
482
|
+
String input = path;
|
483
|
+
while (input.indexOf("//") > -1) {
|
484
|
+
input = input.replaceAll("//", "/");
|
485
|
+
}
|
486
|
+
|
487
|
+
// Initialize the output buffer with the empty string.
|
488
|
+
StringBuilder output = new StringBuilder();
|
489
|
+
|
490
|
+
// If the input buffer starts with a root slash "/" then move this
|
491
|
+
// character to the output buffer.
|
492
|
+
if (input.charAt(0) == '/') {
|
493
|
+
output.append("/");
|
494
|
+
input = input.substring(1);
|
495
|
+
}
|
496
|
+
|
497
|
+
printStep("1 ", output.toString(), input);
|
498
|
+
|
499
|
+
// While the input buffer is not empty, loop as follows
|
500
|
+
while (input.length() != 0) {
|
501
|
+
// 2A. If the input buffer begins with a prefix of "./",
|
502
|
+
// then remove that prefix from the input buffer
|
503
|
+
// else if the input buffer begins with a prefix of "../", then
|
504
|
+
// if also the output does not contain the root slash "/" only,
|
505
|
+
// then move this prefix to the end of the output buffer else
|
506
|
+
// remove that prefix
|
507
|
+
if (input.startsWith("./")) {
|
508
|
+
input = input.substring(2);
|
509
|
+
printStep("2A", output.toString(), input);
|
510
|
+
} else if (input.startsWith("../")) {
|
511
|
+
input = input.substring(3);
|
512
|
+
if (!output.toString().equals("/")) {
|
513
|
+
output.append("../");
|
514
|
+
}
|
515
|
+
printStep("2A", output.toString(), input);
|
516
|
+
// 2B. if the input buffer begins with a prefix of "/./" or "/.",
|
517
|
+
// where "." is a complete path segment, then replace that prefix
|
518
|
+
// with "/" in the input buffer; otherwise,
|
519
|
+
} else if (input.startsWith("/./")) {
|
520
|
+
input = input.substring(2);
|
521
|
+
printStep("2B", output.toString(), input);
|
522
|
+
} else if (input.equals("/.")) {
|
523
|
+
// FIXME: what is complete path segment?
|
524
|
+
input = input.replaceFirst("/.", "/");
|
525
|
+
printStep("2B", output.toString(), input);
|
526
|
+
// 2C. if the input buffer begins with a prefix of "/../" or "/..",
|
527
|
+
// where ".." is a complete path segment, then replace that prefix
|
528
|
+
// with "/" in the input buffer and if also the output buffer is
|
529
|
+
// empty, last segment in the output buffer equals "../" or "..",
|
530
|
+
// where ".." is a complete path segment, then append ".." or "/.."
|
531
|
+
// for the latter case respectively to the output buffer else
|
532
|
+
// remove the last segment and its preceding "/" (if any) from the
|
533
|
+
// output buffer and if hereby the first character in the output
|
534
|
+
// buffer was removed and it was not the root slash then delete a
|
535
|
+
// leading slash from the input buffer; otherwise,
|
536
|
+
} else if (input.startsWith("/../")) {
|
537
|
+
input = input.substring(3);
|
538
|
+
if (output.length() == 0) {
|
539
|
+
output.append("/");
|
540
|
+
} else if (output.toString().endsWith("../")) {
|
541
|
+
output.append("..");
|
542
|
+
} else if (output.toString().endsWith("..")) {
|
543
|
+
output.append("/..");
|
544
|
+
} else {
|
545
|
+
int index = output.lastIndexOf("/");
|
546
|
+
if (index == -1) {
|
547
|
+
output = new StringBuilder();
|
548
|
+
if (input.charAt(0) == '/') {
|
549
|
+
input = input.substring(1);
|
550
|
+
}
|
551
|
+
} else {
|
552
|
+
output = output.delete(index, output.length());
|
553
|
+
}
|
554
|
+
}
|
555
|
+
printStep("2C", output.toString(), input);
|
556
|
+
} else if (input.equals("/..")) {
|
557
|
+
// FIXME: what is complete path segment?
|
558
|
+
input = input.replaceFirst("/..", "/");
|
559
|
+
if (output.length() == 0) {
|
560
|
+
output.append("/");
|
561
|
+
} else if (output.toString().endsWith("../")) {
|
562
|
+
output.append("..");
|
563
|
+
} else if (output.toString().endsWith("..")) {
|
564
|
+
output.append("/..");
|
565
|
+
} else {
|
566
|
+
int index = output.lastIndexOf("/");
|
567
|
+
if (index == -1) {
|
568
|
+
output = new StringBuilder();
|
569
|
+
if (input.charAt(0) == '/') {
|
570
|
+
input = input.substring(1);
|
571
|
+
}
|
572
|
+
} else {
|
573
|
+
output = output.delete(index, output.length());
|
574
|
+
}
|
575
|
+
}
|
576
|
+
printStep("2C", output.toString(), input);
|
577
|
+
// 2D. if the input buffer consists only of ".", then remove
|
578
|
+
// that from the input buffer else if the input buffer consists
|
579
|
+
// only of ".." and if the output buffer does not contain only
|
580
|
+
// the root slash "/", then move the ".." to the output buffer
|
581
|
+
// else delte it.; otherwise,
|
582
|
+
} else if (input.equals(".")) {
|
583
|
+
input = "";
|
584
|
+
printStep("2D", output.toString(), input);
|
585
|
+
} else if (input.equals("..")) {
|
586
|
+
if (!output.toString().equals("/")) {
|
587
|
+
output.append("..");
|
588
|
+
}
|
589
|
+
input = "";
|
590
|
+
printStep("2D", output.toString(), input);
|
591
|
+
// 2E. move the first path segment (if any) in the input buffer
|
592
|
+
// to the end of the output buffer, including the initial "/"
|
593
|
+
// character (if any) and any subsequent characters up to, but not
|
594
|
+
// including, the next "/" character or the end of the input buffer.
|
595
|
+
} else {
|
596
|
+
int end = -1;
|
597
|
+
int begin = input.indexOf('/');
|
598
|
+
if (begin == 0) {
|
599
|
+
end = input.indexOf('/', 1);
|
600
|
+
} else {
|
601
|
+
end = begin;
|
602
|
+
begin = 0;
|
603
|
+
}
|
604
|
+
String segment;
|
605
|
+
if (end == -1) {
|
606
|
+
segment = input.substring(begin);
|
607
|
+
input = "";
|
608
|
+
} else {
|
609
|
+
segment = input.substring(begin, end);
|
610
|
+
input = input.substring(end);
|
611
|
+
}
|
612
|
+
output.append(segment);
|
613
|
+
printStep("2E", output.toString(), input);
|
614
|
+
}
|
615
|
+
}
|
616
|
+
|
617
|
+
// 3. Finally, if the only or last segment of the output buffer is
|
618
|
+
// "..", where ".." is a complete path segment not followed by a slash
|
619
|
+
// then append a slash "/". The output buffer is returned as the result
|
620
|
+
// of remove_dot_segments
|
621
|
+
if (output.toString().endsWith("..")) {
|
622
|
+
output.append("/");
|
623
|
+
printStep("3 ", output.toString(), input);
|
624
|
+
}
|
625
|
+
|
626
|
+
return output.toString();
|
627
|
+
}
|
628
|
+
|
629
|
+
private static void printStep(String step, String output, String input) {
|
630
|
+
if (System.getProperty("nokogiri.c14.debug") == "on") {
|
631
|
+
System.out.println(" " + step + ": " + output);
|
632
|
+
if (output.length() == 0) {
|
633
|
+
System.out.println("\t\t\t\t" + input);
|
634
|
+
} else {
|
635
|
+
System.out.println("\t\t\t" + input);
|
636
|
+
}
|
637
|
+
}
|
638
|
+
}
|
639
|
+
}
|