Olá
Pessoal
encontrei essa classe java usando SWT e ela faz o seguinte imagine um doc geraldo por um worddocument entao eu quero fazer uma pesquisa nesse word documento e replace e depois salvar o documento, é exatamente isso que ele ta fazendo, abre um doc faz search e replace e depois salva o doc, minha pergunta é posso usar essa classe com J2EE ?
Será que não terei problemas com ela?
Fiz o teste e aparentemente não deu problema, mais gostaria de saber a opinião de vc.
Grato
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.internal.ole.win32.TYPEATTR;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.OleFunctionDescription;
import org.eclipse.swt.ole.win32.OlePropertyDescription;
import org.eclipse.swt.ole.win32.OleParameterDescription;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.ole.win32.Variant;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
public class WordSearchReplace {
private static final String PROG_ID = "Word.Application";
private static final int WD_REPLACE_ALL = 2;
private static final int WD_FIND_CONTINUE = 1;
private Shell shell = null;
private OleFrame frame = null;
private OleClientSite wordSite = null;
private OleAutomation wordAutomation = null;
private OleAutomation activeDocumentAutomation = null;
private boolean cleaned = false;
/**
* Create a new instance of the WordSearchReplace class.
*/
public WordSearchReplace() {
this.shell = new Shell();
this.frame = new OleFrame(this.shell, SWT.NONE);
this.wordSite = new OleClientSite(this.frame, SWT.NONE, WordSearchReplace.PROG_ID);
this.wordAutomation = new OleAutomation(this.wordSite);
}
public void openFile(String fileName) throws SWTException,
NullPointerException,
FileNotFoundException,
IllegalArgumentException {
OleAutomation documentsAutomation = null;
int[] id = null;
Variant[] arguments = null;
Variant invokeResult = null;
try {
if(fileName == null) {
throw new NullPointerException("Null value passed to " +
"fileName parameters of the openFile() method.");
}
if(!(fileName.endsWith(".doc")) && !(fileName.endsWith(".dot"))) {
throw new IllegalArgumentException(
"The filename must end with the extensions \'.doc\' or \'.dot\'");
}
File fileToPrint = new File(fileName);
if(!(fileToPrint.exists())) {
throw new FileNotFoundException("The file " +
fileName +
"cannot be found.");
}
documentsAutomation = this.getChildAutomation(this.wordAutomation,
"Documents");
id = documentsAutomation.getIDsOfNames(new String[]{"Open"});
if(id == null) {
throw new SWTException("It was not possible to recover an " +
"identifer for the Open method in WordSearchReplace.openFile().");
}
arguments = new Variant[1];
arguments[0] = new Variant(fileName);
invokeResult = documentsAutomation.invoke(id[0], arguments);
if(invokeResult == null) {
throw new SWTException("An error occurred whilst invoking the " +
"Open method for the following file: " +
fileName +
" in WordSearchReplace.openFile().");
}
else {
this.activeDocumentAutomation = this.getChildAutomation(
this.wordAutomation, "ActiveDocument");
}
}
finally {
if(documentsAutomation != null) {
documentsAutomation.dispose();
}
}
}
public void save() throws SWTException {
int[] id = null;
Variant invokeResult = null;
id = this.activeDocumentAutomation.getIDsOfNames(new String[]{"Save"});
if(id == null) {
throw new SWTException("Unable to obtain an automation for " +
"the Save method in WordSearchReplace.save().");
}
invokeResult = this.activeDocumentAutomation.invoke(id[0]);
if(invokeResult == null) {
throw new SWTException("A problem occurred invoking the " +
"Save method in WordSearchReplace.save().");
}
}
public void saveAs(String fileName) throws SWTException,
NullPointerException,
IllegalArgumentException {
int[] id = null;
Variant[] arguments = null;
Variant invokeResult = null;
if(fileName == null) {
throw new NullPointerException("A null value was passed to " +
"the fileName parameter of WordSearchReplace.saveAs().");
}
if(fileName.length() == 0) {
throw new NullPointerException("An empty string was passed " +
"to the fileName parameter of WordSearchReplace.saveAs().");
}
if((!fileName.endsWith(".dot")) && (!fileName.endsWith(".doc"))) {
throw new IllegalArgumentException("An illegal file name was " +
"passed to the fileName parameter of " +
"WordSearchReplace.saveAs(). The file name must " +
"end in \'.dot\' or \'.doc\'.");
}
id = this.activeDocumentAutomation.getIDsOfNames(new String[]{"SaveAs"});
if(id == null) {
throw new SWTException("Unable to obtain an automation for " +
"the SaveAs method in WordSearchReplace.saveAs().");
}
arguments = new Variant[1];
arguments[0] = new Variant(fileName);
invokeResult = this.activeDocumentAutomation.invoke(id[0], arguments);
if(invokeResult == null) {
throw new SWTException("A problem occurred invoking the " +
"SaveAs method in WordSearchReplace.saveAs().");
}
}
public void replace(String searchTerm,
String replacementTerm) throws SWTException,
NullPointerException {
OleAutomation selectionFindAutomation = null;
OleAutomation childAutomation = null;
Variant[] arguments = null;
Variant invokeResult = null;
int[] id = null;
int[] namedArguments = null;
boolean success = true;
if(searchTerm == null) {
throw new NullPointerException("Null value passed to " +
"searchTerm parameter of the replace() method.");
}
if(replacementTerm == null) {
throw new NullPointerException("Null value passed to " +
"replacementTerm parameter of the replace() method.");
}
childAutomation = this.getChildAutomation(this.wordAutomation, "Selection");
selectionFindAutomation = this.getChildAutomation(childAutomation, "Find");
id = selectionFindAutomation.getIDsOfNames(new String[]{"ClearFormatting"});
if(id == null) {
throw new SWTException("It is not possible to recover an identifier " +
"for the ClearFormatting method in WordSearchReplace.replace() " +
"when clearing the formatting for the search string.");
}
invokeResult = selectionFindAutomation.invoke(id[0]);
if(invokeResult == null) {
throw new SWTException("A problem occurred invoking the " +
"ClearFormatting method in WordSearchReplace.repace() " +
"when clearing formatting for the search string.");
}
childAutomation = this.getChildAutomation(selectionFindAutomation,
"Replacement");
id = childAutomation.getIDsOfNames(new String[]{"ClearFormatting"});
if(id == null) {
throw new SWTException("It is not possible to recover an identifier " +
"for the ClearFormatting method in WordSearchReplace.replace() " +
"when clearing the formatting for the replacement string.");
}
invokeResult = childAutomation.invoke(id[0]);
if(invokeResult == null) {
throw new SWTException("A problem occurred invoking the " +
"ClearFormatting method in WordSearchReplace.repace() " +
"when clearing formatting for the replacement string.");
}
arguments = new Variant[1];
arguments[0] = new Variant(searchTerm);
success = this.setPropertyValue(selectionFindAutomation, "Text", arguments);
if(!success) {
throw new SWTException("A problem occurred setting the Text " +
"property for the search string in WordSearchReplace.replace().");
}
childAutomation = this.getChildAutomation(selectionFindAutomation,
"Replacement");
arguments[0] = new Variant(replacementTerm);
success = this.setPropertyValue(childAutomation, "Text", arguments);
if(!success) {
throw new SWTException("A problem occurred setting the Text property" +
" for the replacement string in WordSearchReplace.replace().");
}
arguments[0] = new Variant(true);
success = this.setPropertyValue(selectionFindAutomation, "Forward", arguments);
if(!success) {
throw new SWTException("A problem occurred setting the Forward " +
"property in WordSearchReplace.replace().");
}
arguments[0] = new Variant(WordSearchReplace.WD_FIND_CONTINUE);
success = this.setPropertyValue(selectionFindAutomation, "Wrap", arguments);
if(!success) {
throw new SWTException("A problem occurred setting the Wrap " +
"property in WordSearchReplace.replace().");
}
arguments[0] = new Variant(false);
success = this.setPropertyValue(selectionFindAutomation, "Format", arguments);
if(!success) {
throw new SWTException("A problem occurred setting the Format " +
"property in WordSearchReplace.replace().");
}
arguments[0] = new Variant(false);
success = this.setPropertyValue(selectionFindAutomation, "MatchCase", arguments);
if(!success) {
throw new SWTException("A problem occurred setting the MatchCase " +
"property in WordSearchReplace.replace().");
}
arguments[0] = new Variant(false);
success = this.setPropertyValue(selectionFindAutomation, "MatchWholeWord", arguments);
if(!success) {
throw new SWTException("A problem occurred setting the " +
"MatchWholeWord property in WordSearchReplace.replace().");
}
arguments[0] = new Variant(false);
success = this.setPropertyValue(selectionFindAutomation, "MatchWildCards", arguments);
if(!success) {
throw new SWTException("A problem occurred setting the " +
"MatchWildCards property in WordSearchReplace.replace().");
}
arguments[0] = new Variant(false);
success = this.setPropertyValue(selectionFindAutomation, "MatchSoundsLike", arguments);
if(!success) {
throw new SWTException("A problem occurred setting the " +
"MatchSoundsLike property in WordSearchReplace.replace().");
}
arguments[0] = new Variant(false);
success = this.setPropertyValue(selectionFindAutomation, "MatchAllWordForms", arguments);
if(!success) {
throw new SWTException("A problem occurred setting the " +
"MatchAllWordForms property in WordSearchReplace.replace().");
}
id = selectionFindAutomation.getIDsOfNames(new String[]{"Execute", "Replace"});
if(id == null) {
throw new SWTException("It was not possible to recover an identifier " +
"for the Execute method in WordSearchReplace.replace().");
}
arguments = new Variant[1];
arguments[0] = new Variant(WordSearchReplace.WD_REPLACE_ALL);
namedArguments = new int[1];
namedArguments[0] = id[1];
invokeResult = selectionFindAutomation.invoke(id[0], arguments, namedArguments);
if(invokeResult == null) {
throw new SWTException("A problem occurred trying to invoke the " +
"Execute method in WordSearchReplace.replace().");
}
}
public void closeFile() throws SWTException {
int[] id = null;
Variant[] arguments = null;
Variant invokeResult = null;
try {
id = this.activeDocumentAutomation.getIDsOfNames(new String[]{"Close"});
if(id == null) {
throw new SWTException("It was not possible to recover an " +
"identifier for the Close method in " +
"WordSearchReplace.closeFile().");
}
invokeResult = this.activeDocumentAutomation.invoke(id[0]);
if(invokeResult == null) {
throw new SWTException(
"An error occurred invoking the Close method in " +
"WordSearchReplace.closeFile().");
}
}
finally {
if(this.activeDocumentAutomation != null) {
this.activeDocumentAutomation.dispose();
}
}
}
/**
* Release resources.
*/
public void dispose() throws SWTException {
try {
this.cleaned = true;
int[] id = this.wordAutomation.getIDsOfNames(new String[]{"Quit"});
if(id == null) {
throw new SWTException("Unable to obtain an id for the Quit " +
"property in WordSearchReplace.dispose().");
}
Variant result = this.wordAutomation.invoke(id[0]);
if(result == null) {
throw new SWTException("A problem occurred trying to invoke the " +
"Quit method in WordSearchReplace.dispose().");
}
}
finally {
// Finally, dispose of the word application automation.
this.wordAutomation.dispose();
}
}
public void finalize() throws Throwable {
if(!this.cleaned) {
this.dispose();
}
}
private OleAutomation getChildAutomation(OleAutomation automation,
String childName) throws SWTException {
int[] id = automation.getIDsOfNames(new String[]{childName});
if (id == null) {
throw new SWTException(
"A problem occurred trying to obtain and id for: " +
childName +
"in the getChildAutomation() method.");
}
Variant pVarResult = automation.getProperty(id[0]);
if (pVarResult == null) {
throw new SWTException("A problem occurred trying to obtain an automation for property: " + id[0] + " in the getChildAutomation() method.");
}
return(pVarResult.getAutomation());
}
private boolean setPropertyValue(OleAutomation automation,
String propertyName,
Variant[] arguments) throws SWTException,
NullPointerException,
IllegalArgumentException {
if(automation == null) {
throw new NullPointerException(
"A null value was passed to the automation parameter of " +
"WordSearchReplace.setPropertyValue().");
}
if(propertyName == null) {
throw new NullPointerException(
"A null value was passed to the propertyName parameter of " +
"WordSearchReplace.setPropertyValue().");
}
if(propertyName.length() == 0) {
throw new IllegalArgumentException(
"An empty - zero length - String was passed to the propertyName " +
"parameter of WordSearchReplace.setPropertyValue().");
}
if(arguments == null) {
throw new NullPointerException(
"A null value was passed to the arguments parameter of " +
"WordSearchReplace.setPropertyValue().");
}
if(arguments.length == 0) {
throw new IllegalArgumentException(
"An empty - zero length - array was passed to the arguments " +
"parameter of WordSearchReplace.setPropertyValue().");
}
int[] id = automation.getIDsOfNames(new String[]{propertyName});
if(id == null) {
throw new SWTException("Unable to obtain an identifier for the " +
propertyName +
" property in WordSearchReplace.setPropertyValue().");
}
return(automation.setProperty(id[0], arguments));
}
public static void main(String[] args) {
WordSearchReplace wordSR = null;
try {
wordSR = new WordSearchReplace();
wordSR.openFile("c:\\Java\\MyDoc.doc");
wordSR.replace("texto", "TextoReplace");
wordSR.save();
wordSR.closeFile();
//wordSR.save();
}
catch(Exception e) {
System.out.println("Caught: " + e.getClass().getName());
System.out.println(e.getMessage());
e.printStackTrace(System.out);
}
finally {
if(wordSR != null) {
try {
wordSR.dispose();
}
catch(Exception innerE) {
System.out.println("Caught: " + innerE.getClass().getName());
System.out.println(innerE.getMessage());
innerE.printStackTrace(System.out);
}
}
}
}
}