String
instead of java.lang.String
, these classes must be imported.
The java.lang package is not imported by default, because its classes would conflict with Object, Boolean, Math, and other built-in JavaScript objects.
To import a Java class, use the JavaImporter object and the with
statement, which limits the scope of the imported Java packages and classes.
Java.type
API extension.
See this tutorial for details: http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/
There is a known issue with JavaImporter
that causes intermittent errors with patterns like with (imports) { } *
. The preferred approach is Java.type()
.
A pattern that works well is:
Java.type()
usage:
Variable | Type | Description |
---|---|---|
id | java.lang.String | The ID of the object to fetch. This is almost always the URI of the datasource to connect to and fetch content. |
lastModified | long | The time since the epoch from which the item was last touched. |
signature | java.lang.String | An optional string meant to be used to compare versions of the ID being fetched, e.g. an ETag in a web-crawl. |
content | crawler.common.MutableObject | A Content object that can be modified and returned, for fine grained control over the return. |
_fetcher | Fetcher | The current Fetcher instance (usually type JavascriptFetcher ), used to interact with the Fetcher, including getting a WebFetcher instance using _fetcher.getWebFetcher() . |
_context | java.util.Map | A map used to store data to persist across calls to fetch() , e.g. an instance of WebFetcher obtained using _fetcher.getWebFetcher() . |
Object | Description |
---|---|
String | A string object. This is converted to UTF-8 bytes and added as the raw content on a common.crawler.Content object and returned from the fetch() method. |
byte [] | A byte array. This array is set on a common.crawler.Content object and returned from the fetch() method. |
common.crawler.MutableContent | If you want to have complete control over the return from fetch() , make changes to the content object provided in the Context and return it. Do not create a new object. |
toString()
on the objects in the array. |
| A JavaScript Map | The map is converted to fields on the Content item returned. |
If the JavaScript script is implemented as a function, the return statement must return one of the above types. If the script is not function-based, the last line in the script must evaluate to one of these object types.