Solved

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

Posted on 2016-10-24
3
74 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

777 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