Solved

Java Filesystem manipulation of Word Docx file (find replace text)

Posted on 2016-10-24
3
39 Views
Last Modified: 2016-10-24
I am attempting to use a simple find and replace method using example code provided below. This is in Beanshell in my app. I am getting an error on the line indicated; Does so if I use 'env' or 'Null'! The logit lines indicate that 'env' is populated and the path coming from a form does exist. (using UNC path to network share).

Error: "Typed variable declaration : Attempt to resolve method: newFileSystem() on undefined variable or class name: FileSystems : at Line: 46 : in file: inline evaluation of: FileSystems .newFileSystem ( docxUri , null )"

Help very much appreciated.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

File zipfile = new File(thisProcess.getFieldValue("txt_Path"));
URI docxUri = new URI("jar:" + zipfile.toURI());

Map env = new HashMap();
importObject( env );
put("create", "false");
put("encoding", "UTF-8");

if(zipfile.exists() && !zipfile.isDirectory())
{
    logit.info("Document EXISTS");
}

logit.info(env);

try
{
    FileSystem zipFS = FileSystems.newFileSystem(docxUri, null); //ERROR ON THIS LINE
    Path documentXmlPath = zipFS.getPath("/word/document.xml");

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(Files.newInputStream(documentXmlPath));

    byte[] content = Files.readAllBytes(documentXmlPath);
    String xml = new String(content, StandardCharsets.UTF_8);
    xml = xml.replace("#DATE#", "2014-09-24");
    xml = xml.replace("#NAME#", StringEscapeUtils.escapeXml("Sniper"));
    content = xml.getBytes(StandardCharsets.UTF_8);
    Files.delete(documentXmlPath);
    Files.write(documentXmlPath, content);
}
0
Comment
Question by:BrightRaven
  • 2
3 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 41856649
Not sure if Beanshell supports APIs of younger age. Why are you using it btw?
0
 

Author Comment

by:BrightRaven
ID: 41856656
Hi CEHJ

The IDE built into the BPM application I am using is either Javascript or Beanshell. (its very basic IDE).
The product is supposed to be extensible with full Java!

Just need the simplest method of finding and replacing text in a Docx and saving it back out with a new name. Was considering Docx4J (or similiar) if this method doesn't work.

Wise old owl guidance appreciated.

A.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 41856660
Was considering Docx4J (or similiar) if this method doesn't work.
That sounds more direct and less likely to fall over owing to complexity involving extra layers of interpretation
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now