You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Matt Kendall <ma...@iname.com> on 2005/11/17 00:14:15 UTC

[betwixt] Array of java.io.File objects.

I've been tearing my hair out on this all afternoon. I've tried
configuring Betwixt in way too many different ways to list them all
here, so instead I'll just ask: what's the best way to handle the File
object, specifically an array of file objects? I would like the
following code to print out "2 == 2".

public class FileTest {
	private File[] files;
	
	public File[] getFiles() {
		return files;
	}
	
	public void setFiles(File[] files) {
		this.files = files;
	}

	public static void main(String[] args) {
		FileTest test = new FileTest();

		test.setFiles(new File[] { new File("a.txt"), new File("b.txt") });

		try {
			BeanWriter writer = new BeanWriter(new FileWriter("ftout.xml"));
			writer.enablePrettyPrint();
			writer.write(test);
			writer.flush();
			writer.close();
			
			BeanReader reader = new BeanReader();
			reader.registerBeanClass(FileTest.class);
			test = (FileTest) reader.parse("ftout.xml");
			
			System.out.println("2 == " + test.getFiles().length);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

I apologize if this question is too vague, but I've created so many
mappers, converters, chains, etc. that enumerating all of them is much
more complicated than simply understanding what I want from the
following code.

Oh, and the one solution I cannot employ is to rewrite the bean that I
need to convert to XML. I didn't write it so I cannot change it.

Thanks!
Matt

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [betwixt][SOLVED] Array of java.io.File objects.

Posted by Matt Kendall <ma...@iname.com>.
It's always a good idea to take breaks during problem solving, I found
this solution in about thirty minutes this morning. The answer wound
up being quite simple (as I knew it should be). Posting for posterity:

For output, use a .betwixt file to suppress the majority of the File
object's properties:

<info>
	<element name="File">
		<attribute name="absolutePath" property="absolutePath"/>
	</element>
</info>

For input, add a custom creator the the BeanCreationChain (I did so by
using a BeanCreationList) that uses the absolutePath attribute to
return a file object, like so:

public class FileCreator implements ChainedBeanCreator {
	public Object create(ElementMapping mapping, ReadContext context,
			BeanCreationChain chain) {
		if (File.class.equals(mapping.getType())) {
			return new File(mapping.getAttributes().getValue("absolutePath"));
		}
		return chain.create(mapping, context);
	}
}

This kind of pattern *should* work for any complex object that you can
easily construct using one or more of its properties.

Matt

On 11/16/05, Matt Kendall <ma...@iname.com> wrote:
> I've been tearing my hair out on this all afternoon. I've tried
> configuring Betwixt in way too many different ways to list them all
> here, so instead I'll just ask: what's the best way to handle the File
> object, specifically an array of file objects? I would like the
> following code to print out "2 == 2".
>
> public class FileTest {
>         private File[] files;
>
>         public File[] getFiles() {
>                 return files;
>         }
>
>         public void setFiles(File[] files) {
>                 this.files = files;
>         }
>
>         public static void main(String[] args) {
>                 FileTest test = new FileTest();
>
>                 test.setFiles(new File[] { new File("a.txt"), new File("b.txt") });
>
>                 try {
>                         BeanWriter writer = new BeanWriter(new FileWriter("ftout.xml"));
>                         writer.enablePrettyPrint();
>                         writer.write(test);
>                         writer.flush();
>                         writer.close();
>
>                         BeanReader reader = new BeanReader();
>                         reader.registerBeanClass(FileTest.class);
>                         test = (FileTest) reader.parse("ftout.xml");
>
>                         System.out.println("2 == " + test.getFiles().length);
>                 } catch (Exception e) {
>                         e.printStackTrace();
>                 }
>
>         }
>
> }
>
> I apologize if this question is too vague, but I've created so many
> mappers, converters, chains, etc. that enumerating all of them is much
> more complicated than simply understanding what I want from the
> following code.
>
> Oh, and the one solution I cannot employ is to rewrite the bean that I
> need to convert to XML. I didn't write it so I cannot change it.
>
> Thanks!
> Matt
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org