I am dealing with the text extraction from pdf. To this end I wrote my own text extraction strategy. I have one dynamic class and within this class i invoke text extraction strategy. However, when i introduce some parameters to my dynamic class i cannot use them within strategy class. To be clear i am adding my code template below.
My question is briefly, is it possible to invoke parameter unq showing up in "get_intro" class, from renderText? Or other way around, can a variable or parameter created inside the "renderText" class be invoked in the "get_intro"?
public class trial {
public trial(){}
public Boolean get_intro(String pdf, String unq){
try { ....
for (int j = 1; j <= 3; j++) {
out.println(PdfTextExtractor.getTextFromPage(reader, j, semTextExtractionStrategy));
}
...} catch (Exception e) {
e.printStackTrace();
}
semTextExtractionStrategy part:
public class SemTextExtractionStrategy implements TextExtractionStrategy {
@Override
public void beginTextBlock() {
}
@Override
public void renderText(TextRenderInfo renderInfo) {
text = renderInfo.getText();...}
@Override
public void endTextBlock() {
}
@Override
public void renderImage(ImageRenderInfo renderInfo) {
}
@Override
public String getResultantText() {
//return text;
return main;
}
}
One could consider the following problematic solution:
And in get_intro:
The problem is that you want to pass some context class on calling the entry function (like ActionEvent or such). But by its name a strategy class probably is a stateless singleton. In the above solution you would need to instantiate from a
Class<TextExctractionStrategy>, Class<D>
a new strategy instance. Or like in the MouseAdapter class pass the same event class parameter to every method.This smells of "over-designing" or a skewed pattern application.
As we are on the brink of Java 8 lambdas, you might even consider a "backport" of a design with lambdas.
But for the moment I would go with adding a generic
D textExtractionContext
to every called function, if the API is not for an external library.