File: ParserContext.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 (260 lines) | stat: -rw-r--r-- 7,442 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
package nokogiri.internals;

import static nokogiri.internals.NokogiriHelpers.rubyStringToString;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.concurrent.Callable;

import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyObject;
import org.jruby.RubyString;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import org.jruby.util.IOInputStream;
import org.xml.sax.InputSource;

/**
 * Base class for the various parser contexts.  Handles converting
 * Ruby objects to InputSource objects.
 *
 * @author Patrick Mahoney <pat@polycrystal.org>
 * @author Yoko Harada <yokolet@gmail.com>
 */
public abstract class ParserContext extends RubyObject
{
  protected InputSource source = null;
  protected IRubyObject detected_encoding = null;
  protected int stringDataSize = -1;
  protected String java_encoding;

  public
  ParserContext(Ruby runtime)
  {
    // default to class 'Object' because this class isn't exposed to Ruby
    super(runtime, runtime.getObject());
  }

  public
  ParserContext(Ruby runtime, RubyClass klass)
  {
    super(runtime, klass);
  }

  protected InputSource
  getInputSource()
  {
    return source;
  }

  public void
  setIOInputSource(ThreadContext context, IRubyObject data, IRubyObject url)
  {
    source = new InputSource();
    ParserContext.setUrl(context, source, url);

    Ruby ruby = context.getRuntime();

    if (!(data.respondsTo("read"))) {
      throw ruby.newTypeError("must respond to :read");
    }

    source.setByteStream(new IOInputStream(data));
    if (java_encoding != null) {
      source.setEncoding(java_encoding);
    }
  }

  public void
  setStringInputSource(ThreadContext context, IRubyObject data, IRubyObject url)
  {
    source = new InputSource();
    ParserContext.setUrl(context, source, url);

    Ruby ruby = context.getRuntime();

    if (!(data instanceof RubyString)) {
      throw ruby.newTypeError("must be kind_of String");
    }

    RubyString stringData = (RubyString) data;

    if (stringData.encoding(context) != null) {
      RubyString stringEncoding = stringData.encoding(context).asString();
      String encName = NokogiriHelpers.getValidEncodingOrNull(stringEncoding);
      if (encName != null) {
        java_encoding = encName;
      }
    }

    ByteList bytes = stringData.getByteList();

    stringDataSize = bytes.length() - bytes.begin();
    ByteArrayInputStream stream = new ByteArrayInputStream(bytes.unsafeBytes(), bytes.begin(), bytes.length());
    source.setByteStream(stream);
    source.setEncoding(java_encoding);
  }

  public static void
  setUrl(ThreadContext context, InputSource source, IRubyObject url)
  {
    String path = rubyStringToString(url);
    // Dir.chdir might be called at some point before this.
    if (path != null) {
      try {
        URI uri = URI.create(path);
        source.setSystemId(uri.toURL().toString());
      } catch (Exception ex) {
        // fallback to the old behavior
        File file = new File(path);
        if (file.isAbsolute()) {
          source.setSystemId(path);
        } else {
          String pwd = context.getRuntime().getCurrentDirectory();
          String absolutePath;
          try {
            absolutePath = new File(pwd, path).getCanonicalPath();
          } catch (IOException e) {
            absolutePath = new File(pwd, path).getAbsolutePath();
          }
          source.setSystemId(absolutePath);
        }
      }
    }
  }

  protected void
  setEncoding(String encoding)
  {
    source.setEncoding(encoding);
  }

  /**
   * Set the InputSource to read from <code>file</code>, a String filename.
   */
  public void
  setInputSourceFile(ThreadContext context, IRubyObject file)
  {
    source = new InputSource();
    ParserContext.setUrl(context, source, file);
  }

  /**
   * Set the InputSource from <code>stream</code>.
   */
  public void
  setInputSource(InputStream stream)
  {
    source = new InputSource(stream);
  }

  /**
   * Wrap Nokogiri parser options in a utility class.  This is
   * read-only.
   */
  public static class Options
  {
    protected static final long STRICT = 0;
    protected static final long RECOVER = 1;
    protected static final long NOENT = 2;
    protected static final long DTDLOAD = 4;
    protected static final long DTDATTR = 8;
    protected static final long DTDVALID = 16;
    protected static final long NOERROR = 32;
    protected static final long NOWARNING = 64;
    protected static final long PEDANTIC = 128;
    protected static final long NOBLANKS = 256;
    protected static final long SAX1 = 512;
    protected static final long XINCLUDE = 1024;
    protected static final long NONET = 2048;
    protected static final long NODICT = 4096;
    protected static final long NSCLEAN = 8192;
    protected static final long NOCDATA = 16384;
    protected static final long NOXINCNODE = 32768;

    public final boolean strict;
    public final boolean recover;
    public final boolean noEnt;
    public final boolean dtdLoad;
    public final boolean dtdAttr;
    public final boolean dtdValid;
    public final boolean noError;
    public final boolean noWarning;
    public final boolean pedantic;
    public final boolean noBlanks;
    public final boolean sax1;
    public final boolean xInclude;
    public final boolean noNet;
    public final boolean noDict;
    public final boolean nsClean;
    public final boolean noCdata;
    public final boolean noXIncNode;

    protected static boolean
    test(long options, long mask)
    {
      return ((options & mask) == mask);
    }

    public
    Options(long options)
    {
      strict = ((options & RECOVER) == STRICT);
      recover = test(options, RECOVER);
      noEnt = test(options, NOENT);
      dtdLoad = test(options, DTDLOAD);
      dtdAttr = test(options, DTDATTR);
      dtdValid = test(options, DTDVALID);
      noError = test(options, NOERROR);
      noWarning = test(options, NOWARNING);
      pedantic = test(options, PEDANTIC);
      noBlanks = test(options, NOBLANKS);
      sax1 = test(options, SAX1);
      xInclude = test(options, XINCLUDE);
      noNet = test(options, NONET);
      noDict = test(options, NODICT);
      nsClean = test(options, NSCLEAN);
      noCdata = test(options, NOCDATA);
      noXIncNode = test(options, NOXINCNODE);
    }
  }

  /*
  public static class NokogiriXInlcudeEntityResolver implements org.xml.sax.EntityResolver {
      InputSource source;
      public NokogiriXInlcudeEntityResolver(InputSource source) {
          this.source = source;
      }

      @Override
      public InputSource resolveEntity(String publicId, String systemId)
              throws SAXException, IOException {
          if (systemId != null) source.setSystemId(systemId);
          if (publicId != null) source.setPublicId(publicId);
          return source;
      }
  } */

  public static abstract class ParserTask<T extends ParserContext> implements Callable<T>
  {

    protected final ThreadContext context; // TODO does not seem like a good idea!?
    protected final IRubyObject handler;
    protected final T parser;

    protected
    ParserTask(ThreadContext context, IRubyObject handler, T parser)
    {
      this.context = context;
      this.handler = handler;
      this.parser = parser;
    }

  }

}