2013-05-08 3 views
0

Я отправился в репозиторий GitHub для файлов docx4j и загрузил VariableReplace. Когда я скопировал этот файл в netbeans, я получил ошибку в строке 86 (Cannont find symbol).variableReplace с сообщением об ошибке

Вот код:

/* 
* Copyright 2007-2008, Plutext Pty Ltd. 
* 
* This file is part of docx4j. 

    docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 

    You may obtain a copy of the License at 

     http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 

*/ 


import java.util.HashMap; 

import org.docx4j.XmlUtils; 
import org.docx4j.jaxb.Context; 
import org.docx4j.openpackaging.io.SaveToZipFile; 
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; 
import org.docx4j.wml.Document; 


/** 
* There are at least 3 approaches for replacing variables in 
* a docx. 
* 
* 1. as shows in this example 
* 2. using Merge Fields (see org.docx4j.model.fields.merge.MailMerger) 
* 3. binding content controls to an XML Part (via XPath) 
* 
* Approach 3 is the recommended one when using docx4j. See the 
* ContentControl* examples, Getting Started, and the subforum. 
* 
* Approach 1, as shown in this example, works in simple cases 
* only. It won't work if your KEY is split across separate 
* runs in your docx (which often happens), or if you want 
* to insert images, or multiple rows in a table. 
* 
* You're encouraged to investigate binding content controls 
* to an XML part. There is org.docx4j.model.datastorage.migration.FromVariableReplacement 
* to automatically convert your templates to this better 
* approach. 
* 
* OK, enough preaching. If you want to use VariableReplace, 
* your variables should be appear like so: ${key1}, ${key2} 
* 
* And if you are having problems with your runs being split, 
* VariablePrepare can clean them up. 
* 
*/ 
public class VariableReplace { 

    public static void main(String[] args) throws Exception { 

     // Exclude context init from timing 
     org.docx4j.wml.ObjectFactory foo = Context.getWmlObjectFactory(); 

     // Input docx has variables in it: ${colour}, ${icecream} 
     String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/unmarshallFromTemplateExample176.docx"; 

     boolean save = false; 
     String outputfilepath = System.getProperty("user.dir") 
       + "/OUT_VariableReplace.docx"; 

     WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage 
       .load(new java.io.File(inputfilepath)); 
     MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); 

     HashMap<String, String> mappings = new HashMap<String, String>(); 
     mappings.put("colour", "green"); 
     mappings.put("icecream", "chocolate"); 

     long start = System.currentTimeMillis(); 

     // Approach 1 (from 3.0.0; faster if you haven't yet caused unmarshalling to occur): 

      documentPart.variableReplace(mappings); 

/*  // Approach 2 (original) 

      // unmarshallFromTemplate requires string input 
      String xml = XmlUtils.marshaltoString(documentPart.getJaxbElement(), true); 
      // Do it... 
      Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings); 
      // Inject result into docx 
      documentPart.setJaxbElement((Document) obj); 
*/ 

     long end = System.currentTimeMillis(); 
     long total = end - start; 
     System.out.println("Time: " + total); 

     // Save it 
     if (save) { 
      SaveToZipFile saver = new SaveToZipFile(wordMLPackage); 
      saver.save(outputfilepath); 
     } else { 
      System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, 
        true)); 
     } 
    } 

} 

Кто-нибудь знает, почему я получаю эту ошибку?

Вот несколько фотографий:

Error 1 Overview

Error 1 Details

Благодаря Грач

ответ

0

EDIT:

Я не получаю немедленные ошибки, но когда я бегу код ниже, я получаю следующие ошибки:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger 
    at org.docx4j.openpackaging.Base.<clinit>(Base.java:42) 
    at VariableReplace.main(VariableReplace.java:61) 
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 
    ... 2 more 
Java Result: 1 

Я только что выяснил проблему :). Я использовал старые образцы.

Вот новый код:

/* 
* Copyright 2007-2008, Plutext Pty Ltd. 
* 
* This file is part of docx4j. 

    docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 

    You may obtain a copy of the License at 

     http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 

*/ 

package org.docx4j.samples; 


import java.util.HashMap; 

import org.docx4j.XmlUtils; 
import org.docx4j.openpackaging.io.SaveToZipFile; 
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; 
import org.docx4j.wml.Document; 


/** 
* There are at least 3 approaches for replacing variables in 
* a docx. 
* 
* 1. as shows in this example 
* 2. using Merge Fields (see org.docx4j.model.fields.merge.MailMerger) 
* 3. binding content controls to an XML Part (via XPath) 
* 
* Approach 3 is the recommended one when using docx4j. See the 
* ContentControl* examples, Getting Started, and the subforum. 
* 
* Approach 1, as shown in this example, works in simple cases 
* only. It won't work if your KEY is split across separate 
* runs in your docx (which often happens), or if you want 
* to insert images, or multiple rows in a table. 
* 
* You're encouraged to investigate binding content controls 
* to an XML part. 
* 
*/ 
public class VariableReplace { 

    public static void main(String[] args) throws Exception { 

     String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/unmarshallFromTemplateExample.docx"; 

     boolean save = false; 
     String outputfilepath = System.getProperty("user.dir") 
       + "/OUT_VariableReplace.docx"; 

     WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage 
       .load(new java.io.File(inputfilepath)); 
     MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); 

     // unmarshallFromTemplate requires string input 
     String xml = XmlUtils.marshaltoString(documentPart.getJaxbElement(), true); 

     HashMap<String, String> mappings = new HashMap<String, String>(); 
     mappings.put("colour", "green"); 
     mappings.put("icecream", "chocolate"); 

     // Do it... 
     Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings); 

     // Inject result into docx 
     documentPart.setJaxbElement((Document) obj); 

     // Save it 
     if (save) { 
      SaveToZipFile saver = new SaveToZipFile(wordMLPackage); 
      saver.save(outputfilepath); 
     } else { 
      System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, 
        true)); 
     } 
    } 

} 
+0

я не уверен ли вы решить вашу проблему или нет, но вам нужно log4j банку (и другие зависимости docx4j в) на вашем пути к классам. – JasonPlutext

+1

У меня есть файл jar, но я не уверен, что он в моем классе, спасибо. Я попробую это, когда вернусь домой. –

Смежные вопросы