smile-xml 1.0.0-jruby → 1.0.2-jruby

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/lib/smile-xml.jar +0 -0
  2. data/src/main/java/smile/xml/AttrJ.java +160 -0
  3. data/src/main/java/smile/xml/AttributesJ.java +212 -0
  4. data/src/main/java/smile/xml/BaseJ.java +60 -0
  5. data/src/main/java/smile/xml/DocumentJ.java +435 -0
  6. data/src/main/java/smile/xml/EncodingJ.java +94 -0
  7. data/src/main/java/smile/xml/ErrorJ.java +162 -0
  8. data/src/main/java/smile/xml/NamespaceJ.java +67 -0
  9. data/src/main/java/smile/xml/NamespacesJ.java +62 -0
  10. data/src/main/java/smile/xml/NodeJ.java +1029 -0
  11. data/src/main/java/smile/xml/NodeSetJ.java +90 -0
  12. data/src/main/java/smile/xml/ParserContextJ.java +44 -0
  13. data/src/main/java/smile/xml/ParserJ.java +196 -0
  14. data/src/main/java/smile/xml/ParserOptionsJ.java +58 -0
  15. data/src/main/java/smile/xml/ReaderJ.java +34 -0
  16. data/src/main/java/smile/xml/SchemaJ.java +66 -0
  17. data/src/main/java/smile/xml/SmileXML.java +65 -0
  18. data/src/main/java/smile/xml/XmlJ.java +58 -0
  19. data/src/main/java/smile/xml/sax/CallbackHandler.java +113 -0
  20. data/src/main/java/smile/xml/sax/SaxParserCallbacksJ.java +71 -0
  21. data/src/main/java/smile/xml/sax/SaxParserJ.java +153 -0
  22. data/src/main/java/smile/xml/util/UtilJ.java +424 -0
  23. data/src/main/java/smile/xml/xpath/CustomNamespaceContext.java +59 -0
  24. data/src/main/java/smile/xml/xpath/XPathContextJ.java +154 -0
  25. data/src/main/java/smile/xml/xpath/XPathExpressionJ.java +62 -0
  26. data/src/main/java/smile/xml/xpath/XPathJ.java +36 -0
  27. data/src/main/java/smile/xml/xpath/XPathObjectJ.java +196 -0
  28. data/src/main/java/smile/xml/xpath/XPointerJ.java +32 -0
  29. data/src/main/ruby/xml/libxml.rb +1 -0
  30. data/src/main/ruby/xml.rb +5 -0
  31. data/src/test/ruby/etc_doc_to_s.rb +21 -0
  32. data/src/test/ruby/ets_doc_file.rb +17 -0
  33. data/src/test/ruby/ets_doc_to_s.rb +23 -0
  34. data/src/test/ruby/ets_gpx.rb +28 -0
  35. data/src/test/ruby/ets_node_gc.rb +23 -0
  36. data/src/test/ruby/ets_test.xml +2 -0
  37. data/src/test/ruby/ets_tsr.rb +11 -0
  38. data/src/test/ruby/model/atom.xml +13 -0
  39. data/src/test/ruby/model/bands.iso-8859-1.xml +5 -0
  40. data/src/test/ruby/model/bands.utf-8.xml +5 -0
  41. data/src/test/ruby/model/bands.xml +5 -0
  42. data/src/test/ruby/model/books.xml +146 -0
  43. data/src/test/ruby/model/merge_bug_data.xml +58 -0
  44. data/src/test/ruby/model/ruby-lang.html +238 -0
  45. data/src/test/ruby/model/rubynet.xml +79 -0
  46. data/src/test/ruby/model/shiporder.rnc +28 -0
  47. data/src/test/ruby/model/shiporder.rng +86 -0
  48. data/src/test/ruby/model/shiporder.xml +23 -0
  49. data/src/test/ruby/model/shiporder.xsd +31 -0
  50. data/src/test/ruby/model/soap.xml +27 -0
  51. data/src/test/ruby/model/xinclude.xml +5 -0
  52. data/src/test/ruby/smile_xml_test.rb +64 -0
  53. data/src/test/ruby/tc_attr.rb +191 -0
  54. data/src/test/ruby/tc_attr_decl.rb +133 -0
  55. data/src/test/ruby/tc_attributes.rb +135 -0
  56. data/src/test/ruby/tc_deprecated_require.rb +13 -0
  57. data/src/test/ruby/tc_document.rb +162 -0
  58. data/src/test/ruby/tc_document_write.rb +212 -0
  59. data/src/test/ruby/tc_dtd.rb +125 -0
  60. data/src/test/ruby/tc_error.rb +150 -0
  61. data/src/test/ruby/tc_html_parser.rb +140 -0
  62. data/src/test/ruby/tc_namespace.rb +62 -0
  63. data/src/test/ruby/tc_namespaces.rb +210 -0
  64. data/src/test/ruby/tc_node.rb +273 -0
  65. data/src/test/ruby/tc_node_cdata.rb +51 -0
  66. data/src/test/ruby/tc_node_comment.rb +33 -0
  67. data/src/test/ruby/tc_node_copy.rb +42 -0
  68. data/src/test/ruby/tc_node_edit.rb +178 -0
  69. data/src/test/ruby/tc_node_text.rb +73 -0
  70. data/src/test/ruby/tc_node_write.rb +108 -0
  71. data/src/test/ruby/tc_node_xlink.rb +29 -0
  72. data/src/test/ruby/tc_parser.rb +371 -0
  73. data/src/test/ruby/tc_parser_context.rb +189 -0
  74. data/src/test/ruby/tc_properties.rb +40 -0
  75. data/src/test/ruby/tc_reader.rb +306 -0
  76. data/src/test/ruby/tc_relaxng.rb +54 -0
  77. data/src/test/ruby/tc_sax_parser.rb +340 -0
  78. data/src/test/ruby/tc_schema.rb +59 -0
  79. data/src/test/ruby/tc_traversal.rb +222 -0
  80. data/src/test/ruby/tc_xinclude.rb +21 -0
  81. data/src/test/ruby/tc_xml.rb +226 -0
  82. data/src/test/ruby/tc_xpath.rb +210 -0
  83. data/src/test/ruby/tc_xpath_context.rb +80 -0
  84. data/src/test/ruby/tc_xpath_expression.rb +38 -0
  85. data/src/test/ruby/tc_xpointer.rb +74 -0
  86. data/src/test/ruby/test_helper.rb +23 -0
  87. data/src/test/ruby/test_suite.rb +41 -0
  88. metadata +91 -5
