nokogiri 1.5.0.beta.4 → 1.5.0

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 (107) hide show
  1. data/.gemtest +0 -0
  2. data/CHANGELOG.ja.rdoc +34 -0
  3. data/CHANGELOG.rdoc +40 -1
  4. data/Manifest.txt +11 -2
  5. data/README.rdoc +1 -1
  6. data/Rakefile +100 -104
  7. data/bin/nokogiri +1 -2
  8. data/ext/nokogiri/nokogiri.c +24 -1
  9. data/ext/nokogiri/xml_io.c +32 -7
  10. data/ext/nokogiri/xml_node.c +14 -13
  11. data/ext/nokogiri/xml_sax_parser.c +9 -4
  12. data/ext/nokogiri/xslt_stylesheet.c +7 -1
  13. data/lib/nokogiri.rb +3 -22
  14. data/lib/nokogiri/css.rb +4 -0
  15. data/lib/nokogiri/html/document.rb +10 -14
  16. data/lib/nokogiri/version.rb +76 -23
  17. data/lib/nokogiri/xml/builder.rb +7 -0
  18. data/lib/nokogiri/xml/document.rb +17 -1
  19. data/lib/nokogiri/xml/document_fragment.rb +14 -0
  20. data/lib/nokogiri/xml/node.rb +36 -28
  21. data/lib/nokogiri/xml/node/save_options.rb +17 -1
  22. data/lib/nokogiri/xml/node_set.rb +7 -0
  23. data/lib/nokogiri/xml/parse_options.rb +8 -0
  24. data/lib/nokogiri/xml/reader.rb +6 -6
  25. data/lib/nokogiri/xml/schema.rb +7 -1
  26. data/nokogiri_help_responses.md +40 -0
  27. data/tasks/cross_compile.rb +134 -159
  28. data/tasks/nokogiri.org.rb +18 -0
  29. data/tasks/test.rb +1 -1
  30. data/test/files/encoding.html +82 -0
  31. data/test/files/encoding.xhtml +84 -0
  32. data/test/files/metacharset.html +10 -0
  33. data/test/files/noencoding.html +47 -0
  34. data/test/helper.rb +2 -0
  35. data/test/html/test_document.rb +15 -0
  36. data/test/html/test_document_encoding.rb +13 -0
  37. data/test/test_memory_leak.rb +20 -0
  38. data/test/test_reader.rb +22 -0
  39. data/test/test_xslt_transforms.rb +6 -2
  40. data/test/xml/node/test_save_options.rb +10 -2
  41. data/test/xml/test_builder.rb +17 -0
  42. data/test/xml/test_document.rb +22 -0
  43. data/test/xml/test_node.rb +19 -1
  44. data/test/xml/test_node_reparenting.rb +16 -3
  45. data/test/xml/test_node_set.rb +34 -0
  46. data/test/xml/test_schema.rb +5 -0
  47. data/test/xslt/test_exception_handling.rb +37 -0
  48. metadata +185 -157
  49. data/deps.rip +0 -5
  50. data/ext/java/nokogiri/EncodingHandler.java +0 -124
  51. data/ext/java/nokogiri/HtmlDocument.java +0 -149
  52. data/ext/java/nokogiri/HtmlElementDescription.java +0 -145
  53. data/ext/java/nokogiri/HtmlEntityLookup.java +0 -79
  54. data/ext/java/nokogiri/HtmlSaxParserContext.java +0 -259
  55. data/ext/java/nokogiri/NokogiriService.java +0 -535
  56. data/ext/java/nokogiri/XmlAttr.java +0 -191
  57. data/ext/java/nokogiri/XmlAttributeDecl.java +0 -130
  58. data/ext/java/nokogiri/XmlCdata.java +0 -91
  59. data/ext/java/nokogiri/XmlComment.java +0 -86
  60. data/ext/java/nokogiri/XmlDocument.java +0 -529
  61. data/ext/java/nokogiri/XmlDocumentFragment.java +0 -217
  62. data/ext/java/nokogiri/XmlDtd.java +0 -467
  63. data/ext/java/nokogiri/XmlElement.java +0 -222
  64. data/ext/java/nokogiri/XmlElementContent.java +0 -382
  65. data/ext/java/nokogiri/XmlElementDecl.java +0 -148
  66. data/ext/java/nokogiri/XmlEntityDecl.java +0 -162
  67. data/ext/java/nokogiri/XmlEntityReference.java +0 -75
  68. data/ext/java/nokogiri/XmlNamespace.java +0 -128
  69. data/ext/java/nokogiri/XmlNode.java +0 -1399
  70. data/ext/java/nokogiri/XmlNodeSet.java +0 -311
  71. data/ext/java/nokogiri/XmlProcessingInstruction.java +0 -103
  72. data/ext/java/nokogiri/XmlReader.java +0 -411
  73. data/ext/java/nokogiri/XmlRelaxng.java +0 -144
  74. data/ext/java/nokogiri/XmlSaxParserContext.java +0 -367
  75. data/ext/java/nokogiri/XmlSaxPushParser.java +0 -184
  76. data/ext/java/nokogiri/XmlSchema.java +0 -319
  77. data/ext/java/nokogiri/XmlSyntaxError.java +0 -119
  78. data/ext/java/nokogiri/XmlText.java +0 -136
  79. data/ext/java/nokogiri/XmlXpathContext.java +0 -179
  80. data/ext/java/nokogiri/XsltStylesheet.java +0 -183
  81. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +0 -206
  82. data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +0 -73
  83. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +0 -86
  84. data/ext/java/nokogiri/internals/NokogiriHandler.java +0 -327
  85. data/ext/java/nokogiri/internals/NokogiriHelpers.java +0 -582
  86. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +0 -171
  87. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +0 -118
  88. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +0 -74
  89. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +0 -121
  90. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +0 -79
  91. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +0 -126
  92. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +0 -56
  93. data/ext/java/nokogiri/internals/ParserContext.java +0 -278
  94. data/ext/java/nokogiri/internals/PushInputStream.java +0 -411
  95. data/ext/java/nokogiri/internals/ReaderNode.java +0 -474
  96. data/ext/java/nokogiri/internals/SaveContext.java +0 -288
  97. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +0 -76
  98. data/ext/java/nokogiri/internals/XmlDeclHandler.java +0 -42
  99. data/ext/java/nokogiri/internals/XmlDomParser.java +0 -77
  100. data/ext/java/nokogiri/internals/XmlDomParserContext.java +0 -238
  101. data/ext/java/nokogiri/internals/XmlSaxParser.java +0 -65
  102. data/ext/java/nokogiri/internals/XsltExtensionFunction.java +0 -72
  103. data/lib/isorelax.jar +0 -0
  104. data/lib/jing.jar +0 -0
  105. data/lib/nekodtd.jar +0 -0
  106. data/lib/nekohtml.jar +0 -0
  107. data/lib/xercesImpl.jar +0 -0
