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.
- data/lib/smile-xml.jar +0 -0
- data/src/main/java/smile/xml/AttrJ.java +160 -0
- data/src/main/java/smile/xml/AttributesJ.java +212 -0
- data/src/main/java/smile/xml/BaseJ.java +60 -0
- data/src/main/java/smile/xml/DocumentJ.java +435 -0
- data/src/main/java/smile/xml/EncodingJ.java +94 -0
- data/src/main/java/smile/xml/ErrorJ.java +162 -0
- data/src/main/java/smile/xml/NamespaceJ.java +67 -0
- data/src/main/java/smile/xml/NamespacesJ.java +62 -0
- data/src/main/java/smile/xml/NodeJ.java +1029 -0
- data/src/main/java/smile/xml/NodeSetJ.java +90 -0
- data/src/main/java/smile/xml/ParserContextJ.java +44 -0
- data/src/main/java/smile/xml/ParserJ.java +196 -0
- data/src/main/java/smile/xml/ParserOptionsJ.java +58 -0
- data/src/main/java/smile/xml/ReaderJ.java +34 -0
- data/src/main/java/smile/xml/SchemaJ.java +66 -0
- data/src/main/java/smile/xml/SmileXML.java +65 -0
- data/src/main/java/smile/xml/XmlJ.java +58 -0
- data/src/main/java/smile/xml/sax/CallbackHandler.java +113 -0
- data/src/main/java/smile/xml/sax/SaxParserCallbacksJ.java +71 -0
- data/src/main/java/smile/xml/sax/SaxParserJ.java +153 -0
- data/src/main/java/smile/xml/util/UtilJ.java +424 -0
- data/src/main/java/smile/xml/xpath/CustomNamespaceContext.java +59 -0
- data/src/main/java/smile/xml/xpath/XPathContextJ.java +154 -0
- data/src/main/java/smile/xml/xpath/XPathExpressionJ.java +62 -0
- data/src/main/java/smile/xml/xpath/XPathJ.java +36 -0
- data/src/main/java/smile/xml/xpath/XPathObjectJ.java +196 -0
- data/src/main/java/smile/xml/xpath/XPointerJ.java +32 -0
- data/src/main/ruby/xml/libxml.rb +1 -0
- data/src/main/ruby/xml.rb +5 -0
- data/src/test/ruby/etc_doc_to_s.rb +21 -0
- data/src/test/ruby/ets_doc_file.rb +17 -0
- data/src/test/ruby/ets_doc_to_s.rb +23 -0
- data/src/test/ruby/ets_gpx.rb +28 -0
- data/src/test/ruby/ets_node_gc.rb +23 -0
- data/src/test/ruby/ets_test.xml +2 -0
- data/src/test/ruby/ets_tsr.rb +11 -0
- data/src/test/ruby/model/atom.xml +13 -0
- data/src/test/ruby/model/bands.iso-8859-1.xml +5 -0
- data/src/test/ruby/model/bands.utf-8.xml +5 -0
- data/src/test/ruby/model/bands.xml +5 -0
- data/src/test/ruby/model/books.xml +146 -0
- data/src/test/ruby/model/merge_bug_data.xml +58 -0
- data/src/test/ruby/model/ruby-lang.html +238 -0
- data/src/test/ruby/model/rubynet.xml +79 -0
- data/src/test/ruby/model/shiporder.rnc +28 -0
- data/src/test/ruby/model/shiporder.rng +86 -0
- data/src/test/ruby/model/shiporder.xml +23 -0
- data/src/test/ruby/model/shiporder.xsd +31 -0
- data/src/test/ruby/model/soap.xml +27 -0
- data/src/test/ruby/model/xinclude.xml +5 -0
- data/src/test/ruby/smile_xml_test.rb +64 -0
- data/src/test/ruby/tc_attr.rb +191 -0
- data/src/test/ruby/tc_attr_decl.rb +133 -0
- data/src/test/ruby/tc_attributes.rb +135 -0
- data/src/test/ruby/tc_deprecated_require.rb +13 -0
- data/src/test/ruby/tc_document.rb +162 -0
- data/src/test/ruby/tc_document_write.rb +212 -0
- data/src/test/ruby/tc_dtd.rb +125 -0
- data/src/test/ruby/tc_error.rb +150 -0
- data/src/test/ruby/tc_html_parser.rb +140 -0
- data/src/test/ruby/tc_namespace.rb +62 -0
- data/src/test/ruby/tc_namespaces.rb +210 -0
- data/src/test/ruby/tc_node.rb +273 -0
- data/src/test/ruby/tc_node_cdata.rb +51 -0
- data/src/test/ruby/tc_node_comment.rb +33 -0
- data/src/test/ruby/tc_node_copy.rb +42 -0
- data/src/test/ruby/tc_node_edit.rb +178 -0
- data/src/test/ruby/tc_node_text.rb +73 -0
- data/src/test/ruby/tc_node_write.rb +108 -0
- data/src/test/ruby/tc_node_xlink.rb +29 -0
- data/src/test/ruby/tc_parser.rb +371 -0
- data/src/test/ruby/tc_parser_context.rb +189 -0
- data/src/test/ruby/tc_properties.rb +40 -0
- data/src/test/ruby/tc_reader.rb +306 -0
- data/src/test/ruby/tc_relaxng.rb +54 -0
- data/src/test/ruby/tc_sax_parser.rb +340 -0
- data/src/test/ruby/tc_schema.rb +59 -0
- data/src/test/ruby/tc_traversal.rb +222 -0
- data/src/test/ruby/tc_xinclude.rb +21 -0
- data/src/test/ruby/tc_xml.rb +226 -0
- data/src/test/ruby/tc_xpath.rb +210 -0
- data/src/test/ruby/tc_xpath_context.rb +80 -0
- data/src/test/ruby/tc_xpath_expression.rb +38 -0
- data/src/test/ruby/tc_xpointer.rb +74 -0
- data/src/test/ruby/test_helper.rb +23 -0
- data/src/test/ruby/test_suite.rb +41 -0
- metadata +91 -5
@@ -0,0 +1,435 @@
|
|
1
|
+
package smile.xml;
|
2
|
+
|
3
|
+
import java.io.File;
|
4
|
+
import java.io.FileWriter;
|
5
|
+
import java.io.IOException;
|
6
|
+
import java.util.ArrayList;
|
7
|
+
import java.util.List;
|
8
|
+
import java.util.Map;
|
9
|
+
import java.util.Map.Entry;
|
10
|
+
import java.util.concurrent.atomic.AtomicBoolean;
|
11
|
+
|
12
|
+
import javax.xml.transform.dom.DOMSource;
|
13
|
+
import javax.xml.validation.Schema;
|
14
|
+
import javax.xml.validation.Validator;
|
15
|
+
|
16
|
+
import org.jruby.Ruby;
|
17
|
+
import org.jruby.RubyArray;
|
18
|
+
import org.jruby.RubyBoolean;
|
19
|
+
import org.jruby.RubyClass;
|
20
|
+
import org.jruby.RubyFixnum;
|
21
|
+
import org.jruby.RubyHash;
|
22
|
+
import org.jruby.RubyNil;
|
23
|
+
import org.jruby.RubyString;
|
24
|
+
import org.jruby.RubySymbol;
|
25
|
+
import org.jruby.anno.JRubyClass;
|
26
|
+
import org.jruby.anno.JRubyMethod;
|
27
|
+
import org.jruby.runtime.Block;
|
28
|
+
import org.jruby.runtime.ObjectAllocator;
|
29
|
+
import org.jruby.runtime.ThreadContext;
|
30
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
31
|
+
import org.w3c.dom.Document;
|
32
|
+
import org.w3c.dom.Node;
|
33
|
+
import org.xml.sax.ErrorHandler;
|
34
|
+
import org.xml.sax.SAXException;
|
35
|
+
import org.xml.sax.SAXParseException;
|
36
|
+
|
37
|
+
import smile.xml.util.UtilJ;
|
38
|
+
import smile.xml.xpath.XPathContextJ;
|
39
|
+
import smile.xml.xpath.XPathExpressionJ;
|
40
|
+
import smile.xml.xpath.XPathObjectJ;
|
41
|
+
|
42
|
+
@JRubyClass( name="LibXML::XML::Document" )
|
43
|
+
public class DocumentJ extends BaseJ<Document> {
|
44
|
+
private static final long serialVersionUID = 1585911078348739867L;
|
45
|
+
private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
|
46
|
+
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
|
47
|
+
return new DocumentJ(runtime, klass);
|
48
|
+
}
|
49
|
+
};
|
50
|
+
|
51
|
+
private RubyString version;
|
52
|
+
private EncodingJ encoding;
|
53
|
+
|
54
|
+
public static RubyClass define(Ruby runtime) {
|
55
|
+
return UtilJ.defineClass(runtime, DocumentJ.class, ALLOCATOR);
|
56
|
+
}
|
57
|
+
|
58
|
+
private static RubyClass getRubyClass(Ruby runtime) {
|
59
|
+
return UtilJ.getClass(runtime, DocumentJ.class );
|
60
|
+
}
|
61
|
+
|
62
|
+
public static DocumentJ newInstance(ThreadContext context) {
|
63
|
+
return (DocumentJ) getRubyClass(context.getRuntime()).newInstance(
|
64
|
+
context, new IRubyObject[0], null);
|
65
|
+
}
|
66
|
+
|
67
|
+
@JRubyMethod(name = { "file" }, module = true)
|
68
|
+
public static DocumentJ fromFile(ThreadContext context, IRubyObject klass,
|
69
|
+
IRubyObject pFile) throws Exception {
|
70
|
+
ParserJ parser = ParserJ.fromFile(context, pFile,RubyHash.newHash(context.getRuntime()));
|
71
|
+
|
72
|
+
return parser.parse(context);
|
73
|
+
}
|
74
|
+
|
75
|
+
@JRubyMethod(name = { "io" }, module = true)
|
76
|
+
public static DocumentJ fromIo(ThreadContext context, IRubyObject klass,
|
77
|
+
IRubyObject pIo) throws Exception {
|
78
|
+
ParserJ parser = ParserJ.fromIo(context, null, pIo);
|
79
|
+
return parser.parse(context);
|
80
|
+
}
|
81
|
+
|
82
|
+
@JRubyMethod(name = { "string" }, module = true)
|
83
|
+
public static DocumentJ fromString(ThreadContext context,
|
84
|
+
IRubyObject klass, IRubyObject pString) throws Exception {
|
85
|
+
IRubyObject[] args = { pString };
|
86
|
+
ParserJ parser = ParserJ.fromString(context, null, args);
|
87
|
+
return parser.parse(context);
|
88
|
+
}
|
89
|
+
|
90
|
+
public DocumentJ(Ruby runtime, RubyClass klass) {
|
91
|
+
super(runtime, klass);
|
92
|
+
}
|
93
|
+
|
94
|
+
@JRubyMethod(name = { "initialize" }, optional = 1)
|
95
|
+
public void initialize(ThreadContext context, IRubyObject[] args) {
|
96
|
+
if (args.length > 0) {
|
97
|
+
this.version = (RubyString) args[0];
|
98
|
+
} else {
|
99
|
+
this.version = context.getRuntime().newString("1.0");
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
@JRubyMethod( name="node_type" )
|
104
|
+
public IRubyObject getNodeType( ThreadContext context ) {
|
105
|
+
return context.getRuntime().newFixnum( NodeJ.DOCUMENT_NODE );
|
106
|
+
}
|
107
|
+
|
108
|
+
@JRubyMethod(name = { "root=" })
|
109
|
+
public void setRoot(ThreadContext context, IRubyObject pRoot) throws Exception {
|
110
|
+
NodeJ node = (NodeJ) pRoot;
|
111
|
+
|
112
|
+
if( node.isDocPresent() && getJavaObject().equals( node.getJavaObject().getOwnerDocument() ) == false )
|
113
|
+
throw ErrorJ.newRaiseException(context, " Nodes belong to different documents. You must first import the node by calling XML::Document.import.");
|
114
|
+
|
115
|
+
if (getJavaObject() == null) {
|
116
|
+
setJavaObject( UtilJ.getBuilder().newDocument() );
|
117
|
+
} else if( getJavaObject().getDocumentElement() != null) {
|
118
|
+
getJavaObject().removeChild( getJavaObject().getDocumentElement() );
|
119
|
+
}
|
120
|
+
//System.out.println( UtilJ.toString( node.getJavaObject(), true ) );
|
121
|
+
getJavaObject().appendChild( getJavaObject().adoptNode( node.getJavaObject() ) );
|
122
|
+
}
|
123
|
+
|
124
|
+
@JRubyMethod(name = { "root" })
|
125
|
+
public NodeJ getRoot(ThreadContext context) {
|
126
|
+
NodeJ node = NodeJ.newInstance(context);
|
127
|
+
node.setJavaObject(((Document) getJavaObject()).getDocumentElement());
|
128
|
+
node.setDocPresent(true);
|
129
|
+
return node;
|
130
|
+
}
|
131
|
+
|
132
|
+
@JRubyMethod(name = "import")
|
133
|
+
public NodeJ adoptNode(ThreadContext context, IRubyObject pNode) {
|
134
|
+
|
135
|
+
NodeJ node = (NodeJ) pNode;
|
136
|
+
Node newNode = getJavaObject().adoptNode( node.getJavaObject().cloneNode(false) );
|
137
|
+
|
138
|
+
NodeJ result = NodeJ.newInstance(context);
|
139
|
+
result.setJavaObject( newNode );
|
140
|
+
result.setDocPresent(true);
|
141
|
+
return result;
|
142
|
+
}
|
143
|
+
|
144
|
+
@JRubyMethod(name = { "node_type_name" })
|
145
|
+
public RubyString getNodetypeName(ThreadContext context) throws Exception {
|
146
|
+
|
147
|
+
return context.getRuntime().newString("document_xml");
|
148
|
+
}
|
149
|
+
|
150
|
+
@JRubyMethod(name ="canonicalize", rest=true )
|
151
|
+
public RubyString canonicalize(ThreadContext context, IRubyObject[] args ) throws Exception {
|
152
|
+
|
153
|
+
boolean keepComment = false;
|
154
|
+
|
155
|
+
if( args.length > 0 && args[0] instanceof RubyBoolean ) {
|
156
|
+
keepComment = ((RubyBoolean)args[0]).isTrue();
|
157
|
+
|
158
|
+
}
|
159
|
+
|
160
|
+
String string = toString();
|
161
|
+
string = string.replace("\r", "" );
|
162
|
+
|
163
|
+
if( keepComment == false )
|
164
|
+
for( int i = string.indexOf("<!--"); i != -1; i = string.indexOf("<!--") ) {
|
165
|
+
int j = string.indexOf("-->");
|
166
|
+
string = string.substring( 0, i ) + string.substring(j+3);
|
167
|
+
}
|
168
|
+
|
169
|
+
if( string.trim().startsWith("<?xml") ) {
|
170
|
+
int i = string.indexOf( "?>" );
|
171
|
+
string = string.substring( i+2 );
|
172
|
+
}
|
173
|
+
|
174
|
+
return context.getRuntime().newString(string);
|
175
|
+
}
|
176
|
+
|
177
|
+
@JRubyMethod(name ="child")
|
178
|
+
public NodeJ getChild(ThreadContext context) {
|
179
|
+
NodeJ node = NodeJ.newInstance(context);
|
180
|
+
node.setJavaObject(((Document) getJavaObject()).getFirstChild());
|
181
|
+
node.setDocPresent( true );
|
182
|
+
return node;
|
183
|
+
}
|
184
|
+
|
185
|
+
@JRubyMethod( name = "child?" )
|
186
|
+
public RubyBoolean hasChild(ThreadContext context) {
|
187
|
+
return UtilJ.toBool( context, getJavaObject().getFirstChild() != null );
|
188
|
+
}
|
189
|
+
|
190
|
+
@JRubyMethod(name = "compression" )
|
191
|
+
public RubyFixnum getCompression(ThreadContext context) {
|
192
|
+
// TODO
|
193
|
+
return context.getRuntime().newFixnum(0);
|
194
|
+
}
|
195
|
+
|
196
|
+
@JRubyMethod(name = { "compression=" })
|
197
|
+
public void setCompression(ThreadContext context, IRubyObject value) {
|
198
|
+
// TODO
|
199
|
+
}
|
200
|
+
|
201
|
+
@JRubyMethod(name = { "compression?" })
|
202
|
+
public RubyBoolean hasCompression(ThreadContext context) {
|
203
|
+
return UtilJ.toBool(context, false);
|
204
|
+
}
|
205
|
+
|
206
|
+
@JRubyMethod(name ="context", optional=1)
|
207
|
+
public IRubyObject getContext(ThreadContext context, IRubyObject[] args ) {
|
208
|
+
if( args.length > 0 ) {
|
209
|
+
NamespacesJ namespaces = (NamespacesJ) args[0];
|
210
|
+
}
|
211
|
+
|
212
|
+
return XPathContextJ.newInstance(context, this);
|
213
|
+
}
|
214
|
+
|
215
|
+
@JRubyMethod( name="encoding" )
|
216
|
+
public IRubyObject getEncoding(ThreadContext context) {
|
217
|
+
return this.encoding == null ? null : this.encoding.toConstant(context);
|
218
|
+
}
|
219
|
+
|
220
|
+
@JRubyMethod( name="encoding=" )
|
221
|
+
public void setEncoding(ThreadContext context, IRubyObject value) {
|
222
|
+
if( value instanceof RubyString ) {
|
223
|
+
this.encoding = EncodingJ.newInstance(context, (RubyString) value );
|
224
|
+
} else if (value instanceof EncodingJ) {
|
225
|
+
this.encoding = (EncodingJ) value;
|
226
|
+
} else if (value instanceof RubyNil ) {
|
227
|
+
this.encoding = null;
|
228
|
+
} else {
|
229
|
+
throw context.getRuntime().newArgumentError("unsupported " + value.getMetaClass().getName() );
|
230
|
+
}
|
231
|
+
}
|
232
|
+
|
233
|
+
@JRubyMethod(name = { "find" }, required = 1, optional = 1)
|
234
|
+
public IRubyObject find(ThreadContext context, IRubyObject[] args) {
|
235
|
+
|
236
|
+
if( args.length == 0 || ( ( args[0] instanceof RubyString ) == false && ( args[0] instanceof XPathExpressionJ ) == false ) ) {
|
237
|
+
|
238
|
+
throw context.getRuntime().newTypeError("Argument should be an intance of a String or XPath::Expression");
|
239
|
+
}
|
240
|
+
|
241
|
+
IRubyObject xpath = args[0];
|
242
|
+
|
243
|
+
IRubyObject[] namespaces;
|
244
|
+
if( args.length > 1 && ( args[1] instanceof RubyString || args[1] instanceof RubyArray ) )
|
245
|
+
namespaces = UtilJ.toStringArray(context, args, 1);
|
246
|
+
else if( args.length > 1 && args[1] instanceof RubyHash ) {
|
247
|
+
Map map = (Map) args[1];
|
248
|
+
List<IRubyObject> list = new ArrayList<IRubyObject>(map.size());
|
249
|
+
for( Object o : map.entrySet() ) {
|
250
|
+
Map.Entry e = (Entry) o;
|
251
|
+
String k = toJavaString(context, e.getKey() );
|
252
|
+
String v = toJavaString(context, e.getValue() );
|
253
|
+
list.add( toRubyString(context, k + ":" + v ) );
|
254
|
+
}
|
255
|
+
namespaces = list.toArray( new IRubyObject[ list.size() ] );
|
256
|
+
} else {
|
257
|
+
namespaces = new IRubyObject[]{};
|
258
|
+
}
|
259
|
+
|
260
|
+
return XPathObjectJ.newInstance( context, xpath, this, namespaces );
|
261
|
+
}
|
262
|
+
|
263
|
+
@JRubyMethod(name = "debug" )
|
264
|
+
public IRubyObject debug(ThreadContext context) {
|
265
|
+
return context.getRuntime().getFalse();
|
266
|
+
}
|
267
|
+
|
268
|
+
@JRubyMethod(name = "save", rest=true )
|
269
|
+
public IRubyObject save(ThreadContext context, IRubyObject[] args ) throws IOException {
|
270
|
+
|
271
|
+
RubyString file = (RubyString) args[0];
|
272
|
+
Boolean indent = null;
|
273
|
+
String encoding = null;
|
274
|
+
if( args.length > 1 ) {
|
275
|
+
RubyHash hash = (RubyHash) args[1];
|
276
|
+
Object tmp = hash.get( context.getRuntime().newSymbol("indent") );
|
277
|
+
indent = UtilJ.toJavaBoolean( context, tmp );
|
278
|
+
tmp = hash.get( context.getRuntime().newSymbol("encoding") );
|
279
|
+
encoding = UtilJ.toJavaString( context, tmp );
|
280
|
+
}
|
281
|
+
|
282
|
+
File f = new File( file.asJavaString() );
|
283
|
+
FileWriter writer = new FileWriter( file.asJavaString() );
|
284
|
+
try {
|
285
|
+
UtilJ.write(writer, getJavaObject(), nvl( indent, true ), nvl( encoding, EncodingJ.UTF_8 ) );
|
286
|
+
} finally {
|
287
|
+
writer.close();
|
288
|
+
}
|
289
|
+
|
290
|
+
return context.getRuntime().newFixnum( f.length() );
|
291
|
+
}
|
292
|
+
|
293
|
+
@JRubyMethod(name = { "version" })
|
294
|
+
public IRubyObject getVersion(ThreadContext context) throws Exception {
|
295
|
+
if( this.version == null )
|
296
|
+
return context.getRuntime().getNil();
|
297
|
+
return this.version;
|
298
|
+
}
|
299
|
+
|
300
|
+
|
301
|
+
@JRubyMethod(name ="xhtml?")
|
302
|
+
public IRubyObject isXhtml(ThreadContext context) throws Exception {
|
303
|
+
// TODO
|
304
|
+
return UtilJ.toBool(context, false);
|
305
|
+
}
|
306
|
+
|
307
|
+
@JRubyMethod(name = { "find_first" }, optional = 1)
|
308
|
+
public IRubyObject findFirst(ThreadContext context, IRubyObject[] args)
|
309
|
+
throws Exception {
|
310
|
+
RubyString xpath = (RubyString) args[0];
|
311
|
+
if (args.length > 0)
|
312
|
+
;
|
313
|
+
return XPathObjectJ.newInstance(context, xpath, this,
|
314
|
+
new IRubyObject[0]).getFirst(context);
|
315
|
+
}
|
316
|
+
|
317
|
+
@Override
|
318
|
+
public String toString() {
|
319
|
+
return UtilJ.toString( getJavaObject(), true, encoding );
|
320
|
+
}
|
321
|
+
|
322
|
+
@JRubyMethod(name="to_s", rest=true)
|
323
|
+
public RubyString toString(ThreadContext context, IRubyObject[] args ) throws Exception {
|
324
|
+
RubyBoolean indent;
|
325
|
+
EncodingJ encoding = null;
|
326
|
+
if( args.length > 0 ) {
|
327
|
+
if( args[0] instanceof RubyHash ) {
|
328
|
+
RubyHash hash = (RubyHash) args[0];
|
329
|
+
RubySymbol key = context.getRuntime().newSymbol( "indent" );
|
330
|
+
indent = toRubyBool( context, hash.get( key ) );
|
331
|
+
key = context.getRuntime().newSymbol( "encoding" );
|
332
|
+
encoding = EncodingJ.get(context, hash.get( key ) );
|
333
|
+
} else if( args[0] instanceof RubyBoolean ) {
|
334
|
+
indent = (RubyBoolean) args[0];
|
335
|
+
} else if( args[0] instanceof RubyNil ) {
|
336
|
+
|
337
|
+
} else {
|
338
|
+
throw context.getRuntime().newArgumentError("");
|
339
|
+
}
|
340
|
+
}
|
341
|
+
|
342
|
+
String string = UtilJ.toString( getJavaObject(), true, encoding );
|
343
|
+
return context.getRuntime().newString(string);
|
344
|
+
}
|
345
|
+
|
346
|
+
private RubyBoolean toRubyBool( ThreadContext context, Object obj ) {
|
347
|
+
if( obj instanceof RubyBoolean ) {
|
348
|
+
return (RubyBoolean) obj;
|
349
|
+
}
|
350
|
+
if( obj instanceof Boolean ) {
|
351
|
+
return context.getRuntime().newBoolean( (Boolean) obj );
|
352
|
+
}
|
353
|
+
if( obj == null ) {
|
354
|
+
return null;
|
355
|
+
}
|
356
|
+
if( obj == null || obj instanceof RubyNil ) {
|
357
|
+
return null;
|
358
|
+
}
|
359
|
+
throw context.getRuntime().newArgumentError("");
|
360
|
+
}
|
361
|
+
|
362
|
+
@JRubyMethod(name = { "eql?" }, alias = { "==", "equal?" })
|
363
|
+
public RubyBoolean isEql(ThreadContext context, IRubyObject arg) {
|
364
|
+
boolean r = (arg instanceof BaseJ) ? ((BaseJ) arg).getJavaObject()
|
365
|
+
.equals(getJavaObject()) : false;
|
366
|
+
|
367
|
+
if (!r) {
|
368
|
+
r = toString().equals( arg.toString() );
|
369
|
+
}
|
370
|
+
return r ? context.getRuntime().getTrue() : context.getRuntime()
|
371
|
+
.getFalse();
|
372
|
+
}
|
373
|
+
|
374
|
+
@JRubyMethod(name = { "validate_schema" }, optional = 1)
|
375
|
+
public IRubyObject validateSchema(final ThreadContext context,
|
376
|
+
IRubyObject[] args, final Block block) throws IOException {
|
377
|
+
SchemaJ schema = (SchemaJ) args[0];
|
378
|
+
|
379
|
+
Validator validator = ((Schema) schema.getJavaObject()).newValidator();
|
380
|
+
final Block handler = ErrorJ.getErrorHandler(context, null);
|
381
|
+
final AtomicBoolean b = new AtomicBoolean(true);
|
382
|
+
if( handler != null || ( block != null && block.isGiven() ) )
|
383
|
+
validator.setErrorHandler(new ErrorHandler() {
|
384
|
+
|
385
|
+
private void callHandler( IRubyObject message, IRubyObject exception ) {
|
386
|
+
IRubyObject[] args = { message, exception };
|
387
|
+
handler.call(context, args, null );
|
388
|
+
}
|
389
|
+
|
390
|
+
private void callBlock( IRubyObject exception ) {
|
391
|
+
IRubyObject[] args = { exception };
|
392
|
+
block.call(context, args, null );
|
393
|
+
}
|
394
|
+
|
395
|
+
private void invoke( Exception exception ) {
|
396
|
+
RubyString message = context.getRuntime().newString( exception.getMessage());
|
397
|
+
IRubyObject obj = ErrorJ.newInstance( context, exception.getMessage() );
|
398
|
+
if( block != null && block.isGiven() )
|
399
|
+
callBlock( obj );
|
400
|
+
if( handler != null && handler.isGiven() )
|
401
|
+
callHandler(message, obj );
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
public void warning(SAXParseException exception)
|
406
|
+
throws SAXException {
|
407
|
+
|
408
|
+
invoke( exception );
|
409
|
+
b.set(false);
|
410
|
+
}
|
411
|
+
|
412
|
+
public void fatalError(SAXParseException exception)
|
413
|
+
throws SAXException {
|
414
|
+
|
415
|
+
invoke( exception );
|
416
|
+
b.set(false);
|
417
|
+
}
|
418
|
+
|
419
|
+
public void error(SAXParseException exception)
|
420
|
+
throws SAXException {
|
421
|
+
|
422
|
+
invoke( exception );
|
423
|
+
b.set(false);
|
424
|
+
}
|
425
|
+
});
|
426
|
+
try {
|
427
|
+
validator.validate(new DOMSource(getJavaObject() ));
|
428
|
+
} catch (SAXException e) {
|
429
|
+
throw ErrorJ.newRaiseException(context, e.getMessage());
|
430
|
+
}
|
431
|
+
|
432
|
+
return b.get() ? context.getRuntime().getTrue() : context.getRuntime()
|
433
|
+
.getFalse();
|
434
|
+
}
|
435
|
+
}
|
@@ -0,0 +1,94 @@
|
|
1
|
+
package smile.xml;
|
2
|
+
|
3
|
+
import org.jruby.Ruby;
|
4
|
+
import org.jruby.RubyClass;
|
5
|
+
import org.jruby.RubyObject;
|
6
|
+
import org.jruby.RubyString;
|
7
|
+
import org.jruby.anno.JRubyClass;
|
8
|
+
import org.jruby.anno.JRubyConstant;
|
9
|
+
import org.jruby.anno.JRubyMethod;
|
10
|
+
import org.jruby.runtime.ObjectAllocator;
|
11
|
+
import org.jruby.runtime.ThreadContext;
|
12
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
13
|
+
|
14
|
+
import smile.xml.util.UtilJ;
|
15
|
+
|
16
|
+
@JRubyClass( name="LibXML::XML::Encoding" )
|
17
|
+
public class EncodingJ extends RubyObject {
|
18
|
+
|
19
|
+
private static final long serialVersionUID = -3033814105839661460L;
|
20
|
+
|
21
|
+
private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
|
22
|
+
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
|
23
|
+
return new EncodingJ(runtime, klass);
|
24
|
+
}
|
25
|
+
};
|
26
|
+
|
27
|
+
public static RubyClass define(Ruby runtime) {
|
28
|
+
RubyClass c = UtilJ.defineClass( runtime, EncodingJ.class, ALLOCATOR );
|
29
|
+
c.setConstant("UTF_8", runtime.newString("UTF-8") );
|
30
|
+
c.setConstant("ISO_8859_1", runtime.newString("ISO-8859-1") );
|
31
|
+
return c;
|
32
|
+
}
|
33
|
+
|
34
|
+
@JRubyConstant
|
35
|
+
public static final String NONE = null;
|
36
|
+
|
37
|
+
// @JRubyConstant
|
38
|
+
public static final String UTF_8 = "UTF-8";
|
39
|
+
//
|
40
|
+
// @JRubyConstant
|
41
|
+
// public static final String ISO_8859_1 = "ISO-8859-1";
|
42
|
+
|
43
|
+
public static EncodingJ get(ThreadContext context, Object object ) {
|
44
|
+
if( object == null )
|
45
|
+
return null;
|
46
|
+
if( object instanceof EncodingJ )
|
47
|
+
return (EncodingJ) object;
|
48
|
+
if( object instanceof String )
|
49
|
+
return newInstance(context, (String) object );
|
50
|
+
if( object instanceof RubyString )
|
51
|
+
return newInstance(context, (RubyString) object );
|
52
|
+
throw context.getRuntime().newArgumentError("");
|
53
|
+
}
|
54
|
+
|
55
|
+
public static EncodingJ newInstance(ThreadContext context, RubyString string) {
|
56
|
+
IRubyObject[] args = { string };
|
57
|
+
EncodingJ encoding = (EncodingJ) getRubyClass( context.getRuntime() ).newInstance(context, args, null );
|
58
|
+
return encoding;
|
59
|
+
}
|
60
|
+
|
61
|
+
public static EncodingJ newInstance(ThreadContext context, String string) {
|
62
|
+
return newInstance( context, context.getRuntime().newString(string) );
|
63
|
+
}
|
64
|
+
|
65
|
+
public static RubyClass getRubyClass(Ruby runtime) {
|
66
|
+
return UtilJ.getClass(runtime, "LibXML", "XML", "Encoding");
|
67
|
+
}
|
68
|
+
|
69
|
+
private EncodingJ(Ruby runtime, RubyClass metaClass) {
|
70
|
+
super(runtime, metaClass);
|
71
|
+
}
|
72
|
+
|
73
|
+
private RubyString encoding;
|
74
|
+
|
75
|
+
@JRubyMethod(name = { "initialize" }, rest = true)
|
76
|
+
public void initialize(ThreadContext context, IRubyObject[] args) {
|
77
|
+
if (args.length > 0 && args[0] instanceof RubyString) {
|
78
|
+
this.encoding = (RubyString) args[0];
|
79
|
+
this.encoding = this.encoding.upcase(context);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
@Override
|
84
|
+
public String asJavaString() {
|
85
|
+
return encoding == null ? "" : encoding.asJavaString();
|
86
|
+
}
|
87
|
+
|
88
|
+
public IRubyObject toConstant( ThreadContext context ) {
|
89
|
+
|
90
|
+
return encoding;
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
}
|
@@ -0,0 +1,162 @@
|
|
1
|
+
package smile.xml;
|
2
|
+
|
3
|
+
import java.util.concurrent.atomic.AtomicReference;
|
4
|
+
|
5
|
+
import org.jruby.Ruby;
|
6
|
+
import org.jruby.RubyClass;
|
7
|
+
import org.jruby.RubyException;
|
8
|
+
import org.jruby.RubyModule;
|
9
|
+
import org.jruby.anno.JRubyClass;
|
10
|
+
import org.jruby.anno.JRubyConstant;
|
11
|
+
import org.jruby.anno.JRubyMethod;
|
12
|
+
import org.jruby.exceptions.RaiseException;
|
13
|
+
import org.jruby.runtime.Block;
|
14
|
+
import org.jruby.runtime.ObjectAllocator;
|
15
|
+
import org.jruby.runtime.ThreadContext;
|
16
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
17
|
+
|
18
|
+
import smile.xml.util.UtilJ;
|
19
|
+
|
20
|
+
@JRubyClass( name="LibXML::XML::Error", parent="StandardError" )
|
21
|
+
public class ErrorJ extends RubyException {
|
22
|
+
|
23
|
+
private static final long serialVersionUID = -8185698427872540911L;
|
24
|
+
|
25
|
+
private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
|
26
|
+
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
|
27
|
+
return new ErrorJ(runtime, klass);
|
28
|
+
}
|
29
|
+
};
|
30
|
+
|
31
|
+
private static final AtomicReference<Block> handler = new AtomicReference<Block>();
|
32
|
+
|
33
|
+
public static RubyClass define(Ruby runtime) {
|
34
|
+
return UtilJ.defineClass(runtime, ErrorJ.class, ALLOCATOR);
|
35
|
+
}
|
36
|
+
|
37
|
+
private static RubyClass getRubyClass(Ruby runtime) {
|
38
|
+
return UtilJ.getClass(runtime, ErrorJ.class );
|
39
|
+
}
|
40
|
+
|
41
|
+
public static RaiseException newRaiseException(ThreadContext context,
|
42
|
+
String message) {
|
43
|
+
Ruby run = context.getRuntime();
|
44
|
+
return new RaiseException(run, getRubyClass(context.getRuntime()), message, true);
|
45
|
+
}
|
46
|
+
|
47
|
+
public static RubyException newInstance(ThreadContext context,
|
48
|
+
String message) {
|
49
|
+
return RubyException.newException(context.getRuntime(),getRubyClass(context.getRuntime()), message);
|
50
|
+
}
|
51
|
+
|
52
|
+
|
53
|
+
@JRubyConstant
|
54
|
+
public static final IRubyObject VERBOSE_HANDLER = null;
|
55
|
+
|
56
|
+
@JRubyConstant
|
57
|
+
public static final IRubyObject QUIET_HANDLER = null;
|
58
|
+
|
59
|
+
@JRubyConstant
|
60
|
+
public static final IRubyObject PARSER = null;
|
61
|
+
|
62
|
+
@JRubyConstant
|
63
|
+
public static final IRubyObject DOCUMENT_END = null;
|
64
|
+
|
65
|
+
@JRubyConstant
|
66
|
+
public static final IRubyObject FATAL = null;
|
67
|
+
|
68
|
+
@JRubyConstant
|
69
|
+
public static final IRubyObject TAG_NAME_MISMATCH = null;
|
70
|
+
|
71
|
+
@JRubyConstant
|
72
|
+
public static final IRubyObject SCHEMASV = null;
|
73
|
+
|
74
|
+
@JRubyConstant
|
75
|
+
public static final IRubyObject SCHEMAV_ELEMENT_CONTENT = null;
|
76
|
+
|
77
|
+
@JRubyConstant
|
78
|
+
public static final IRubyObject ERROR = null;
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
private ErrorJ(Ruby runtime, RubyClass metaClass) {
|
84
|
+
super(runtime, metaClass);
|
85
|
+
}
|
86
|
+
|
87
|
+
@JRubyMethod(name = "initialize")
|
88
|
+
public void initialize(ThreadContext context, IRubyObject pString) {
|
89
|
+
message = pString;
|
90
|
+
}
|
91
|
+
|
92
|
+
@JRubyMethod(name = "domain")
|
93
|
+
public IRubyObject getDomain(ThreadContext context) {
|
94
|
+
return context.getRuntime().getNil();
|
95
|
+
}
|
96
|
+
|
97
|
+
@JRubyMethod(name = "level")
|
98
|
+
public IRubyObject getLevel(ThreadContext context) {
|
99
|
+
return context.getRuntime().getNil();
|
100
|
+
}
|
101
|
+
|
102
|
+
@JRubyMethod(name = "code")
|
103
|
+
public IRubyObject getCode(ThreadContext context) {
|
104
|
+
return context.getRuntime().getNil();
|
105
|
+
}
|
106
|
+
|
107
|
+
@JRubyMethod(name = "file")
|
108
|
+
public IRubyObject getFile(ThreadContext context) {
|
109
|
+
return context.getRuntime().getNil();
|
110
|
+
}
|
111
|
+
|
112
|
+
@JRubyMethod(name = "line")
|
113
|
+
public IRubyObject getLine(ThreadContext context) {
|
114
|
+
return context.getRuntime().getNil();
|
115
|
+
}
|
116
|
+
|
117
|
+
@JRubyMethod(name = "str1")
|
118
|
+
public IRubyObject getStringOne(ThreadContext context) {
|
119
|
+
return context.getRuntime().getNil();
|
120
|
+
}
|
121
|
+
|
122
|
+
@JRubyMethod(name = "str2")
|
123
|
+
public IRubyObject getStringTwo(ThreadContext context) {
|
124
|
+
return context.getRuntime().getNil();
|
125
|
+
}
|
126
|
+
|
127
|
+
@JRubyMethod(name = "str3")
|
128
|
+
public IRubyObject getStringThree(ThreadContext context) {
|
129
|
+
return context.getRuntime().getNil();
|
130
|
+
}
|
131
|
+
|
132
|
+
@JRubyMethod(name = "int1")
|
133
|
+
public IRubyObject getIntOne(ThreadContext context) {
|
134
|
+
return context.getRuntime().getNil();
|
135
|
+
}
|
136
|
+
|
137
|
+
@JRubyMethod(name = "int2")
|
138
|
+
public IRubyObject getIntTwo(ThreadContext context) {
|
139
|
+
return context.getRuntime().getNil();
|
140
|
+
}
|
141
|
+
|
142
|
+
@JRubyMethod(name = "node")
|
143
|
+
public IRubyObject getNode(ThreadContext context) {
|
144
|
+
return context.getRuntime().getNil();
|
145
|
+
}
|
146
|
+
|
147
|
+
@JRubyMethod(name = "set_handler", module=true)
|
148
|
+
public static void setHandler(ThreadContext context, IRubyObject self, Block pHandler) {
|
149
|
+
handler.set(pHandler);
|
150
|
+
}
|
151
|
+
|
152
|
+
@JRubyMethod(name = "reset_handler", module=true)
|
153
|
+
public static void resetHandler(ThreadContext context, IRubyObject self) {
|
154
|
+
|
155
|
+
handler.set(null);
|
156
|
+
}
|
157
|
+
|
158
|
+
@JRubyMethod(name = "handler")
|
159
|
+
public static Block getErrorHandler(ThreadContext context, IRubyObject self) {
|
160
|
+
return (Block) handler.get();
|
161
|
+
}
|
162
|
+
}
|