More Filtering Options
In last section we saw some options to filter elements we want. Beside those options, JCotrane has two more options: BeforeElement and InsideElement. Let's see both options in details, based on XML code that follows:
<?xml version="1.0" encoding="UTF-8"?>
<beanDescriptor>
<line>
<property mandatory="true" page="4" language="pt" />
<paragraphy>
<phrase />
</paragraphy>
<property2 mandatory="false" nacionality="pt" />
</line>
</beanDescriptor>
BeforeElement
Sometimes, to executing some parsing task, you want that some previos element be already started and it's not ended yet. So, let's supose you want to execute some task when starting a Element that the previous element is paragraphy . To acomplish this, we write the following class:
public class BeforeElementClass {
@BeforeElement(elementDeep=1,tag="paragraphy")
@StartElement
public void executeInStartElement(@CurrentBranch String currentBranch){
System.out.println(currentBranch);
System.out.println("Executing something in start element\n");
}
}
As you can see, we used the BeforeElement annotation to tell JColtrane what we want. When JColtrane is parsing some document, it pushes the element in a stack when the element starts and pop from stack in the element's end. Thus, the elementDeep with value 1 indicate the first Element before the current element, with value 2, the second element before and so on.
Besides the elementDeep, BeforeElement accepts the options tag, uri, localName and ContainAttribute. All of these option works just like we saw in EndElement and StartElement.
Running the code, system prints:
/beanDescriptor/line/paragraphy/phrase/
Executing something in start element
NOTE: you must to use BeforeElement in adition with a EndElement or StartElement to indicate when the method must execute. If you use only BeforeElement, system will not invoke the annotated method.
InsideElement
Let's say that you want to execute something all the time there is some element start before the current one, in any deep. To do this, you can use the InsideElement annotation. For example, let's say you want to execute some action when starting element to all elements inside the element with tag "line". So you write the class bellow:
public class InsideElementClass {
@InsideElement(tag="line")
@StartElement
public void executeInStartElement(@CurrentBranch String currentBranch){
System.out.println(currentBranch);
System.out.println("Executing something in start element\n");
}
}
Besides the option tag, InsideElement accepts uri, localName and ContainAttribute. All of these option works just like we saw in EndElement and StartElement.
Running the code, system prints:
/beanDescriptor/line/
Executing something in start element
/beanDescriptor/line/property/
Executing something in start element
/beanDescriptor/line/paragraphy/
Executing something in start element
/beanDescriptor/line/paragraphy/phrase/
Executing something in start element
/beanDescriptor/line/property2/
Executing something in start element
NOTE: just like in BeforeElement, you must to use InsideElement in adition with a EndElement or StartElement to indicate when the method must execute. If you use only InsideElement, system will not invoke the annotated method.
Now that you saw how InsideElement and BeforeElement works, let's see how you can build annotation with your own conditions in Making Your Own Filter Conditions.