Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:38:48+00:00 2026-06-11T15:38:48+00:00

Okay, so I have been looking at the sample code below from the Ephesoft

  • 0

Okay, so I have been looking at the sample code below from the Ephesoft Developer’s Guide…

//import java.io.File;
//
//import javax.xml.transform.Result;
//import javax.xml.transform.Source;
//import javax.xml.transform.Transformer;
//import javax.xml.transform.TransformerConfigurationException;
//import javax.xml.transform.TransformerException;
//import javax.xml.transform.TransformerFactory;
//import javax.xml.transform.TransformerFactoryConfigurationError;
//import javax.xml.transform.dom.DOMSource;
//import javax.xml.transform.stream.StreamResult;
//
//import org.w3c.dom.Document;
//import org.w3c.dom.Element;
//import org.w3c.dom.Node;
//import org.w3c.dom.NodeList;

import com.ephesoft.dcma.script.IScripts;
//--------------------------------------

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

//import com.ephesoft.dcma.script.IJDomScript;


/**
 * The <code>ScriptDocumentAssembler</code> class represents the script execute structure. Writer of scripts plug-in should implement this IScript
 * interface to execute it from the scripting plug-in. Via implementing this interface writer can change its java file at run time.
 * Before the actual call of the java Scripting plug-in will compile the java and run the new class file.
 * 
 * @author Ephesoft
 * @version 1.0
 */

public class ScriptDocumentAssembler 
{

    private static final String BATCH_LOCAL_PATH = "BatchLocalPath";

    private static final String BATCH_INSTANCE_ID = "BatchInstanceIdentifier";

    private static final String EXT_BATCH_XML_FILE = "_batch.xml";

    private static final String DOCUMENTS = "Documents";

    private static final String DOCUMENT = "Document";

    private static final String PAGES = "Pages";

    private static final String PAGE = "Page";

    private static String ZIP_FILE_EXT = ".zip";

    /**
     * The <code>execute</code> method will execute the script written by the writer at run time with new compilation of java file. It
     * will execute the java file dynamically after new compilation.
     * 
     * @param document {@link Document}
     */
    public void execute(Document document, String fieldName, String docIdentifier) {
        System.out.println("*************  Inside ScriptDocumentAssembler scripts.");
        System.out.println("*************  Start execution of the ScriptDocumentAssembler scripts.");
        System.out.println("Custom ScriptDocumentAssembler, removing Document seperator sheets...");
        removeFirstPageOfDoc(document);
        boolean isWrite = true;
        //boolean isWrite = false;
        // write the document object to the XML file.
        if (isWrite) {
            writeToXML(document);
            System.out.println("*************  Successfully write the xml file for the ScriptDocumentAssembler scripts.");
        } else {
            System.out.println("************** No changes performed by ScriptDocumentAssembler scripts.");
        }
        System.out.println("*************  End execution of the ScriptDocumentAssembler scripts.");
    }

    private void removeFirstPageOfDoc(Document documentFile) {
        Element documentsList = (Element) documentFile.getChildren(DOCUMENTS).get(0);
        List<?> documentList = documentsList.getChildren(DOCUMENT);
        for (int documentIndex = 0; documentIndex < documentList.size(); documentIndex++) {
            Element document = (Element) documentList.get(documentIndex);
            System.out.println("Processing Document - " + document.getChildren("Identifier").get(0).getText());
            Element pages = (Element) document.getChildren(PAGES).get(0);
            List<?> pageList = pages.getChildren(PAGE);         
            Element page = (Element)pageList.get(0);            

            System.out.println(document.getChildren("Identifier").get(0).getText() + " Page Count = " + pageList.size());
            System.out.println("Removing page node " + page.getChildren("Identifier").get(0).getText() + " from " +
                    document.getChildren("Identifier").get(0).getText());
            pages.remove(page);             
            System.out.println(document.getChildren("Identifier").get(0).getText() + " Page Count = " + pageList.size());           
        }
    }