@@ -1,217 +0,0 @@
1
- /**
2
- * (The MIT License)
3
- *
4
- * Copyright (c) 2008 - 2011:
5
- *
6
- * * {Aaron Patterson}[http://tenderlovemaking.com]
7
- * * {Mike Dalessio}[http://mike.daless.io]
8
- * * {Charles Nutter}[http://blog.headius.com]
9
- * * {Sergio Arbeo}[http://www.serabe.com]
10
- * * {Patrick Mahoney}[http://polycrystal.org]
11
- * * {Yoko Harada}[http://yokolet.blogspot.com]
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining
14
- * a copy of this software and associated documentation files (the
15
- * 'Software'), to deal in the Software without restriction, including
16
- * without limitation the rights to use, copy, modify, merge, publish,
17
- * distribute, sublicense, and/or sell copies of the Software, and to
18
- * permit persons to whom the Software is furnished to do so, subject to
19
- * the following conditions:
20
- *
21
- * The above copyright notice and this permission notice shall be
22
- * included in all copies or substantial portions of the Software.
23
- *
24
- * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
- */
32
-
33
- package nokogiri;
34
-
35
- import static nokogiri.internals.NokogiriHelpers.getLocalNameForNamespace;
36
- import static nokogiri.internals.NokogiriHelpers.getLocalPart;
37
- import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
38
- import static nokogiri.internals.NokogiriHelpers.getPrefix;
39
- import static nokogiri.internals.NokogiriHelpers.isNamespace;
40
- import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
41
-
42
- import java.util.HashMap;
43
- import java.util.Map;
44
- import java.util.Set;
45
- import java.util.regex.Matcher;
46
- import java.util.regex.Pattern;
47
-
48
- import nokogiri.internals.SaveContext;
49
-
50
- import org.jruby.Ruby;
51
- import org.jruby.RubyArray;
52
- import org.jruby.RubyClass;
53
- import org.jruby.RubyString;
54
- import org.jruby.anno.JRubyClass;
55
- import org.jruby.anno.JRubyMethod;
56
- import org.jruby.javasupport.util.RuntimeHelpers;
57
- import org.jruby.runtime.ThreadContext;
58
- import org.jruby.runtime.builtin.IRubyObject;
59
- import org.w3c.dom.Attr;
60
- import org.w3c.dom.NamedNodeMap;
61
-
62
- /**
63
- * Class for Nokogiri::XML::DocumentFragment
64
- *
65
- * @author sergio
66
- * @author Yoko Harada <yokolet@gmail.com>
67
- */
68
- @JRubyClass(name="Nokogiri::XML::DocumentFragment", parent="Nokogiri::XML::Node")
69
- public class XmlDocumentFragment extends XmlNode {
70
- private XmlElement fragmentContext = null;
71
-
72
- public XmlDocumentFragment(Ruby ruby) {
73
- this(ruby, getNokogiriClass(ruby, "Nokogiri::XML::DocumentFragment"));
74
- }
75
-
76
- public XmlDocumentFragment(Ruby ruby, RubyClass klazz) {
77
- super(ruby, klazz);
78
- }
79
-
80
- @JRubyMethod(name="new", meta = true, required=1, optional=2)
81
- public static IRubyObject rbNew(ThreadContext context, IRubyObject cls, IRubyObject[] args) {
82
-
83
- if(args.length < 1) {
84
- throw context.getRuntime().newArgumentError(args.length, 1);
85
- }
86
-
87
- if(!(args[0] instanceof XmlDocument)){
88
- throw context.getRuntime().newArgumentError("first parameter must be a Nokogiri::XML::Document instance");
89
- }
90
-
91
- XmlDocument doc = (XmlDocument) args[0];
92
-
93
- // make wellformed fragment, ignore invalid namespace, or add appropriate namespace to parse
94
- if (args.length > 1 && args[1] instanceof RubyString) {
95
- args[1] = RubyString.newString(context.getRuntime(), ignoreNamespaceIfNeeded(doc, rubyStringToString(args[1])));
96
- args[1] = RubyString.newString(context.getRuntime(), addNamespaceDeclIfNeeded(doc, rubyStringToString(args[1])));
97
- }
98
-
99
- XmlDocumentFragment fragment = (XmlDocumentFragment) NokogiriService.XML_DOCUMENT_FRAGMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass)cls);
100
- fragment.setDocument(context, doc);
101
- fragment.setNode(context, doc.getDocument().createDocumentFragment());
102
-
103
- //TODO: Get namespace definitions from doc.
104
- if (args.length == 3 && args[2] != null && args[2] instanceof XmlElement) {
105
- fragment.fragmentContext = (XmlElement)args[2];
106
- }
107
- RuntimeHelpers.invoke(context, fragment, "initialize", args);
108
- return fragment;
109
- }
110
-
111
- private static Pattern qname_pattern = Pattern.compile("[^</:>\\s]+:[^</:>=\\s]+");
112
- private static Pattern starttag_pattern = Pattern.compile("<[^</>]+>");
113
-
114
- private static String ignoreNamespaceIfNeeded(XmlDocument doc, String tags) {
115
- if (doc.getDocument() == null) return tags;
116
- if (doc.getDocument().getDocumentElement() == null) return tags;
117
- Matcher matcher = qname_pattern.matcher(tags);
118
- Map<String, String> rewriteTable = new HashMap<String, String>();
119
- while(matcher.find()) {
120
- String qName = matcher.group();
121
- NamedNodeMap nodeMap = doc.getDocument().getDocumentElement().getAttributes();
122
- if (!isNamespaceDefined(qName, nodeMap)) {
123
- rewriteTable.put(qName, getLocalPart(qName));
124
- }
125
- }
126
- Set<String> keys = rewriteTable.keySet();
127
- for (String key : keys) {
128
- tags = tags.replace(key, rewriteTable.get(key));
129
- }
130
- return tags;
131
- }
132
-
133
- private static boolean isNamespaceDefined(String qName, NamedNodeMap nodeMap) {
134
- if (isNamespace(qName.intern())) return true;
135
- for (int i=0; i < nodeMap.getLength(); i++) {
136
- Attr attr = (Attr)nodeMap.item(i);
137
- if (isNamespace(attr.getNodeName())) {
138
- String localPart = getLocalNameForNamespace(attr.getNodeName());
139
- if (getPrefix(qName).equals(localPart)) {
140
- return true;
141
- }
142
- }
143
- }
144
- return false;
145
- }
146
-
147
- private static String addNamespaceDeclIfNeeded(XmlDocument doc, String tags) {
148
- if (doc.getDocument() == null) return tags;
149
- if (doc.getDocument().getDocumentElement() == null) return tags;
150
- Matcher matcher = starttag_pattern.matcher(tags);
151
- Map<String, String> rewriteTable = new HashMap<String, String>();
152
- while(matcher.find()) {
153
- String start_tag = matcher.group();
154
- Matcher matcher2 = qname_pattern.matcher(start_tag);
155
- while(matcher2.find()) {
156
- String qName = matcher2.group();
157
- NamedNodeMap nodeMap = doc.getDocument().getDocumentElement().getAttributes();
158
- if (isNamespaceDefined(qName, nodeMap)) {
159
- String namespaceDecl = getNamespceDecl(getPrefix(qName), nodeMap);
160
- if (namespaceDecl != null) {
161
- rewriteTable.put("<"+qName+">", "<"+qName + " " + namespaceDecl+">");
162
- }
163
- }
164
- }
165
- }
166
- Set<String> keys = rewriteTable.keySet();
167
- for (String key : keys) {
168
- tags = tags.replace(key, rewriteTable.get(key));
169
- }
170
-
171
- return tags;
172
- }
173
-
174
- private static String getNamespceDecl(String prefix, NamedNodeMap nodeMap) {
175
- for (int i=0; i < nodeMap.getLength(); i++) {
176
- Attr attr = (Attr)nodeMap.item(i);
177
- if (prefix.equals(attr.getLocalName())) {
178
- return attr.getName() + "=\"" + attr.getValue() + "\"";
179
- }
180
- }
181
- return null;
182
- }
183
-
184
- public XmlElement getFragmentContext() {
185
- return fragmentContext;
186
- }
187
-
188
- //@Override
189
- public void add_child(ThreadContext context, XmlNode child) {
190
- // Some magic for DocumentFragment
191
-
192
- Ruby ruby = context.getRuntime();
193
- XmlNodeSet children = (XmlNodeSet) child.children(context);
194
-
195
- long length = children.length();
196
-
197
- RubyArray childrenArray = children.convertToArray();
198
-
199
- if(length != 0) {
200
- for(int i = 0; i < length; i++) {
201
- XmlNode item = (XmlNode) ((XmlNode) childrenArray.aref(ruby.newFixnum(i))).dup_implementation(context, true);
202
- add_child(context, item);
203
- }
204
- }
205
- }
206
-
207
- @Override
208
- public void relink_namespace(ThreadContext context) {
209
- ((XmlNodeSet) children(context)).relink_namespace(context);
210
- }
211
-
212
- @Override
213
- public void saveContent(ThreadContext context, SaveContext ctx) {
214
- saveNodeListContent(context, (XmlNodeSet) children(context), ctx);
215
- }
216
-
217
- }
@@ -1,467 +0,0 @@
1
- /**
2
- * (The MIT License)
3
- *
4
- * Copyright (c) 2008 - 2011:
5
- *
6
- * * {Aaron Patterson}[http://tenderlovemaking.com]
7
- * * {Mike Dalessio}[http://mike.daless.io]
8
- * * {Charles Nutter}[http://blog.headius.com]
9
- * * {Sergio Arbeo}[http://www.serabe.com]
10
- * * {Patrick Mahoney}[http://polycrystal.org]
11
- * * {Yoko Harada}[http://yokolet.blogspot.com]
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining
14
- * a copy of this software and associated documentation files (the
15
- * 'Software'), to deal in the Software without restriction, including
16
- * without limitation the rights to use, copy, modify, merge, publish,
17
- * distribute, sublicense, and/or sell copies of the Software, and to
18
- * permit persons to whom the Software is furnished to do so, subject to
19
- * the following conditions:
20
- *
21
- * The above copyright notice and this permission notice shall be
22
- * included in all copies or substantial portions of the Software.
23
- *
24
- * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
- */
32
-
33
- package nokogiri;
34
-
35
- import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
- import static nokogiri.internals.NokogiriHelpers.nonEmptyStringOrNil;
37
- import static nokogiri.internals.NokogiriHelpers.stringOrNil;
38
- import static org.jruby.javasupport.util.RuntimeHelpers.invoke;
39
- import nokogiri.internals.NokogiriHelpers;
40
- import nokogiri.internals.SaveContext;
41
-
42
- import org.apache.xerces.xni.QName;
43
- import org.cyberneko.dtd.DTDConfiguration;
44
- import org.jruby.Ruby;
45
- import org.jruby.RubyArray;
46
- import org.jruby.RubyClass;
47
- import org.jruby.RubyHash;
48
- import org.jruby.anno.JRubyClass;
49
- import org.jruby.anno.JRubyMethod;
50
- import org.jruby.runtime.ThreadContext;
51
- import org.jruby.runtime.builtin.IRubyObject;
52
- import org.w3c.dom.Document;
53
- import org.w3c.dom.DocumentType;
54
- import org.w3c.dom.Element;
55
- import org.w3c.dom.Node;
56
-
57
- /**
58
- * Class for Nokogiri::XML::DTD
59
- *
60
- * @author sergio
61
- * @author Patrick Mahoney <pat@polycrystal.org>
62
- * @author Yoko Harada <yokolet@gmail.com>
63
- */
64
-
65
- @JRubyClass(name="Nokogiri::XML::DTD", parent="Nokogiri::XML::Node")
66
- public class XmlDtd extends XmlNode {
67
- protected RubyArray allDecls = null;
68
-
69
- /** cache of children, Nokogiri::XML::NodeSet */
70
- protected IRubyObject children = null;
71
-
72
- /** cache of name => XmlAttributeDecl */
73
- protected RubyHash attributes = null;
74
-
75
- /** cache of name => XmlElementDecl */
76
- protected RubyHash elements = null;
77
-
78
- /** cache of name => XmlEntityDecl */
79
- protected RubyHash entities = null;
80
-
81
- /** cache of name => Nokogiri::XML::Notation */
82
- protected RubyHash notations = null;
83
- protected RubyClass notationClass;
84
-
85
- /** temporary store of content models before they are added to
86
- * their XmlElementDecl. */
87
- protected RubyHash contentModels;
88
-
89
- /** node name */
90
- protected IRubyObject name;
91
-
92
- /** public ID (or external ID) */
93
- protected IRubyObject pubId;
94
-
95
- /** system ID */
96
- protected IRubyObject sysId;
97
-
98
- public XmlDtd(Ruby ruby, RubyClass rubyClass) {
99
- super(ruby, rubyClass);
100
- }
101
-
102
- public XmlDtd(Ruby ruby) {
103
- this(ruby, getNokogiriClass(ruby, "Nokogiri::XML::DTD"), null);
104
- }
105
-
106
- public XmlDtd(Ruby ruby, Node dtd) {
107
- this(ruby, getNokogiriClass(ruby, "Nokogiri::XML::DTD"), dtd);
108
- }
109
-
110
- public XmlDtd(Ruby ruby, RubyClass rubyClass, Node dtd) {
111
- super(ruby, rubyClass, dtd);
112
- notationClass = (RubyClass)
113
- ruby.getClassFromPath("Nokogiri::XML::Notation");
114
-
115
- name = pubId = sysId = ruby.getNil();
116
- if (dtd == null) return;
117
-
118
- // This is the dtd declaration stored in the document; it
119
- // contains the DTD name (root element) and public and system
120
- // ids. The actual declarations are in the NekoDTD 'dtd'
121
- // variable. I don't know of a way to consolidate the two.
122
-
123
- DocumentType otherDtd = dtd.getOwnerDocument().getDoctype();
124
- if (otherDtd != null) {
125
- name = stringOrNil(ruby, otherDtd.getNodeName());
126
- pubId = nonEmptyStringOrNil(ruby, otherDtd.getPublicId());
127
- sysId = nonEmptyStringOrNil(ruby, otherDtd.getSystemId());
128
- }
129
- }
130
-
131
- public static XmlDtd newEmpty(Ruby ruby,
132
- Document doc,
133
- IRubyObject name,
134
- IRubyObject external_id,
135
- IRubyObject system_id) {
136
- Element placeHolder = doc.createElement("dtd_placeholder");
137
- XmlDtd dtd = new XmlDtd(ruby, placeHolder);
138
- dtd.name = name;
139
- dtd.pubId = external_id;
140
- dtd.sysId = system_id;
141
- return dtd;
142
- }
143
-
144
-
145
- /**
146
- * Create an unparented element that contains DTD declarations
147
- * parsed from the internal subset attached as user data to
148
- * <code>doc</code>. The attached dtd must be the tree from
149
- * NekoDTD. The owner document of the returned tree will be
150
- * <code>doc</doc>.
151
- *
152
- * NekoDTD parser returns a new document node containing elements
153
- * representing the dtd declarations. The plan is to get the root
154
- * element and adopt it into the correct document, stipping the
155
- * Document provided by NekoDTD.
156
- *
157
- */
158
- public static XmlDtd newFromInternalSubset(Ruby ruby, Document doc) {
159
- Object dtdTree_ = doc.getUserData(XmlDocument.DTD_RAW_DOCUMENT);
160
- if (dtdTree_ == null)
161
- return new XmlDtd(ruby);
162
-
163
- Node dtdTree = (Node) dtdTree_;
164
- Node dtd = getInternalSubset(dtdTree);
165
- if (dtd == null) {
166
- return new XmlDtd(ruby);
167
- } else {
168
- // Import the node into doc so it has the correct owner document.
169
- dtd = doc.importNode(dtd, true);
170
- return new XmlDtd(ruby, dtd);
171
- }
172
- }
173
-
174
- public static IRubyObject newFromExternalSubset(Ruby ruby, Document doc) {
175
- Object dtdTree_ = doc.getUserData(XmlDocument.DTD_RAW_DOCUMENT);
176
- if (dtdTree_ == null) {
177
- return ruby.getNil();
178
- }
179
-
180
- Node dtdTree = (Node) dtdTree_;
181
- Node dtd = getExternalSubset(dtdTree);
182
- if (dtd == null) {
183
- return ruby.getNil();
184
- } else if (!dtd.hasChildNodes()) {
185
- return ruby.getNil();
186
- } else {
187
- // Import the node into doc so it has the correct owner document.
188
- dtd = doc.importNode(dtd, true);
189
- return new XmlDtd(ruby, dtd);
190
- }
191
- }
192
-
193
- /*
194
- * <code>dtd</code> is the document node of a NekoDTD tree.
195
- * NekoDTD tree looks like this:
196
- *
197
- * <code><pre>
198
- * [#document: null]
199
- * [#comment: ...]
200
- * [#comment: ...]
201
- * [dtd: null] // a DocumentType; isDTD(node) => false
202
- * [dtd: null] // root of dtd, an Element node; isDTD(node) => true
203
- * ... decls, content models, etc. ...
204
- * [externalSubset: null] pubid="the pubid" sysid="the sysid"
205
- * ... external subset decls, etc. ...
206
- * </pre></code>
207
- */
208
- protected static Node getInternalSubset(Node dtdTree) {
209
- Node root;
210
- for (root = dtdTree.getFirstChild(); ; root = root.getNextSibling()) {
211
- if (root == null)
212
- return null;
213
- else if (isDTD(root))
214
- return root; // we have second dtd which is root
215
- }
216
- }
217
-
218
- protected static Node getExternalSubset(Node dtdTree) {
219
- Node dtd = getInternalSubset(dtdTree);
220
- if (dtd == null) return null;
221
- for (Node ext = dtd.getFirstChild(); ; ext = ext.getNextSibling()) {
222
- if (ext == null)
223
- return null;
224
- else if (isExternalSubset(ext))
225
- return ext;
226
- }
227
- }
228
-
229
- /**
230
- * This overrides the #attributes method defined in
231
- * lib/nokogiri/xml/node.rb.
232
- */
233
- @JRubyMethod
234
- public IRubyObject attributes(ThreadContext context) {
235
- if (attributes == null) extractDecls(context);
236
-
237
- return attributes;
238
- }
239
-
240
- @JRubyMethod
241
- public IRubyObject elements(ThreadContext context) {
242
- if (elements == null) extractDecls(context);
243
-
244
- return elements;
245
- }
246
-
247
- @JRubyMethod
248
- public IRubyObject entities(ThreadContext context) {
249
- if (entities == null) extractDecls(context);
250
-
251
- return entities;
252
- }
253
-
254
- @JRubyMethod
255
- public IRubyObject notations(ThreadContext context) {
256
- if (notations == null) extractDecls(context);
257
-
258
- return notations;
259
- }
260
-
261
- /**
262
- * Our "node" object is as-returned by NekoDTD. The actual
263
- * "children" that we're interested in (Attribute declarations,
264
- * etc.) are a few layers deep.
265
- */
266
- @Override
267
- @JRubyMethod
268
- public IRubyObject children(ThreadContext context) {
269
- if (children == null) extractDecls(context);
270
-
271
- return children;
272
- }
273
-
274
- /**
275
- * Returns the name of the dtd.
276
- */
277
- @Override
278
- @JRubyMethod
279
- public IRubyObject node_name(ThreadContext context) {
280
- return name;
281
- }
282
-
283
- @Override
284
- @JRubyMethod(name = "node_name=")
285
- public IRubyObject node_name_set(ThreadContext context, IRubyObject name) {
286
- throw context.getRuntime()
287
- .newRuntimeError("cannot change name of DTD");
288
- }
289
-
290
- @JRubyMethod
291
- public IRubyObject system_id(ThreadContext context) {
292
- return sysId;
293
- }
294
-
295
- @JRubyMethod
296
- public IRubyObject external_id(ThreadContext context) {
297
- return pubId;
298
- }
299
-
300
- @JRubyMethod
301
- public IRubyObject validate(ThreadContext context, IRubyObject doc) {
302
- RubyArray errors = RubyArray.newArray(context.getRuntime());
303
- if (doc instanceof XmlDocument) {
304
- errors = (RubyArray) ((XmlDocument)doc).getInstanceVariable("@errors");
305
- }
306
- return errors;
307
- }
308
-
309
- public static boolean nameEquals(Node node, QName name) {
310
- return name.localpart.equals(node.getNodeName());
311
- }
312
-
313
- public static boolean isExternalSubset(Node node) {
314
- return nameEquals(node, DTDConfiguration.E_EXTERNAL_SUBSET);
315
- }
316
-
317
- /**
318
- * Checks instanceof Element so we return false for a DocumentType
319
- * node (NekoDTD uses Element for all its nodes).
320
- */
321
- public static boolean isDTD(Node node) {
322
- return (node instanceof Element &&
323
- nameEquals(node, DTDConfiguration.E_DTD));
324
- }
325
-
326
- public static boolean isAttributeDecl(Node node) {
327
- return nameEquals(node, DTDConfiguration.E_ATTRIBUTE_DECL);
328
- }
329
-
330
- public static boolean isElementDecl(Node node) {
331
- return nameEquals(node, DTDConfiguration.E_ELEMENT_DECL);
332
- }
333
-
334
- public static boolean isEntityDecl(Node node) {
335
- return (nameEquals(node, DTDConfiguration.E_INTERNAL_ENTITY_DECL) ||
336
- nameEquals(node, DTDConfiguration.E_UNPARSED_ENTITY_DECL));
337
- }
338
-
339
- public static boolean isNotationDecl(Node node) {
340
- return nameEquals(node, DTDConfiguration.E_NOTATION_DECL);
341
- }
342
-
343
- public static boolean isContentModel(Node node) {
344
- return nameEquals(node, DTDConfiguration.E_CONTENT_MODEL);
345
- }
346
-
347
- /**
348
- * Recursively extract various DTD declarations and store them in
349
- * the various collections.
350
- */
351
- protected void extractDecls(ThreadContext context) {
352
- Ruby runtime = context.getRuntime();
353
-
354
- // initialize data structures
355
- allDecls = RubyArray.newArray(runtime);
356
- attributes = RubyHash.newHash(runtime);
357
- elements = RubyHash.newHash(runtime);
358
- entities = RubyHash.newHash(runtime);
359
- notations = RubyHash.newHash(runtime);
360
- contentModels = RubyHash.newHash(runtime);
361
- children = runtime.getNil();
362
-
363
- // recursively extract decls
364
- if (node == null) return; // leave all the decl hash's empty
365
- extractDecls(context, node.getFirstChild());
366
-
367
- // convert allDecls to a NodeSet
368
- children =
369
- new XmlNodeSet(runtime,
370
- getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"),
371
- allDecls);
372
-
373
- // add attribute decls as attributes to the matching element decl
374
- RubyArray keys = attributes.keys();
375
- for (int i = 0; i < keys.getLength(); ++i) {
376
- IRubyObject akey = keys.entry(i);
377
- IRubyObject val;
378
-
379
- val = attributes.op_aref(context, akey);
380
- if (val.isNil()) continue;
381
- XmlAttributeDecl attrDecl = (XmlAttributeDecl) val;
382
- IRubyObject ekey = attrDecl.element_name(context);
383
- val = elements.op_aref(context, ekey);
384
- if (val.isNil()) continue;
385
- XmlElementDecl elemDecl = (XmlElementDecl) val;
386
-
387
- elemDecl.appendAttrDecl(attrDecl);
388
- }
389
-
390
- // add content models to the matching element decl
391
- keys = contentModels.keys();
392
- for (int i = 0; i < keys.getLength(); ++i) {
393
- IRubyObject key = keys.entry(i);
394
- IRubyObject cm = contentModels.op_aref(context, key);
395
-
396
- IRubyObject elem = elements.op_aref(context, key);
397
- if (elem.isNil()) continue;
398
- if (((XmlElementDecl)elem).isEmpty()) continue;
399
- ((XmlElementDecl) elem).setContentModel(cm);
400
- }
401
- }
402
-
403
- /**
404
- * The <code>node</code> is either the first child of the root dtd
405
- * node (as returned by getInternalSubset()) or the first child of
406
- * the external subset node (as returned by getExternalSubset()).
407
- *
408
- * This recursive function will not descend into an
409
- * 'externalSubset' node, thus for an internal subset it only
410
- * extracts nodes in the internal subset, and for an external
411
- * subset it extracts everything and assumess <code>node</code>
412
- * and all children are part of the external subset.
413
- */
414
- protected void extractDecls(ThreadContext context, Node node) {
415
- while (node != null) {
416
- if (isExternalSubset(node)) {
417
- return;
418
- } else if (isAttributeDecl(node)) {
419
- XmlAttributeDecl decl = (XmlAttributeDecl)
420
- XmlAttributeDecl.create(context, node);
421
- attributes.op_aset(context, decl.attribute_name(context), decl);
422
- allDecls.append(decl);
423
- } else if (isElementDecl(node)) {
424
- XmlElementDecl decl = (XmlElementDecl)
425
- XmlElementDecl.create(context, node);
426
- elements.op_aset(context, decl.element_name(context), decl);
427
- allDecls.append(decl);
428
- } else if (isEntityDecl(node)) {
429
- XmlEntityDecl decl = (XmlEntityDecl)
430
- XmlEntityDecl.create(context, node);
431
- entities.op_aset(context, decl.node_name(context), decl);
432
- allDecls.append(decl);
433
- } else if (isNotationDecl(node)) {
434
- XmlNode tmp = (XmlNode)
435
- NokogiriHelpers.constructNode(context.getRuntime(), node);
436
- IRubyObject decl = invoke(context, notationClass, "new",
437
- tmp.getAttribute(context, "name"),
438
- tmp.getAttribute(context, "pubid"),
439
- tmp.getAttribute(context, "sysid"));
440
- notations.op_aset(context,
441
- tmp.getAttribute(context, "name"), decl);
442
- allDecls.append(decl);
443
- } else if (isContentModel(node)) {
444
- XmlElementContent cm =
445
- new XmlElementContent(context.getRuntime(),
446
- (XmlDocument) document(context),
447
- node);
448
- contentModels.op_aset(context, cm.element_name(context), cm);
449
- } else {
450
- // recurse
451
- extractDecls(context, node.getFirstChild());
452
- }
453
-
454
- node = node.getNextSibling();
455
- }
456
- }
457
-
458
- public void saveContent(ThreadContext context, SaveContext ctx) {
459
- ctx.append("<!DOCTYPE " + name + " ");
460
- if (pubId != null) {
461
- ctx.append("PUBLIC \"" + pubId + "\" \"" + sysId + "\">");
462
- } else if (sysId != null) {
463
- ctx.append("SYSTEM " + sysId);
464
- }
465
- }
466
-
467
- }