0

さて、私は 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;
    }

}

元のコードから何も変更していないことに注意してください。ただし、ビルド パスに jdom および ephesoft jar を追加しました。ただし、removeFirstPageOfDoc メソッド内では、キャストに関連するエラーが引き続き発生します。たとえば、この行Element documentsList = (Element) documentFile.getChildren(DOCUMENTS).get(0);では、documentFile が Element のメソッドにアクセスできるようにする必要があります。ただし、タイプ document のメソッドにしかアクセスできないようです。ここで何が問題なのか、どうすれば解決できるのだろうかと思っていました。

4

2 に答える 2

3

たとえば、この行Element documentsList = (Element) documentFile.getChildren(DOCUMENTS).get(0);は documentFile が Element のメソッドにアクセスできるようにする必要がありますよね?

いいえ、キャストはドット演算子よりも優先順位が低いためです。documentFiletypeにキャストするにElementは、次のように記述します。

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

を括弧で囲みます(Element) documentFile

追加するために編集されました (以下のコメントからの情報を組み込みます)

ただし、Javadoc fororg.jdom.Documentおよびfororg.jdom.Elementによると、どちらも実際のクラスであり、どちらもインターフェースではなく、どちらも他方のサブタイプではありません。これは、実際に一方から他方にキャストできないことを意味します。(Java では、キャストによって、あるタイプのインスタンスを別のタイプに変換することはできません。 for((Type) reference)が機能するためにreferenceは、実際に type に属しているオブジェクトを参照する必要がありますType。オブジェクトが両方のインスタンスになることは決してないため、ElementそしてDocument、コンパイラはここでこの種のキャストさえ許可しません。)

代わりに、このサンプル コードを書いた人はおそらく次のように書いたはずです。

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

getRootElement()にキャストするのではなく (ドキュメントのルート要素を返す) メソッドを使用します (ドキュメントが単に要素Elementあることをコンパイラに納得させようとします)。

于 2012-09-18T17:15:25.277 に答える
1

ruakh は正しいですが、次のレベルも行う必要があります。

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

もちろん、JDOM 2.x (正しい一般的な型付け) では、これはすべて簡単です....

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

ロルフル

于 2012-09-18T17:47:01.877 に答える