1

我目前正在开发一个基于某些模板文件生成和验证 .xml 文件的应用程序。

我调用生成这些文件的方法如下:

FtlProcessingController ctrl = new FtlProcessingController();
ctrl.process(TEMPLATES, OUTPUT_DIRECTORY, root, metadata);

我不是这个FtlProcessingController类的作者,但是process方法基本上从TEMPLATES目录中获取模板文件,用对象(Answer 对象树)和metadata对象(附加数据的 Map )内部的内容填充它们,并将 .xml 文件输出OUTPUT_DIRECTORY 。

简而言之,主要代码如下所示:

// Prepare files and data

FtlProcessingController ctrl = new FtlProcessingController();
ctrl.process(TEMPLATES, OUTPUT_DIRECTORY, root, metadata);

// Validate XML files

问题是进程调用后的任何控制台输出都不起作用;控制台中没有显示任何内容。

我尝试用一​​些测试输出围绕呼叫并保存PrintStream重置:

System.out.println("Console");
PrintStream stdOut = System.out;

FtlProcessingController ctrl = new FtlProcessingController();
ctrl.process(TEMPLATES, OUTPUT_DIRECTORY, root, metadata);

// Test Exception was thrown here.

System.setOut(stdOut);
System.out.println("Console again");

但我只得到:

安慰

我确定在此调用中没有无限循环System.exit(),因为文件已成功生成。我什至尝试在调用后立即抛出异常以确认这一点,并且异常被正常抛出。

我的问题是:不管process方法对System.out做什么或不做什么,我不应该能够在System.setOut(stdOut)通话后在控制台上再次打印吗?

什么可能会弄乱标准输出以使保存的 PrintStream 不起作用?

谢谢!

完整的主要代码,对于好奇:

public static void main(String[] args) {
    try {
        // Parse answers
        Answer root = AnswerUtils.fromJSON(JSON_FILE, Answer.class);

        ModuleInfo manager = new SimpleModuleInfo(MODULE_MANAGER_ID, MODULE_MANAGER_CATEGORY, MODULE_MANAGER_NAME,
                MODULE_MANAGER_VERSION, MODULE_MANAGER_VERSION);
        ModuleInfo module = new SimpleModuleInfo(MODULE_ID, MODULE_CATEGORY, MODULE_NAME, MODULE_VERSION, INSTANCE);

        List<ModuleInfo> info = new ArrayList<ModuleInfo>();
        info.add(manager);
        info.add(module);
        if (DEPENDENCIES_FILES != null) {
            // property file, with all dependencies as a comma-separated value under the 'dependencies' key
            Properties props = new Properties();
            InputStream in = null;
            try {
                in = new FileInputStream(DEPENDENCIES_FILES);
                props.load(in);
            } finally {
                IOUtils.closeQuietly(in);
            }
            String[] values = StringUtils.split(props.getProperty("dependencies"), ',');
            if (values != null) {
                for (String value : values) {
                    ModuleDependency dep = new ModuleDependency(StringUtils.trim(value));
                    info.add(new SimpleModuleInfo(dep.getIdentifier(), "Category", WordUtils.capitalizeFully(
                            dep.getIdentifier(), new char[] { '-' }), dep.getVersion().toString(), dep
                            .isInstanceDependency() ? module.getInstance() : "Default"));
                }
            }
        }
        Map<String, Object> metadata = getMetadata(HOME, os, arch, module, info.toArray(new ModuleInfo[info.size()]));

        System.out.println("Console");
        PrintStream stdOut = System.out;

        FtlProcessingController ctrl = new FtlProcessingController();
        ctrl.process(TEMPLATES, OUTPUT_DIRECTORY, root, metadata);

        System.setOut(stdOut);
        System.out.println("Console again");

        // Validate XML
        Collection<File> xmlFiles = FileUtils.listFiles(OUTPUT_DIRECTORY, OUTPUT_FILES_EXTENSIONS, WITH_SUBDIRECTORIES);
        for (File file : xmlFiles) {
            System.out.println(file.getAbsolutePath());
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setValidating(false);
            factory.setNamespaceAware(true);
            factory.setFeature(FACTORY_DTD_FEATURE, false);

            SAXParser parser = factory.newSAXParser();

            XMLReader reader = parser.getXMLReader();
            reader.setErrorHandler(new SPErrorHandler());
            reader.parse(file.getAbsolutePath());
        }
        if(true)
            throw new IOException();
    } catch (SAXException se) {
        se.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    }
}
4

1 回答 1

1

您调用的方法可能会重新分配System.out流,这可以通过该System.setOut(PrintStream)方法完成。

If that is the case and output is no longer visible in the Java console it maybe that the output is written to a log file. This approach for logging is, however, an extremely bad idea and one shouldn't do it especially in a library that others may end up using.

An idea even worse than that would be to actually close the System.out stream. I can't think of any scenario justifying doing this.

于 2014-01-20T21:21:55.140 回答