[ad_1]
Over on my Characteristic Flags E book web site, I am beginning to transfer one of the most content material in the back of a pay-wall; and, to try this, I am the use of jSoup to interchange a couple of content material paragraphs with a unmarried acquire realize paragraph inside of designated chapters. On the other hand, in my first technique to this set of rules, I used to be getting the next jSoup error:
Index 1 out of bounds for period 0
The mistake is not extraordinarily useful; however, I imagine what is going down this is that once I take away a component from the jSoup DOM (File Object Style) the use of an .empty()
name, jSoup is now not breaking the parent-child dating to the got rid of parts. Which is then inflicting a topic once I move to re-append the got rid of parts again into the identical dad or mum.
I will be able to reproduce this mistake with a easy jSoup demo the use of this HTML record:
<frame>
<p>jSoup + ColdFusion = Noice!</p>
</frame>
To breed the mistake with ColdFusion (Lucee CFML), I will .empty()
the frame
after which re-append the only p
detail:
<cfscript>
frame = javaNew( "org.jsoup.Jsoup" )
.parseBodyFragment( fileRead( "./content material.htm" ) )
.frame()
;
paragraph = frame.firstElementChild();
// Take away all of the youngsters from the BODY after which attempt to re-add the paragraph.
frame
.empty()
.appendChild( paragraph )
;
// Output resultant HTML to the web page.
echo( frame.outerHtml() );
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
/**
* I create a brand new Java magnificence wrapper the use of the jSoup JAR information.
*/
public any serve as javaNew( required string className ) {
var jarPaths = [
expandPath( "./jsoup-1.16.1.jar" )
];
go back( createObject( "java", className, jarPaths ) );
}
</cfscript>
And, after we run this ColdFusion code, we get the next error:
Index 1 out of bounds for period 0
For any individual Googling to get right here, that is the stacktrace that I am getting:
lucee.runtime.exp.NativeException: Index 1 out of bounds for period 0
at java.base/jdk.interior.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.interior.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.interior.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Items.checkIndex(Items.java:372)
at java.base/java.util.ArrayList.take away(ArrayList.java:536)
at org.jsoup.helper.ChangeNotifyingArrayList.take away(ChangeNotifyingArrayList.java:37)
at org.jsoup.nodes.Node.removeChild(Node.java:504)
at org.jsoup.nodes.Node.setParentNode(Node.java:482)
at org.jsoup.nodes.Node.reparentChild(Node.java:563)
at org.jsoup.nodes.Component.appendChild(Component.java:577)
To mend this mistake, we want to name .take away()
at the p
detail prior to we attempt to re-append it to the frame
:
<cfscript>
frame = javaNew( "org.jsoup.Jsoup" )
.parseBodyFragment( fileRead( "./content material.htm" ) )
.frame()
;
paragraph = frame.firstElementChild();
// To be able to re-append the paragraph again into the record, we need to first BREAK
// THE PARENT RELATIONSHIP to the frame. We will be able to do this via calling eliminating() at the
// paragraph itself.
paragraph.take away();
// Take away all of the youngsters from the BODY after which attempt to re-add the paragraph.
frame
.empty() // Take away any ultimate non-element nodes (ex, feedback).
.appendChild( paragraph )
;
// Output resultant HTML to the web page.
echo( frame.outerHtml() );
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
/**
* I create a brand new Java magnificence wrapper the use of the jSoup JAR information.
*/
public any serve as javaNew( required string className ) {
var jarPaths = [
expandPath( "./jsoup-1.16.1.jar" )
];
go back( createObject( "java", className, jarPaths ) );
}
</cfscript>
The one distinction on this model of the code is that I am calling paragraph.take away()
prior to including the node again into the DOM. No matter that is doing in the back of the scenes, it’s correctly breaking the parent-child dating in some way that calling .empty()
does now not.
ASIDE: Some jSoup strategies, like
.youngsters()
, go back an Array ofComponent
nodes known asParts
. This array has its personal.take away()
way that can name.take away()
on all the nodes within the assortment.
I have no idea sufficient about jSoup — or the goal of those strategies — with a view to name this a “trojan horse”; however, I will be able to say that it kind of feels sudden to me. Actually, I’d be expecting an .empty()
solution to be little greater than a short-hand implementation for looping over all of the child-nodes and calling .take away()
on them in flip.
Need to use code from this put up?
Take a look at the license.
[ad_2]