File: NokogiriService.java

package info (click to toggle)
ruby-nokogiri 1.13.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,416 kB
  • sloc: ansic: 38,198; xml: 28,086; ruby: 22,271; java: 15,517; cpp: 7,037; yacc: 244; sh: 148; makefile: 136
file content (636 lines) | stat: -rw-r--r-- 26,482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package nokogiri;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyModule;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.BasicLibraryService;

/**
 * Class to provide Nokogiri. This class is used to make "require 'nokogiri'" work
 * in JRuby. Also, this class holds a Ruby type cache and allocators of Ruby types.
 *
 * @author headius
 * @author Yoko Harada <yokolet@gmail.com>
 */
public class NokogiriService implements BasicLibraryService
{
  public boolean
  basicLoad(Ruby ruby)
  {
    init(ruby);
    return true;
  }

  public static Map<String, RubyClass>
  getNokogiriClassCache(Ruby ruby)
  {
    return (Map<String, RubyClass>) ruby.getModule("Nokogiri").getInternalVariable("cache");
  }

  private static Map<String, RubyClass>
  populateNokogiriClassCahce(Ruby ruby)
  {
    Map<String, RubyClass> nokogiriClassCache = new HashMap<String, RubyClass>();
    nokogiriClassCache.put("Nokogiri::EncodingHandler", (RubyClass)ruby.getClassFromPath("Nokogiri::EncodingHandler"));
    nokogiriClassCache.put("Nokogiri::HTML4::Document", (RubyClass)ruby.getClassFromPath("Nokogiri::HTML4::Document"));
    nokogiriClassCache.put("Nokogiri::HTML4::ElementDescription",
                           (RubyClass)ruby.getClassFromPath("Nokogiri::HTML4::ElementDescription"));
    nokogiriClassCache.put("Nokogiri::XML::Attr", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Attr"));
    nokogiriClassCache.put("Nokogiri::XML::Document", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Document"));
    nokogiriClassCache.put("Nokogiri::XML::DocumentFragment",
                           (RubyClass)ruby.getClassFromPath("Nokogiri::XML::DocumentFragment"));
    nokogiriClassCache.put("Nokogiri::XML::DTD", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::DTD"));
    nokogiriClassCache.put("Nokogiri::XML::Text", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Text"));
    nokogiriClassCache.put("Nokogiri::XML::Comment", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Comment"));
    nokogiriClassCache.put("Nokogiri::XML::Element", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Element"));
    nokogiriClassCache.put("Nokogiri::XML::ElementContent",
                           (RubyClass)ruby.getClassFromPath("Nokogiri::XML::ElementContent"));
    nokogiriClassCache.put("Nokogiri::XML::ElementDecl", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::ElementDecl"));
    nokogiriClassCache.put("Nokogiri::XML::EntityDecl", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::EntityDecl"));
    nokogiriClassCache.put("Nokogiri::XML::EntityReference",
                           (RubyClass)ruby.getClassFromPath("Nokogiri::XML::EntityReference"));
    nokogiriClassCache.put("Nokogiri::XML::ProcessingInstruction",
                           (RubyClass)ruby.getClassFromPath("Nokogiri::XML::ProcessingInstruction"));
    nokogiriClassCache.put("Nokogiri::XML::CDATA", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::CDATA"));
    nokogiriClassCache.put("Nokogiri::XML::Node", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Node"));
    nokogiriClassCache.put("Nokogiri::XML::NodeSet", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::NodeSet"));
    nokogiriClassCache.put("Nokogiri::XML::Namespace", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Namespace"));
    nokogiriClassCache.put("Nokogiri::XML::SyntaxError", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::SyntaxError"));
    nokogiriClassCache.put("Nokogiri::XML::Reader", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Reader"));
    nokogiriClassCache.put("Nokogiri::XML::RelaxNG", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::RelaxNG"));
    nokogiriClassCache.put("Nokogiri::XML::Schema", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Schema"));
    nokogiriClassCache.put("Nokogiri::XML::XPathContext", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::XPathContext"));
    nokogiriClassCache.put("Nokogiri::XML::AttributeDecl",
                           (RubyClass)ruby.getClassFromPath("Nokogiri::XML::AttributeDecl"));
    nokogiriClassCache.put("Nokogiri::XML::SAX::ParserContext",
                           (RubyClass)ruby.getClassFromPath("Nokogiri::XML::SAX::ParserContext"));
    return Collections.unmodifiableMap(nokogiriClassCache);
  }

  private void
  init(Ruby ruby)
  {
    RubyModule nokogiri = ruby.defineModule("Nokogiri");
    RubyModule xmlModule = nokogiri.defineModuleUnder("XML");
    RubyModule xmlSaxModule = xmlModule.defineModuleUnder("SAX");
    RubyModule htmlModule = nokogiri.defineModuleUnder("HTML4");
    RubyModule htmlSaxModule = htmlModule.defineModuleUnder("SAX");
    RubyModule xsltModule = nokogiri.defineModuleUnder("XSLT");

    createJavaLibraryVersionConstants(ruby, nokogiri);
    createNokogiriModule(ruby, nokogiri);
    createSyntaxErrors(ruby, nokogiri, xmlModule);
    RubyClass xmlNode = createXmlModule(ruby, xmlModule);
    createHtmlModule(ruby, htmlModule);
    createDocuments(ruby, xmlModule, htmlModule, xmlNode);
    createSaxModule(ruby, xmlSaxModule, htmlSaxModule);
    createXsltModule(ruby, xsltModule);
    nokogiri.setInternalVariable("cache", populateNokogiriClassCahce(ruby));
  }

  private void
  createJavaLibraryVersionConstants(Ruby ruby, RubyModule nokogiri)
  {
    nokogiri.defineConstant("XERCES_VERSION", ruby.newString(org.apache.xerces.impl.Version.getVersion()));
    nokogiri.defineConstant("NEKO_VERSION", ruby.newString(org.cyberneko.html.Version.getVersion()));
  }

  private void
  createNokogiriModule(Ruby ruby, RubyModule nokogiri)
  {
    RubyClass encHandler = nokogiri.defineClassUnder("EncodingHandler", ruby.getObject(), ENCODING_HANDLER_ALLOCATOR);
    encHandler.defineAnnotatedMethods(EncodingHandler.class);
  }

  private void
  createSyntaxErrors(Ruby ruby, RubyModule nokogiri, RubyModule xmlModule)
  {
    RubyClass syntaxError = nokogiri.defineClassUnder("SyntaxError", ruby.getStandardError(),
                            ruby.getStandardError().getAllocator());
    RubyClass xmlSyntaxError = xmlModule.defineClassUnder("SyntaxError", syntaxError, XML_SYNTAXERROR_ALLOCATOR);
    xmlSyntaxError.defineAnnotatedMethods(XmlSyntaxError.class);
  }

  private RubyClass
  createXmlModule(Ruby ruby, RubyModule xmlModule)
  {
    RubyClass node = xmlModule.defineClassUnder("Node", ruby.getObject(), XML_NODE_ALLOCATOR);
    node.defineAnnotatedMethods(XmlNode.class);

    RubyClass attr = xmlModule.defineClassUnder("Attr", node, XML_ATTR_ALLOCATOR);
    attr.defineAnnotatedMethods(XmlAttr.class);

    RubyClass attrDecl = xmlModule.defineClassUnder("AttributeDecl", node, XML_ATTRIBUTE_DECL_ALLOCATOR);
    attrDecl.defineAnnotatedMethods(XmlAttributeDecl.class);

    RubyClass characterData = xmlModule.defineClassUnder("CharacterData", node, null);

    RubyClass comment = xmlModule.defineClassUnder("Comment", characterData, XML_COMMENT_ALLOCATOR);
    comment.defineAnnotatedMethods(XmlComment.class);

    RubyClass text = xmlModule.defineClassUnder("Text", characterData, XML_TEXT_ALLOCATOR);
    text.defineAnnotatedMethods(XmlText.class);

    RubyModule cdata = xmlModule.defineClassUnder("CDATA", text, XML_CDATA_ALLOCATOR);
    cdata.defineAnnotatedMethods(XmlCdata.class);

    RubyClass dtd = xmlModule.defineClassUnder("DTD", node, XML_DTD_ALLOCATOR);
    dtd.defineAnnotatedMethods(XmlDtd.class);

    RubyClass documentFragment = xmlModule.defineClassUnder("DocumentFragment", node, XML_DOCUMENT_FRAGMENT_ALLOCATOR);
    documentFragment.defineAnnotatedMethods(XmlDocumentFragment.class);

    RubyClass element = xmlModule.defineClassUnder("Element", node, XML_ELEMENT_ALLOCATOR);
    element.defineAnnotatedMethods(XmlElement.class);

    RubyClass elementContent = xmlModule.defineClassUnder("ElementContent", ruby.getObject(),
                               XML_ELEMENT_CONTENT_ALLOCATOR);
    elementContent.defineAnnotatedMethods(XmlElementContent.class);

    RubyClass elementDecl = xmlModule.defineClassUnder("ElementDecl", node, XML_ELEMENT_DECL_ALLOCATOR);
    elementDecl.defineAnnotatedMethods(XmlElementDecl.class);

    RubyClass entityDecl = xmlModule.defineClassUnder("EntityDecl", node, XML_ENTITY_DECL_ALLOCATOR);
    entityDecl.defineAnnotatedMethods(XmlEntityDecl.class);

    entityDecl.defineConstant("INTERNAL_GENERAL", RubyFixnum.newFixnum(ruby, XmlEntityDecl.INTERNAL_GENERAL));
    entityDecl.defineConstant("EXTERNAL_GENERAL_PARSED", RubyFixnum.newFixnum(ruby, XmlEntityDecl.EXTERNAL_GENERAL_PARSED));
    entityDecl.defineConstant("EXTERNAL_GENERAL_UNPARSED", RubyFixnum.newFixnum(ruby,
                              XmlEntityDecl.EXTERNAL_GENERAL_UNPARSED));
    entityDecl.defineConstant("INTERNAL_PARAMETER", RubyFixnum.newFixnum(ruby, XmlEntityDecl.INTERNAL_PARAMETER));
    entityDecl.defineConstant("EXTERNAL_PARAMETER", RubyFixnum.newFixnum(ruby, XmlEntityDecl.EXTERNAL_PARAMETER));
    entityDecl.defineConstant("INTERNAL_PREDEFINED", RubyFixnum.newFixnum(ruby, XmlEntityDecl.INTERNAL_PREDEFINED));

    RubyClass entref = xmlModule.defineClassUnder("EntityReference", node, XML_ENTITY_REFERENCE_ALLOCATOR);
    entref.defineAnnotatedMethods(XmlEntityReference.class);

    RubyClass namespace = xmlModule.defineClassUnder("Namespace", ruby.getObject(), XML_NAMESPACE_ALLOCATOR);
    namespace.defineAnnotatedMethods(XmlNamespace.class);

    RubyClass nodeSet = xmlModule.defineClassUnder("NodeSet", ruby.getObject(), XML_NODESET_ALLOCATOR);
    nodeSet.defineAnnotatedMethods(XmlNodeSet.class);

    RubyClass pi = xmlModule.defineClassUnder("ProcessingInstruction", node, XML_PROCESSING_INSTRUCTION_ALLOCATOR);
    pi.defineAnnotatedMethods(XmlProcessingInstruction.class);

    RubyClass reader = xmlModule.defineClassUnder("Reader", ruby.getObject(), XML_READER_ALLOCATOR);
    reader.defineAnnotatedMethods(XmlReader.class);

    RubyClass schema = xmlModule.defineClassUnder("Schema", ruby.getObject(), XML_SCHEMA_ALLOCATOR);
    schema.defineAnnotatedMethods(XmlSchema.class);

    RubyClass relaxng = xmlModule.defineClassUnder("RelaxNG", schema, XML_RELAXNG_ALLOCATOR);
    relaxng.defineAnnotatedMethods(XmlRelaxng.class);

    RubyClass xpathContext = xmlModule.defineClassUnder("XPathContext", ruby.getObject(), XML_XPATHCONTEXT_ALLOCATOR);
    xpathContext.defineAnnotatedMethods(XmlXpathContext.class);

    return node;
  }

  private void
  createHtmlModule(Ruby ruby, RubyModule htmlModule)
  {
    RubyClass htmlElemDesc = htmlModule.defineClassUnder("ElementDescription", ruby.getObject(),
                             HTML_ELEMENT_DESCRIPTION_ALLOCATOR);
    htmlElemDesc.defineAnnotatedMethods(Html4ElementDescription.class);

    RubyClass htmlEntityLookup = htmlModule.defineClassUnder("EntityLookup", ruby.getObject(),
                                 HTML_ENTITY_LOOKUP_ALLOCATOR);
    htmlEntityLookup.defineAnnotatedMethods(Html4EntityLookup.class);
  }

  private void
  createDocuments(Ruby ruby, RubyModule xmlModule, RubyModule htmlModule, RubyClass node)
  {
    RubyClass xmlDocument = xmlModule.defineClassUnder("Document", node, XML_DOCUMENT_ALLOCATOR);
    xmlDocument.defineAnnotatedMethods(XmlDocument.class);

    //RubyModule htmlDoc = html.defineOrGetClassUnder("Document", document);
    RubyModule htmlDocument = htmlModule.defineClassUnder("Document", xmlDocument, HTML_DOCUMENT_ALLOCATOR);
    htmlDocument.defineAnnotatedMethods(Html4Document.class);
  }

  private void
  createSaxModule(Ruby ruby, RubyModule xmlSaxModule, RubyModule htmlSaxModule)
  {
    RubyClass xmlSaxParserContext = xmlSaxModule.defineClassUnder("ParserContext", ruby.getObject(),
                                    XML_SAXPARSER_CONTEXT_ALLOCATOR);
    xmlSaxParserContext.defineAnnotatedMethods(XmlSaxParserContext.class);

    RubyClass xmlSaxPushParser = xmlSaxModule.defineClassUnder("PushParser", ruby.getObject(), XML_SAXPUSHPARSER_ALLOCATOR);
    xmlSaxPushParser.defineAnnotatedMethods(XmlSaxPushParser.class);

    RubyClass htmlSaxPushParser = htmlSaxModule.defineClassUnder("PushParser", ruby.getObject(),
                                  HTML_SAXPUSHPARSER_ALLOCATOR);
    htmlSaxPushParser.defineAnnotatedMethods(Html4SaxPushParser.class);

    RubyClass htmlSaxParserContext = htmlSaxModule.defineClassUnder("ParserContext", xmlSaxParserContext,
                                     HTML_SAXPARSER_CONTEXT_ALLOCATOR);
    htmlSaxParserContext.defineAnnotatedMethods(Html4SaxParserContext.class);
  }

  private void
  createXsltModule(Ruby ruby, RubyModule xsltModule)
  {
    RubyClass stylesheet = xsltModule.defineClassUnder("Stylesheet", ruby.getObject(), XSLT_STYLESHEET_ALLOCATOR);
    stylesheet.defineAnnotatedMethods(XsltStylesheet.class);
    xsltModule.defineAnnotatedMethod(XsltStylesheet.class, "register");
  }

  private static ObjectAllocator ENCODING_HANDLER_ALLOCATOR = new ObjectAllocator()
  {
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new EncodingHandler(runtime, klazz, "");
    }
  };

  public static final ObjectAllocator HTML_DOCUMENT_ALLOCATOR = new ObjectAllocator()
  {
    private Html4Document htmlDocument = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (htmlDocument == null) { htmlDocument = new Html4Document(runtime, klazz); }
      try {
        Html4Document clone = (Html4Document) htmlDocument.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new Html4Document(runtime, klazz);
      }
    }
  };

  private static final ObjectAllocator HTML_SAXPARSER_CONTEXT_ALLOCATOR = new ObjectAllocator()
  {
    private Html4SaxParserContext htmlSaxParserContext = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (htmlSaxParserContext == null) { htmlSaxParserContext = new Html4SaxParserContext(runtime, klazz); }
      try {
        Html4SaxParserContext clone = (Html4SaxParserContext) htmlSaxParserContext.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new Html4SaxParserContext(runtime, klazz);
      }
    }
  };

  private static ObjectAllocator HTML_ELEMENT_DESCRIPTION_ALLOCATOR =
    new ObjectAllocator()
  {
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new Html4ElementDescription(runtime, klazz);
    }
  };

  private static ObjectAllocator HTML_ENTITY_LOOKUP_ALLOCATOR =
    new ObjectAllocator()
  {
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new Html4EntityLookup(runtime, klazz);
    }
  };

  public static final ObjectAllocator XML_ATTR_ALLOCATOR = new ObjectAllocator()
  {
    private XmlAttr xmlAttr = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlAttr == null) { xmlAttr = new XmlAttr(runtime, klazz); }
      try {
        XmlAttr clone = (XmlAttr) xmlAttr.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlAttr(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_CDATA_ALLOCATOR = new ObjectAllocator()
  {
    private XmlCdata xmlCdata = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlCdata == null) { xmlCdata = new XmlCdata(runtime, klazz); }
      try {
        XmlCdata clone = (XmlCdata) xmlCdata.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlCdata(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_COMMENT_ALLOCATOR = new ObjectAllocator()
  {
    private XmlComment xmlComment = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlComment == null) { xmlComment = new XmlComment(runtime, klazz); }
      try {
        XmlComment clone = (XmlComment) xmlComment.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlComment(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_DOCUMENT_ALLOCATOR = new ObjectAllocator()
  {
    private XmlDocument xmlDocument = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlDocument == null) { xmlDocument = new XmlDocument(runtime, klazz); }
      try {
        XmlDocument clone = (XmlDocument) xmlDocument.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlDocument(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_DOCUMENT_FRAGMENT_ALLOCATOR = new ObjectAllocator()
  {
    private XmlDocumentFragment xmlDocumentFragment = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlDocumentFragment == null) { xmlDocumentFragment = new XmlDocumentFragment(runtime, klazz); }
      try {
        XmlDocumentFragment clone = (XmlDocumentFragment)xmlDocumentFragment.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlDocumentFragment(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_DTD_ALLOCATOR = new ObjectAllocator()
  {
    private XmlDtd xmlDtd = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlDtd == null) { xmlDtd = new XmlDtd(runtime, klazz); }
      try {
        XmlDtd clone = (XmlDtd)xmlDtd.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlDtd(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_ELEMENT_ALLOCATOR = new ObjectAllocator()
  {
    private XmlElement xmlElement = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlElement == null) { xmlElement = new XmlElement(runtime, klazz); }
      try {
        XmlElement clone = (XmlElement)xmlElement.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlElement(runtime, klazz);
      }
    }
  };

  public static ObjectAllocator XML_ELEMENT_DECL_ALLOCATOR = new ObjectAllocator()
  {
    private XmlElementDecl xmlElementDecl = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlElementDecl == null) { xmlElementDecl = new XmlElementDecl(runtime, klazz); }
      try {
        XmlElementDecl clone = (XmlElementDecl)xmlElementDecl.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlElementDecl(runtime, klazz);
      }
    }
  };

  public static ObjectAllocator XML_ENTITY_REFERENCE_ALLOCATOR = new ObjectAllocator()
  {
    private XmlEntityReference xmlEntityRef = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlEntityRef == null) { xmlEntityRef = new XmlEntityReference(runtime, klazz); }
      try {
        XmlEntityReference clone = (XmlEntityReference)xmlEntityRef.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlEntityReference(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_NAMESPACE_ALLOCATOR = new ObjectAllocator()
  {
    private XmlNamespace xmlNamespace = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlNamespace == null) { xmlNamespace = new XmlNamespace(runtime, klazz); }
      try {
        XmlNamespace clone = (XmlNamespace) xmlNamespace.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlNamespace(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_NODE_ALLOCATOR = new ObjectAllocator()
  {
    private XmlNode xmlNode = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlNode == null) { xmlNode = new XmlNode(runtime, klazz); }
      try {
        XmlNode clone  = (XmlNode) xmlNode.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlNode(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_NODESET_ALLOCATOR = new ObjectAllocator()
  {
    private XmlNodeSet xmlNodeSet = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlNodeSet == null) { xmlNodeSet = new XmlNodeSet(runtime, klazz); }
      try {
        XmlNodeSet clone  = (XmlNodeSet) xmlNodeSet.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlNodeSet(runtime, klazz);
      }
    }
  };

  public static ObjectAllocator XML_PROCESSING_INSTRUCTION_ALLOCATOR = new ObjectAllocator()
  {
    private XmlProcessingInstruction xmlProcessingInstruction = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlProcessingInstruction == null) { xmlProcessingInstruction = new XmlProcessingInstruction(runtime, klazz); }
      try {
        XmlProcessingInstruction clone = (XmlProcessingInstruction)xmlProcessingInstruction.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlProcessingInstruction(runtime, klazz);
      }
    }
  };

  public static ObjectAllocator XML_READER_ALLOCATOR = new ObjectAllocator()
  {
    private XmlReader xmlReader = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlReader == null) { xmlReader = new XmlReader(runtime, klazz); }
      try {
        XmlReader clone  = (XmlReader) xmlReader.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        xmlReader = new XmlReader(runtime, klazz);
        return xmlReader;
      }
    }
  };

  private static ObjectAllocator XML_ATTRIBUTE_DECL_ALLOCATOR = new ObjectAllocator()
  {
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new XmlAttributeDecl(runtime, klazz);
    }
  };

  private static ObjectAllocator XML_ENTITY_DECL_ALLOCATOR = new ObjectAllocator()
  {
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new XmlEntityDecl(runtime, klazz);
    }
  };

  private static ObjectAllocator XML_ELEMENT_CONTENT_ALLOCATOR = new ObjectAllocator()
  {
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      throw runtime.newNotImplementedError("not implemented");
    }
  };

  public static final ObjectAllocator XML_RELAXNG_ALLOCATOR = new ObjectAllocator()
  {
    private XmlRelaxng xmlRelaxng = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlRelaxng == null) { xmlRelaxng = new XmlRelaxng(runtime, klazz); }
      try {
        XmlRelaxng clone  = (XmlRelaxng) xmlRelaxng.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlRelaxng(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_SAXPARSER_CONTEXT_ALLOCATOR = new ObjectAllocator()
  {
    private XmlSaxParserContext xmlSaxParserContext = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlSaxParserContext == null) { xmlSaxParserContext = new XmlSaxParserContext(runtime, klazz); }
      try {
        XmlSaxParserContext clone = (XmlSaxParserContext) xmlSaxParserContext.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlSaxParserContext(runtime, klazz);
      }
    }
  };

  private static final ObjectAllocator XML_SAXPUSHPARSER_ALLOCATOR = new ObjectAllocator()
  {
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new XmlSaxPushParser(runtime, klazz);
    }
  };

  private static final ObjectAllocator HTML_SAXPUSHPARSER_ALLOCATOR = new ObjectAllocator()
  {
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new Html4SaxPushParser(runtime, klazz);
    }
  };

  public static final ObjectAllocator XML_SCHEMA_ALLOCATOR = new ObjectAllocator()
  {
    private XmlSchema xmlSchema = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlSchema == null) { xmlSchema = new XmlSchema(runtime, klazz); }
      try {
        XmlSchema clone  = (XmlSchema) xmlSchema.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlSchema(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_SYNTAXERROR_ALLOCATOR = new ObjectAllocator()
  {
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new XmlSyntaxError(runtime, klazz);
    }
  };

  public static final ObjectAllocator XML_TEXT_ALLOCATOR = new ObjectAllocator()
  {
    private XmlText xmlText = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xmlText == null) { xmlText = new XmlText(runtime, klazz); }
      try {
        XmlText clone  = (XmlText) xmlText.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlText(runtime, klazz);
      }
    }
  };

  public static final ObjectAllocator XML_XPATHCONTEXT_ALLOCATOR = new ObjectAllocator()
  {
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new XmlXpathContext(runtime, klazz);
    }
  };

  public static ObjectAllocator XSLT_STYLESHEET_ALLOCATOR = new ObjectAllocator()
  {
    private XsltStylesheet xsltStylesheet = null;
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      if (xsltStylesheet == null) { xsltStylesheet = new XsltStylesheet(runtime, klazz); }
      try {
        XsltStylesheet clone  = (XsltStylesheet) xsltStylesheet.clone();
        clone.setMetaClass(klazz);
        return clone;
      } catch (CloneNotSupportedException e) {
        return new XmlText(runtime, klazz);
      }
    }
  };
}