How to get value from HSSFComments in apache POI - java

I want to read/extract the value from HSSFComment.
I can access the HSSFComment by the following code:
HSSFComment comment = workSheet.getCellComment(1, 0);
But, how can I get the text/value from that "comment" instance?
there are tow methods in HSSFComment:
getTextObjectRecord()
getNoteRecord()
But both are protected methods...that's why I can't access those from my class. in other word, these methods are not visible from my class. Following line of code doesn't compile.
TextObjectRecord txo = comment.getTextObjectRecord();
Any comments?

Use getString() inherited from HSSFTextBox. This returns an HSSFRichTextString, which itself has a getString() method to get the plain text. In otherwords
String comment = cell.getComment().getString().getString();
Which you can't do like that due to the possibility of null returns, but that's the idea.

Related

Unable to get a variable value in drools and use it to set in another field

drl file :
rule "pendingMsgSizeModified"
when
$tibcoEMSObj : TibcoEMSData(deliveredMsgCnt.contains("MB") && pendingMsgSize \> 100)
eval (!($tibcoEMSObj.typeOfAlert != null))
then
$tibcoEMSObj.setSendEmail(true);
$tibcoEMSObj.setTypeOfAlert($tibcoEMSobj.getTypeOfAlert()+"pendingMsgSize###");
update($tibcoEMSObj)
end
It gives error at this location : $tibcoEMSobj.getTypeOfAlert()
Error : \[Error: unable to resolve method using strict-mode: org.drools.core.spi.KnowledgeHelper.$tibcoEMSobj()\]
\[Near : {... SObj.setTypeOfAlert($tibcoEMSobj.getTypeOfAlert()+ ....}\]
I am expecting to use the getter method to get it and concatenate the string to set in another variable and save/update it
Your syntax is all over the place. And without actually seeing your model, it's hard to tell what you're even trying to do.
But based on what you said:
I am expecting to use the getter method to get it and concatenate the string to set in another variable and save/update it
And the code you did provide, I think this is what you're trying to do:
rule "pendingMsgSizeModified"
when
$tibcoEMSObj : TibcoEMSData(
deliveredMsgCnt.contains("MB"),
pendingMsgSize > 100,
$alertType: typeOfAlert != null
)
then
String newAlert = $alertType + "pendingMsgSize###";
modify($tibcoEMSObj) {
setSendEmail(true)
setTypeOfAlert(newAlert)
}
end
I presumed that your eval was either typo'd or just plain wrong since it was checking that the value was actually null. Don't use eval unless you have absolutely no other choice. (And I've never actually run into a situation where there was no other choice.)
I also presumed that the alert type will not change. I don't know what your sendEmail method does, or if it has side effects; if it does have side effects that change the typeOfAlert, then you'll of course need to call getTypeOfAlert() after calling setSendEmail.

Java8 : Can't get value of an Optional.of(String) return type

In the context of using the OWLAPI 4.0 API, this following line of code:
ontologyIRI = IRI.create(o.getOntologyID().getOntologyIRI().toString());
returns the following string :
"Optional.of(http://www.indytion.com/music/composition)".
What I need is the sole string "http://www.indytion.com/music/composition".
I tried to declare ontologyIRI as Optional and use .get() method, .orElse(), etc. to no avail. I still have the returned string that includes the 'optional.of()' part.
My question is : How could I get the internal string?
Thank you very much for your help.
Edit : The full code the method
private void LoadOntology(String ontologyPath)
{
OWLOntologyManager man = OWLManager.createOWLOntologyManager();
OWLOntology o;
File ontologyFile = new File(ontologyPath);
Optional<IRI> ontologyIRI;
try {
o = man.loadOntologyFromOntologyDocument(ontologyFile);
ontologyIRI = Optional.of(IRI.create(String.valueOf(o.getOntologyID().getOntologyIRI()).toString()));
System.out.println("Ontology IRI is: " + ontologyIRI.get());
} catch (OWLOntologyCreationException e) {
e.printStackTrace();
}
}
The System.out.println() returns exactly this string:
"Ontology IRI = Optional.of(http://www.indytion.com/music/composition)"
Use .get() instead of toString()
//Returns 'Optional[example]'
Optional.of("example").toString();
//Returns 'example'
Optional.of("example").get();
Short answer: Replace
Optional.of(IRI.create(String.valueOf(o.getOntologyID().getOntologyIRI()).toString()));
with
o.getOntologyID().getOntologyIRI().get();
Longer answer: you're doing an awful lot of back-and forth that's pointless at best and actively harmful in some cases:
In no particular order:
others have already commented that IRI instances are immutable, so creating a new one from an existing one is kind of pointless (if harmless).
calling Optional.of() if you don't intend to actually return an Optional is almost always a bad idea.
String.valueOf() is used to get a string-representation of some value and is usually most useful for debugging, but should not be relied on to fully round-trip everything about an object (the same applies to toString().
So basically what you're left with is this:
o.getOntologyID().getOntologyIRI() gives you an Optional<IRI>
you want an IRI.
Optional::get returns the value contained in the optional, if one exists, so you simply need to call get()
If, however the Optional is empty (i.e. there is no underlying value) then get() will throw a NoSuchElementException. This might or might not be what you want. To work around this either call isPresent() before calling get() to check if a value exists or use any of the multitude of other accessor methods, some of which have "built-in checks" in a way.
Finally, it seems that the problem was not the code itself. This is how the problem has been solved. But I don't understand why it has been solved :
I copy/paste (in the same file) the "shouldAddObjectPropertyAssertions()" example from OWLAPI4 examples -> This example code runs OK (but does not use the getOntologyID() method as I do).
Change SDKs to another minor version '1.8.0_61'
Change again with initial and desired SDK '1.8.0_131'
Invalidate caches and restart the IDE
Problem solved. The exactly same code :
ontologyIRI = o.getOntologyID().getOntologyIRI().get();
System.out.println("Ontology IRI is: " + ontologyIRI);
Now returns the expected string value : "http://www.indytion.com/music/composition" and not "Optional.of(http://www.indytion.com/music/composition)" anymore.
If someone can explain why it has been fixed, I would be very glad.
Thank you again for your help.

How to replace a query string in an Apache Velocity template?

In my web application I'm trying to prevent users from inserting JavaScript in the freeText parameter when they're running a search.
To do this, I've written code in the header Velocity file to check whether the query string contains a parameter called freeText, and if so, use the replace method to replace the characters within the parameter value. However, when you load the page, it still displays the original query string - I'm unsure on how to replace the original query string with my new one which has the replaced characters.
This is my code:
#set($freeTextParameter = "$request.getParameter('freeText')")
freeTextParameter: $freeTextParameter
#if($freeTextParameter)
##Do the replacement:
#set($replacedQueryString = "$freeTextParameter.replace('confirm','replaced')")
replacedQueryString after doing the replace: $replacedQueryString
The query string now: $request.getQueryString()
The freeText parameter now: $request.getParameter('freeText')
#end
In the code above, the replacedQueryString variable has changed as expected (ie the replacement has been carried out as expected), but the $request.getQueryString() and $request.getParameter('freeText') are still the same as before, as if the replacement had never happened.
Seeing as there is a request.getParameter method which works fine for getting the parameters, I assumed there would be a request.setParameter method to do the same thing in reverse, but there isn't.
The Java String is an immutable object, which means that the replace() method will return an altered string, without changing the original one.
Since the parameters map given by the HttpServletRequest object cannot be modified, this approach doesn't work well if your templates rely on $request.getParameter('freeText').
Instead, if you rely on VelocityTools, then you can rather rely on $params.freeText in your templates. Then, you can tune your WEB-INF/tools.xml file to make this parameters map alterable:
<?xml version="1.0">
<tools>
<toolbox scope="request">
<tool key="params" readOnly="false"/>
...
</toolbox>
...
</tools>
(Version 2.0+ of the tools is required).
Then, in your header, you can do:
#set($params.freeText = params.freeText.replace('confirm','replaced'))
I managed to fix the issue myself - it turned out that there was another file (which gets called on every page) in which the $!request.getParameter('freeText')" variable is used. I have updated that file so that it uses the new $!replacedQueryString variable (ie the one with the JavaScript stripped out) instead of the existing "$!request.getParameter('freeText')" variable. This now prevents the JavaScript from being executed on every page.
So, this is the final working code in the header Velocity file:
#set($freeTextParameter = "$!m.request.httpRequest.getParameter('freeText')")
#if($freeTextParameter)
#set($replacedQueryString = "$freeTextParameter.replace('confirm','').replace('<','').replace('>','').replace('(','').replace(')','').replace(';','').replace('/','').replace('\"','').replace('&','').replace('+','').replace('script','').replace('prompt','').replace('*','').replace('.','')")
#end

Pass String with Xpand method call as parameter

In XPAND template, I have a call where, the call should be done for the method which takes the String and the Object.
i.e. ,
«setData("String",Object)-»
For the above method, the String value I want to pass is the simplified value of below string:
where ,«getAddress(object)» , return dynamic value.
"/begin DATA XETK DEFAULT_RASTERS «getAddress(object)» /end DATA"
If I try to do this in the below way, i get compilation error.
«setData("/begin DATA XETK DEFAULT_RASTERS «getAddress(object)» /end DATA",Object)-»
Can anyone help me, how can I pass this string to the method?
Thanks ,
Regards,
Shwetha
Ok , I got the answer for the above question.
In XPAND , the expressions are not allowed to pass as parameter. But this is done in XTEND 2

How to get simply email address after MimeUtility.decodeText()?

How can I simply get the email address after use MimeUtility.decodeText() from javax.mail.internet ?
After that operation I have following String:
Foo Bar <foo.bar#abc.en>
I can do that by methods from String class but I'm interested in existed predefinied method for this ?
It's very easy to define your method. To get the String you only need one line of code:
String email = stringToDecode.split("<")[1].substring(0, stringToDecode.split("<")[1].length() - 1);
Just call new InternetAddress(addr).getAddress(). In fact, you don't even need to call MimeUtility.decodeText() first.

Categories

Resources