Please note that this page is still work-in-progress.
Input Output (IO) and XML #
This page contains the following content:
CAKE provides a centralized concept for all IO operations. IO operations are for example XML parsers and writers or network protocols. The goal of the concept is to easily replace an XML parser or writer with another one or with another technologies. This should be possible without changing the code.
CAKE IO Xerces XML #
In most cases, the import of files is controlled via the CakeInstance. However, if a dedicated reading of a file is required, the IOUtil
class can be used. This provides methods for reading and writing in order to deal with XML files. The reader and writer are based on the xerces XML package, see Xerces.
XML Parser #
To parse an XML stream the IOUtil.readFile
method is provided. The method requires as first parameter the path of the XML file. If already an input stream is available, the filename can be exchanged with the input stream. The second parameter is the name of the reader (if known) or instead the class of the object to be read. As a result, there are 4 ways to read an XML file which are shown in the code snippets below. The examples are made by importing a case base from an XML file. A case base is known to be in the format of an ObjectPool object, therefore the reader with the name ObjectPoolReader
is used.
IO Factory #
All IO components to convert object to and from streams can be requested in the IOFactory
. The factory is a normal Factory
where several IO components can be bound.
Access to an IO component is always the same:
- request the IO names that are able to handle a given class
- request the IO component with one of the names
For example, to get a parser for a
Model
the Java code would be:
String names[] = IOFactory.getReaderNamesFor(Model.class);
if (names.length == 0)
throw new CAKEIOException(IO.LOG_READER_NOT_FOUND, Model.class);
Reader reader = (Reader) IOFactory.newIO(names[0]);
reader.setFilename("...");
Model model = (Model) reader.read();
One factory is available: IOFactory