use of org.jruby.RubyString in project nokogiri by sparklemotion.
the class ParserContext method setInputSource.
/**
* Set the InputSource from <code>url</code> or <code>data</code>,
* which may be an IO object, a String, or a StringIO.
*/
public void setInputSource(ThreadContext context, IRubyObject data, IRubyObject url) {
source = new InputSource();
Ruby ruby = context.getRuntime();
ParserContext.setUrl(context, source, url);
// to the EncodingReaderInputStream
if (setEncoding(context, data))
return;
RubyString stringData = null;
if (invoke(context, data, "respond_to?", ruby.newSymbol("to_io")).isTrue()) {
RubyIO io = (RubyIO) TypeConverter.convertToType(data, ruby.getIO(), "to_io");
// use unclosedable input stream to fix #495
source.setByteStream(new UncloseableInputStream(io.getInStream()));
} else if (invoke(context, data, "respond_to?", ruby.newSymbol("read")).isTrue()) {
stringData = invoke(context, data, "read").convertToString();
} else if (invoke(context, data, "respond_to?", ruby.newSymbol("string")).isTrue()) {
stringData = invoke(context, data, "string").convertToString();
} else if (data instanceof RubyString) {
stringData = (RubyString) data;
} else {
throw ruby.newArgumentError("must be kind_of String or respond to :to_io, :read, or :string");
}
if (stringData != null) {
String encName = null;
if (stringData.encoding(context) != null) {
encName = stringData.encoding(context).toString();
}
Charset charset = null;
if (encName != null) {
try {
charset = Charset.forName(encName);
} catch (UnsupportedCharsetException e) {
// do nothing;
}
}
ByteList bytes = stringData.getByteList();
if (charset != null) {
StringReader reader = new StringReader(new String(bytes.unsafeBytes(), bytes.begin(), bytes.length(), charset));
source.setCharacterStream(reader);
source.setEncoding(charset.name());
} else {
stringDataSize = bytes.length() - bytes.begin();
ByteArrayInputStream stream = new ByteArrayInputStream(bytes.unsafeBytes(), bytes.begin(), bytes.length());
source.setByteStream(stream);
}
}
}
use of org.jruby.RubyString in project gocd by gocd.
the class XsltStylesheet method getTemplatesFromStreamSource.
private Templates getTemplatesFromStreamSource() throws TransformerConfigurationException {
if (stylesheet instanceof RubyString) {
StringReader reader = new StringReader((String) stylesheet.toJava(String.class));
StreamSource xsltStreamSource = new StreamSource(reader);
return factory.newTemplates(xsltStreamSource);
}
return null;
}
use of org.jruby.RubyString in project gocd by gocd.
the class NokogiriHelpers method nkf.
// This method is used from HTML documents. HTML meta tag with encoding specification
// might appear after non-ascii characters are used. For example, a title tag before
// a meta tag. In such a case, Xerces encodes characters in UTF-8 without seeing meta tag.
// Nokogiri uses NKF library to convert characters correct encoding. This means the method
// works only for JIS/Shift_JIS/EUC-JP.
public static String nkf(Ruby runtime, String ruby_encoding, String thing) {
StringBuffer sb = new StringBuffer("-");
Charset that = Charset.forName(ruby_encoding);
if (NokogiriHelpers.shift_jis.compareTo(that) == 0) {
sb.append("S");
} else if (NokogiriHelpers.jis.compareTo(that) == 0) {
sb.append("J");
} else if (NokogiriHelpers.euc_jp.compareTo(that) == 0) {
sb.append("E");
} else {
// should not come here. should be treated before this method.
sb.append("W");
}
sb.append("w");
Class nkfClass = null;
try {
// JRuby 1.7 and later
nkfClass = runtime.getClassLoader().loadClass("org.jruby.ext.nkf.RubyNKF");
} catch (ClassNotFoundException e1) {
try {
// Before JRuby 1.7
nkfClass = runtime.getClassLoader().loadClass("org.jruby.RubyNKF");
} catch (ClassNotFoundException e2) {
return thing;
}
}
Method nkf_method;
try {
nkf_method = nkfClass.getMethod("nkf", ThreadContext.class, IRubyObject.class, IRubyObject.class, IRubyObject.class);
RubyString r_str = (RubyString) nkf_method.invoke(null, runtime.getCurrentContext(), null, runtime.newString(new String(sb)), runtime.newString(thing));
return NokogiriHelpers.rubyStringToString(r_str);
} catch (SecurityException e) {
return thing;
} catch (NoSuchMethodException e) {
return thing;
} catch (IllegalArgumentException e) {
return thing;
} catch (IllegalAccessException e) {
return thing;
} catch (InvocationTargetException e) {
return thing;
}
}
use of org.jruby.RubyString in project gocd by gocd.
the class HtmlSaxPushParser method native_write.
@JRubyMethod
public IRubyObject native_write(ThreadContext context, IRubyObject chunk, IRubyObject isLast) {
try {
initialize_task(context);
} catch (IOException e) {
throw context.getRuntime().newRuntimeError(e.getMessage());
}
byte[] data = null;
if (chunk instanceof RubyString || chunk.respondsTo("to_str")) {
data = chunk.convertToString().getBytes();
} else {
terminateTask(context);
XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::HTML::SyntaxError"));
throw new RaiseException(xmlSyntaxError);
}
int errorCount0 = parserTask.getErrorCount();
;
if (isLast.isTrue()) {
IRubyObject document = invoke(context, this, "document");
invoke(context, document, "end_document");
terminateTask(context);
} else {
try {
Future<Void> task = stream.addChunk(new ByteArrayInputStream(data));
task.get();
} catch (ClosedStreamException ex) {
// this means the stream is closed, ignore this exception
} catch (Exception e) {
throw context.getRuntime().newRuntimeError(e.getMessage());
}
}
if (!options.recover && parserTask.getErrorCount() > errorCount0) {
terminateTask(context);
throw new RaiseException(parserTask.getLastError(), true);
}
return this;
}
use of org.jruby.RubyString in project gocd by gocd.
the class ParserContext method setInputSource.
/**
* Set the InputSource from <code>url</code> or <code>data</code>,
* which may be an IO object, a String, or a StringIO.
*/
public void setInputSource(ThreadContext context, IRubyObject data, IRubyObject url) {
source = new InputSource();
Ruby ruby = context.getRuntime();
ParserContext.setUrl(context, source, url);
// to the EncodingReaderInputStream
if (setEncoding(context, data))
return;
RubyString stringData = null;
if (invoke(context, data, "respond_to?", ruby.newSymbol("to_io").to_sym()).isTrue()) {
/* IO or other object that responds to :to_io */
RubyIO io = (RubyIO) TypeConverter.convertToType(data, ruby.getIO(), "to_io");
// use unclosedable input stream to fix #495
source.setByteStream(new UncloseableInputStream(io.getInStream()));
} else {
if (invoke(context, data, "respond_to?", ruby.newSymbol("string").to_sym()).isTrue()) {
/* StringIO or other object that responds to :string */
stringData = invoke(context, data, "string").convertToString();
} else if (data instanceof RubyString) {
stringData = (RubyString) data;
} else {
throw ruby.newArgumentError("must be kind_of String or respond to :to_io or :string");
}
}
if (stringData != null) {
String encName = null;
if (stringData.encoding(context) != null) {
encName = stringData.encoding(context).toString();
}
Charset charset = null;
if (encName != null) {
try {
charset = Charset.forName(encName);
} catch (UnsupportedCharsetException e) {
// do nothing;
}
}
ByteList bytes = stringData.getByteList();
if (charset != null) {
StringReader reader = new StringReader(new String(bytes.unsafeBytes(), bytes.begin(), bytes.length(), charset));
source.setCharacterStream(reader);
source.setEncoding(charset.name());
} else {
stringDataSize = bytes.length() - bytes.begin();
ByteArrayInputStream stream = new ByteArrayInputStream(bytes.unsafeBytes(), bytes.begin(), bytes.length());
source.setByteStream(stream);
}
}
}
Aggregations