use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class XMLDOMNodeList method nextNode.
/**
* Returns the next node in the collection.
* @return the next node in the collection
*/
@JsxFunction
public Object nextNode() {
final Object nextNode;
final List<DomNode> elements = getElements();
if (currentIndex_ >= 0 && currentIndex_ < elements.size()) {
nextNode = elements.get(currentIndex_).getScriptableObject();
} else {
nextNode = null;
}
currentIndex_++;
return nextNode;
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class XMLHTTPRequest method send.
/**
* Sends an HTTP request to the server and receives a response.
* @param body the body of the message being sent with the request.
*/
@JsxFunction
public void send(final Object body) {
if (webRequest_ == null) {
setState(STATE_DONE, Context.getCurrentContext());
return;
}
if (sent_) {
throw Context.reportRuntimeError("Unspecified error (request already sent).");
}
sent_ = true;
prepareRequest(body);
// quite strange but IE seems to fire state loading twice
setState(STATE_OPENED, Context.getCurrentContext());
final Window w = getWindow();
final WebClient client = w.getWebWindow().getWebClient();
final AjaxController ajaxController = client.getAjaxController();
final HtmlPage page = (HtmlPage) w.getWebWindow().getEnclosedPage();
final boolean synchron = ajaxController.processSynchron(page, webRequest_, async_);
if (synchron) {
doSend(Context.getCurrentContext());
} else {
// Create and start a thread in which to execute the request.
final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
final ContextAction<Object> action = cx -> {
// KEY_STARTING_SCOPE maintains a stack of scopes
@SuppressWarnings("unchecked") Deque<Scriptable> stack = (Deque<Scriptable>) cx.getThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE);
if (null == stack) {
stack = new ArrayDeque<>();
cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, stack);
}
stack.push(w);
try {
doSend(cx);
} finally {
stack.pop();
}
return null;
};
final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavascriptXMLHttpRequestJob(cf, action);
if (LOG.isDebugEnabled()) {
LOG.debug("Starting XMLHTTPRequest thread for asynchronous request");
}
jobID_ = w.getWebWindow().getJobManager().addJob(job, page);
}
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class XSLProcessor method transform.
/**
* Starts the transformation process or resumes a previously failed transformation.
*/
@JsxFunction
public void transform() {
final XMLDOMNode input = input_;
final SgmlPage page = input.getDomNodeOrDie().getPage();
if (output_ == null || !(output_ instanceof XMLDOMNode)) {
final DomDocumentFragment fragment = page.createDocumentFragment();
final XMLDOMDocumentFragment node = new XMLDOMDocumentFragment();
node.setParentScope(getParentScope());
node.setPrototype(getPrototype(node.getClass()));
node.setDomNode(fragment);
output_ = fragment.getScriptableObject();
}
transform(input_, ((XMLDOMNode) output_).getDomNodeOrDie());
final XMLSerializer serializer = new XMLSerializer(false);
final StringBuilder output = new StringBuilder();
for (final DomNode child : ((XMLDOMNode) output_).getDomNodeOrDie().getChildren()) {
if (child instanceof DomText) {
// See XMLDocumentTest.testLoadXML_XMLSpaceAttribute()
if (StringUtils.isNotBlank(((DomText) child).getData())) {
output.append(((DomText) child).getData());
}
} else {
// remove trailing "\r\n"
final String serializedString = serializer.serializeToString(child.getScriptableObject());
output.append(serializedString, 0, serializedString.length() - 2);
}
}
output_ = output.toString();
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class XMLDOMCharacterData method appendData.
/**
* Appends the supplied string to the existing string data.
* @param data the data that is to be appended to the existing string
*/
@JsxFunction
public void appendData(final String data) {
if (data == null || "null".equals(data)) {
throw Context.reportRuntimeError("Type mismatch.");
}
final DomCharacterData domCharacterData = getDomNodeOrDie();
domCharacterData.appendData(data);
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class XMLDOMDocument method createElement.
/**
* Creates an element node using the specified name.
* @param tagName the name for the new element node
* @return the new element object or <code>NOT_FOUND</code> if the tag is not supported
*/
@JsxFunction
public Object createElement(final String tagName) {
if (tagName == null || "null".equals(tagName)) {
throw Context.reportRuntimeError("Type mismatch.");
}
if (StringUtils.isBlank(tagName) || tagName.indexOf('<') >= 0 || tagName.indexOf('>') >= 0) {
throw Context.reportRuntimeError("To create a node of type ELEMENT a valid name must be given.");
}
Object result = NOT_FOUND;
try {
final DomElement domElement = (DomElement) getPage().createElement(tagName);
final Object jsElement = getScriptableFor(domElement);
if (jsElement == NOT_FOUND) {
if (LOG.isDebugEnabled()) {
LOG.debug("createElement(" + tagName + ") cannot return a result as there isn't a JavaScript object for the element " + domElement.getClass().getName());
}
} else {
result = jsElement;
}
} catch (final ElementNotFoundException e) {
// Just fall through - result is already set to NOT_FOUND
}
return result;
}
Aggregations