@@ -0,0 +1,113 @@
1
+ package smile.xml.sax;
2
+
3
+ import java.util.HashMap;
4
+ import java.util.Map;
5
+
6
+ import org.jruby.RubyHash;
7
+ import org.jruby.RubyString;
8
+ import org.jruby.runtime.ThreadContext;
9
+ import org.jruby.runtime.builtin.IRubyObject;
10
+ import org.xml.sax.Attributes;
11
+ import org.xml.sax.SAXException;
12
+ import org.xml.sax.SAXParseException;
13
+ import org.xml.sax.ext.DefaultHandler2;
14
+
15
+ import smile.xml.ErrorJ;
16
+
17
+ public class CallbackHandler extends DefaultHandler2 {
18
+
19
+ private final IRubyObject callback;
20
+ private final ThreadContext context;
21
+ private boolean cdata = false;
22
+
23
+ public CallbackHandler( ThreadContext context, IRubyObject callback ) {
24
+ this.context = context;
25
+ this.callback = callback;
26
+ }
27
+
28
+ private void call( String method, IRubyObject...args ) {
29
+ callback.callMethod( context, method, args );
30
+ }
31
+
32
+ private RubyString toString( String string ) {
33
+ return context.getRuntime().newString( string );
34
+ }
35
+
36
+ private RubyHash toHash( Map<?,?> map ) {
37
+ return RubyHash.newHash( context.getRuntime(), map, context.getRuntime().getNil() );
38
+ }
39
+
40
+ @Override
41
+ public void startDocument() throws SAXException {
42
+ call( "on_start_document" );
43
+ }
44
+
45
+ @Override
46
+ public void endDocument() throws SAXException {
47
+ call( "on_end_document" );
48
+ }
49
+
50
+ @Override
51
+ public void error(SAXParseException e) throws SAXException {
52
+ call( "on_error", ErrorJ.newInstance( context, e.getMessage()) );
53
+ }
54
+
55
+ @Override
56
+ public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
57
+
58
+ // System.out.println( uri );
59
+ // System.out.println( localName );
60
+ // System.out.println( qName );
61
+ // System.out.println("---------");
62
+ Map<IRubyObject, IRubyObject> map = new HashMap<IRubyObject,IRubyObject>();
63
+ for (int i = 0; i < atts.getLength(); i++)
64
+ map.put( toString( atts.getLocalName(i) ), toString( atts.getValue(i) ) );
65
+
66
+ call( "on_start_element", toString(qName), toHash(map) );
67
+ // if( localName.equals( qName ) ) {
68
+ //
69
+ // } else {
70
+ // System.err.println( "on_start_element_ns ");
71
+ // call( "on_start_element_ns", toString(localName), toHash(map), toString(""), toString(uri), context.getRuntime().getNil() );
72
+ // }
73
+ }
74
+
75
+ @Override
76
+ public void endElement(String uri, String localName, String qName) throws SAXException {
77
+ call( "on_end_element", toString( qName ) );
78
+ }
79
+
80
+ @Override
81
+ public void characters(char[] ch, int start, int length) throws SAXException {
82
+
83
+ RubyString string = toString( String.valueOf(ch, start, length) );
84
+ if (this.cdata) {
85
+ call( "on_cdata_block", string );
86
+ } else {
87
+ call( "on_characters", string );
88
+ }
89
+ }
90
+
91
+ @Override
92
+ public void processingInstruction(String target, String data) throws SAXException {
93
+ call( "on_processing_instruction", toString(target), toString(data));
94
+ }
95
+
96
+ @Override
97
+ public void startCDATA() {
98
+ this.cdata = true;
99
+ }
100
+
101
+ @Override
102
+ public void endCDATA() {
103
+ this.cdata = false;
104
+ }
105
+
106
+ @Override
107
+ public void comment(char[] ch, int start, int length) throws SAXException {
108
+ call( "on_comment", toString( String.valueOf(ch, start, length) ) );
109
+ }
110
+
111
+ }
112
+
113
+
@@ -0,0 +1,71 @@
1
+ package smile.xml.sax;
2
+
3
+ import org.jruby.Ruby;
4
+ import org.jruby.RubyModule;
5
+ import org.jruby.anno.JRubyMethod;
6
+ import org.jruby.anno.JRubyModule;
7
+ import org.jruby.runtime.ThreadContext;
8
+ import org.jruby.runtime.builtin.IRubyObject;
9
+
10
+ import smile.xml.util.UtilJ;
11
+
12
+ @JRubyModule(name = "LibXML::XML::SaxParser::Callbacks")
13
+ public class SaxParserCallbacksJ {
14
+ public static RubyModule define(Ruby runtime) {
15
+ return UtilJ.defineModule(runtime, SaxParserCallbacksJ.class);
16
+ }
17
+
18
+ @JRubyMethod(name = "on_start_document")
19
+ public static void onStartDocument(ThreadContext context, IRubyObject self) {
20
+ }
21
+
22
+ @JRubyMethod(name = "on_start_element")
23
+ public static void onStartElement(ThreadContext context, IRubyObject self,
24
+ IRubyObject name, IRubyObject attr) {
25
+ }
26
+
27
+ @JRubyMethod(name = "on_characters")
28
+ public static void onCharacter(ThreadContext context, IRubyObject self,
29
+ IRubyObject arg) {
30
+ }
31
+
32
+ @JRubyMethod(name = "on_comment")
33
+ public static void onComment(ThreadContext context, IRubyObject self,
34
+ IRubyObject arg) {
35
+ }
36
+
37
+ @JRubyMethod(name = "on_processing_instruction")
38
+ public static void onProcessingInstruction(ThreadContext context,
39
+ IRubyObject self, IRubyObject target, IRubyObject data) {
40
+ }
41
+
42
+ @JRubyMethod(name = "on_cdata_block")
43
+ public static void onCDataBlock(ThreadContext context, IRubyObject self,
44
+ IRubyObject cdata) {
45
+ }
46
+
47
+ @JRubyMethod(name = "on_end_element")
48
+ public static void onEndElement(ThreadContext context, IRubyObject self,
49
+ IRubyObject name) {
50
+ }
51
+
52
+ @JRubyMethod(name = "on_end_document")
53
+ public static void onEndDocument(ThreadContext context, IRubyObject self) {
54
+ }
55
+
56
+ @JRubyMethod(name = "on_start_element_ns", rest = true)
57
+ public static void onStartElementNS(ThreadContext context,
58
+ IRubyObject self, IRubyObject[] args) {
59
+ }
60
+
61
+ @JRubyMethod(name = "on_end_element_ns")
62
+ public static void onEndElementNS(ThreadContext context, IRubyObject self,
63
+ IRubyObject name, IRubyObject prefix, IRubyObject uri) {
64
+ }
65
+
66
+ @JRubyMethod(name = "on_error")
67
+ public static void onError(ThreadContext context, IRubyObject self,
68
+ IRubyObject error) {
69
+ }
70
+
71
+ }
@@ -0,0 +1,153 @@
1
+ package smile.xml.sax;
2
+
3
+ import java.io.File;
4
+ import java.io.FileReader;
5
+ import java.io.IOException;
6
+ import java.io.StringReader;
7
+
8
+ import org.jruby.Ruby;
9
+ import org.jruby.RubyClass;
10
+ import org.jruby.RubyObject;
11
+ import org.jruby.RubyString;
12
+ import org.jruby.anno.JRubyClass;
13
+ import org.jruby.anno.JRubyMethod;
14
+ import org.jruby.runtime.ObjectAllocator;
15
+ import org.jruby.runtime.ThreadContext;
16
+ import org.jruby.runtime.builtin.IRubyObject;
17
+ import org.xml.sax.EntityResolver;
18
+ import org.xml.sax.InputSource;
19
+ import org.xml.sax.SAXException;
20
+ import org.xml.sax.XMLReader;
21
+ import org.xml.sax.helpers.XMLReaderFactory;
22
+
23
+ import smile.xml.ErrorJ;
24
+ import smile.xml.ParserJ;
25
+ import smile.xml.util.UtilJ;
26
+
27
+ @JRubyClass(name="LibXML::XML::SaxParser")
28
+ public class SaxParserJ extends RubyObject {
29
+
30
+ private static final long serialVersionUID = -8712513236401964568L;
31
+
32
+ public static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
33
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
34
+ return new SaxParserJ(runtime, klass);
35
+ }
36
+ };
37
+
38
+ public static RubyClass define(Ruby runtime) {
39
+ return UtilJ.defineClass( runtime, SaxParserJ.class, ALLOCATOR );
40
+ }
41
+
42
+ public static RubyClass getRubyClass(Ruby runtime) {
43
+ return UtilJ.getClass(runtime, SaxParserJ.class );
44
+ }
45
+
46
+ @JRubyMethod(name = { "string" }, module = true)
47
+ public static IRubyObject fromString(ThreadContext context, IRubyObject self, IRubyObject pString) {
48
+
49
+ if (pString.isNil())
50
+ throw context.getRuntime().newTypeError( "wrong argument type nil (expected String)" );
51
+
52
+ SaxParserJ parser = new SaxParserJ(context);
53
+ parser.string = ((RubyString) pString);
54
+ return parser;
55
+ }
56
+
57
+ @JRubyMethod(name = "file", module = true)
58
+ public static IRubyObject fromFile(ThreadContext context, IRubyObject self,
59
+ IRubyObject pFile) {
60
+
61
+ if (pFile.isNil())
62
+ throw context.getRuntime().newTypeError( "can't convert nil into String" );
63
+
64
+ if (new File(pFile.asJavaString()).exists() == false)
65
+ throw ErrorJ.newRaiseException( context, "Warning: failed to load external entity \"" + pFile.asJavaString() + "\".");
66
+
67
+ SaxParserJ parser = new SaxParserJ(context);
68
+ parser.fileName = ((RubyString) pFile);
69
+ return parser;
70
+ }
71
+
72
+ private RubyString string;
73
+ private RubyString fileName;
74
+ private IRubyObject callbacks;
75
+
76
+ @JRubyMethod(name = { "io" }, module = true)
77
+ public static IRubyObject fromIo(ThreadContext context, IRubyObject self, IRubyObject pIo) {
78
+ SaxParserJ parser = new SaxParserJ(context);
79
+ parser.string = ((RubyString) pIo.callMethod(context, "read"));
80
+ return parser;
81
+ }
82
+
83
+ public SaxParserJ(Ruby runtime, RubyClass metaClass) {
84
+ super(runtime, metaClass);
85
+ }
86
+
87
+ public SaxParserJ(ThreadContext context) {
88
+ this(context.getRuntime(), getRubyClass(context.getRuntime()));
89
+ }
90
+
91
+ @JRubyMethod(name = { "initialize" }, optional = 1)
92
+ public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
93
+ return this;
94
+ }
95
+
96
+ @JRubyMethod(name = { "filename=" })
97
+ public void setFilename(ThreadContext context, IRubyObject pFileName) {
98
+ this.fileName = ((RubyString) pFileName);
99
+ }
100
+
101
+ @JRubyMethod(name = { "string=" })
102
+ public void setString(ThreadContext context, IRubyObject pFileName) {
103
+ this.string = ((RubyString) pFileName);
104
+ }
105
+
106
+ @JRubyMethod(name = { "string" })
107
+ public IRubyObject getString(ThreadContext context) {
108
+ return this.string;
109
+ }
110
+
111
+ @JRubyMethod(name = { "callbacks=" })
112
+ public void setCallbacks(ThreadContext context, IRubyObject pCallbacks) {
113
+ this.callbacks = pCallbacks;
114
+ }
115
+
116
+ @JRubyMethod(name = { "callbacks" })
117
+ public IRubyObject getCallbacks(ThreadContext context) {
118
+ return this.callbacks;
119
+ }
120
+
121
+ @JRubyMethod(name = { "parse" })
122
+ public IRubyObject parse(ThreadContext context) throws SAXException,
123
+ IOException {
124
+ XMLReader xmlReader = XMLReaderFactory.createXMLReader();
125
+ InputSource inputSource;
126
+ if (this.string != null) {
127
+ inputSource = new InputSource(new StringReader(
128
+ this.string.asJavaString()));
129
+ } else {
130
+ FileReader reader = new FileReader(this.fileName.asJavaString());
131
+ inputSource = new InputSource(reader);
132
+ }
133
+
134
+ xmlReader.setEntityResolver(new EntityResolver() {
135
+ public InputSource resolveEntity(String publicId, String systemId)
136
+ throws SAXException, IOException {
137
+ return new InputSource(new StringReader(""));
138
+ }
139
+ });
140
+ if (callbacks != null && callbacks.isNil() == false)
141
+ xmlReader.setContentHandler(new CallbackHandler(context, callbacks));
142
+
143
+ xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler",xmlReader.getContentHandler());
144
+ try {
145
+ xmlReader.parse(inputSource);
146
+ } catch (SAXException e) {
147
+ throw ErrorJ.newRaiseException(context, e.getMessage());
148
+ }
149
+
150
+ return context.getRuntime().getTrue();
151
+ }
152
+
153
+ }