    private void writeToXML(Document document) {
        String batchLocalPath = null;
        List<?> batchLocalPathList = document.getRootElement().getChildren(BATCH_LOCAL_PATH);
        if (null != batchLocalPathList) {
            batchLocalPath = ((Element) batchLocalPathList.get(0)).getText();
        }
        if (null == batchLocalPath) {
                System.err.println("Unable to find the local folder path in batch xml file.");
            return;
        }
        String batchInstanceID = null;
        List<?> batchInstanceIDList = document.getRootElement().getChildren(BATCH_INSTANCE_ID);
        if (null != batchInstanceIDList) {
            batchInstanceID = ((Element) batchInstanceIDList.get(0)).getText();
        }
        if (null == batchInstanceID) {
            System.err.println("Unable to find the batch instance ID in batch xml file.");
            return;
        }
        String batchXMLPath = batchLocalPath.trim() + File.separator + batchInstanceID + File.separator + batchInstanceID
        + EXT_BATCH_XML_FILE;
        String batchXMLZipPath = batchXMLPath + ZIP_FILE_EXT;
        System.out.println("batchXMLZipPath************" + batchXMLZipPath);
        OutputStream outputStream = null;
        File zipFile = new File(batchXMLZipPath);
        FileWriter writer = null;
        XMLOutputter out = new XMLOutputter();
        try {
            if (zipFile.exists()) {
                System.out.println("Found the batch xml zip file.");
                outputStream = getOutputStreamFromZip(batchXMLPath, batchInstanceID + EXT_BATCH_XML_FILE);
                out.output(document, outputStream);
            } else {
                writer = new java.io.FileWriter(batchXMLPath);
                out.output(document, writer);
                writer.flush();
                writer.close();
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public static OutputStream getOutputStreamFromZip(final String zipName, final String fileName) throws FileNotFoundException, IOException {
        ZipOutputStream stream = null;
        stream = new ZipOutputStream(new FileOutputStream(new File(zipName + ZIP_FILE_EXT)));
        ZipEntry zipEntry = new ZipEntry(fileName);
        stream.putNextEntry(zipEntry);
        return stream;
    }

}

Note I have not changed anything from the original code, but I added the jdom and ephesoft jars to my build path. However, within the removeFirstPageOfDoc method, I am still getting a bunch of errors related to the casting.For example, the line Element documentsList = (Element) documentFile.getChildren(DOCUMENTS).get(0); should allow documentFile to gain access to the methods of Element, right? However, it still seems to only have access to the methods of type document. I was just wondering what the issue might be here and how I might go about resolving it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:38:50+00:00Added an answer on June 11, 2026 at 3:38 pm

    For example, the line Element documentsList = (Element) documentFile.getChildren(DOCUMENTS).get(0); should allow documentFile to gain access to the methods of Element, right?

    No, because casting has lower precedence than the dot operator. To cast documentFile to type Element, you would write this:

    Element documentsList = ((Element) documentFile).getChildren(DOCUMENTS).get(0);
    

    with parentheses around (Element) documentFile.

    Edited to add (incorporating information from the comments below):

    However, according to the Javadoc for org.jdom.Document and that for org.jdom.Element, they’re both actual classes — neither one is an interface — and neither is a subtype of the other. This means that you can’t actually cast from one to the other. (In Java, a cast doesn’t let you convert an instance of one type into another type; in order for ((Type) reference) to work, reference has to refer to an object that really does belong to type Type. Since an object can never be an instance of both Element and Document, the compiler won’t even allow this sort of cast here.)

    Instead, the person who wrote this sample-code probably should have written this:

    Element documentsList =
        documentFile.getRootElement().getChildren(DOCUMENTS).get(0);
    

    which uses the getRootElement() method (which returns the document’s root element) rather than casting to Element (which would try to convince the compiler that the document simply is an element).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I have been looking around on the internet for any examples of
Okay let me start off by saying I have been looking around a lot
okay i have been trying to understand this for hours i am learning VB
Okay, so I have been searching and searching and have not found any help.
Okay, guys i have been trying to define a Stack , each node also
Okay. Now I give up. I have been playing with this for hours. I
Okay, I have just been through a massive amount of bull craparooonie with xcodes
Okay I been trying to work this out but unable too. I have a
I have been reading the code used by R to fit a generalized linear
Okay I have 2 files. One file is data that is updated every 10

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.