/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mitre.taxii.messages.xml11;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.Unmarshaller;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/**
 *
 * @author jasenj1
 */

@RunWith(Parameterized.class)
public class PythonTextOutputFileTests {
    private static TaxiiXml taxiiXml;
    private static Unmarshaller unmarshaller;
    
    private static final String inputPath = "src/test/resources/python_output/1.1";
    private static final String outputPath = "build/test-results";
    
    @Parameterized.Parameter(0)
    public File file;
    
    private static File outDir;
    
    @BeforeClass
    public static void init() throws Exception {
        TaxiiXmlFactory txf = new TaxiiXmlFactory();
        taxiiXml = txf.createTaxiiXml();
        unmarshaller = taxiiXml.getJaxbContext().createUnmarshaller();
        outDir = new File(outputPath);
    }

    @Parameterized.Parameters(name="{index} - {0}")
    public static Iterable<File[]> data() throws IOException {
        final List<File[]> data = new ArrayList<>();        
        Files.walkFileTree(
                Paths.get(inputPath),
                new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        if (!file.toFile().isHidden()) {
                            data.add(new File[] { file.toFile() });
                        }
                        return FileVisitResult.CONTINUE;
                    }
                });
        return data;
    }
    
    @Test
    public void testFile() throws Exception {
        MessageType m = (MessageType) unmarshaller.unmarshal(file);
        String s = PythonTextOutput.toText(m);
        String outName = file.getName().replaceFirst("[.][^.]+$", "");
        File outFile = new File(outDir,outName + ".txt");
        
        Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
        System.out.println(s);
        writer.write(s);
        writer.close();
    }
    
}

@@ -371,36 +371,8 @@
 er;%0A
-import java.io.PrintWriter;%0A
 impo
@@ -815,16 +815,59 @@
 terized;
+%0Aimport org.mitre.taxii.query.DefaultQuery;
 %0A%0A/**%0A *
@@ -1418,24 +1418,102 @@
 lFactory();%0A
+        txf.addJaxbContextPackage(DefaultQuery.class.getPackage().getName());%0A
         taxi
@@ -2500,55 +2500,8 @@
 e);%0A
-        String s = PythonTextOutput.toText(m);%0A
     
@@ -2563,24 +2563,69 @@
 .%5D+$%22, %22%22);%0A
+        %0A        // Write Python text output%0A
         File
@@ -2625,17 +2625,20 @@
    File 
-o
+txtO
 utFile =
@@ -2654,16 +2654,17 @@
 (outDir,
+ 
 outName 
@@ -2686,25 +2686,365 @@
     
-%0A        Writer w
+Writer txtWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(txtOutFile), %22utf-8%22));%0A        String s = PythonTextOutput.toText(m);%0A        System.out.println(s);%0A        txtWriter.write(s);%0A        txtWriter.close();%0A        %0A        // Write XML output%0A        File xmlOutFile = new File(outDir, outName + %22.xml%22);%0A        Writer xmlW
 rite
@@ -3110,17 +3110,20 @@
 tStream(
-o
+xmlO
 utFile),
@@ -3126,32 +3126,88 @@
 le), %22utf-8%22));%0A
+        String xml = taxiiXml.marshalToString(m, true);%0A
         System.o
@@ -3209,33 +3209,35 @@
 tem.out.println(
-s
+xml
 );%0A        write
@@ -3227,25 +3227,28 @@
 l);%0A        
-w
+xmlW
 riter.write(
@@ -3239,33 +3239,35 @@
 xmlWriter.write(
-s
+xml
 );%0A        write
@@ -3261,17 +3261,20 @@
         
-w
+xmlW
 riter.cl

Add support for DefaultQuery text output.