Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
wlfxb - a library for creating and processing of TCF data streams. Copyright (C) Yana Panchenko. This file is part of wlfxb. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
 package eu.clarin.weblicht.wlfxb.io;
 
 import java.io.*;
 import java.util.List;
Class WLDObjector helps to read/write eu.clarin.weblicht.wlfxb.xb.WLData from/to TCF stream.

Author(s):
Yana Panchenko
 
 public class WLDObjector {
     
     public static WLData read(InputStream inputStreamthrows WLFormatException {
         WLData data = null;
         try {
             JAXBContext context = JAXBContext.newInstance(WLData.class);
             Unmarshaller unmarshaller = context.createUnmarshaller();
             data = ((WLDataunmarshaller.unmarshal(inputStream));
         } catch (JAXBException e) {
             throw new WLFormatException(e.getMessage(), e);
         }
         return data;
     }
 
     public static WLData read(Reader readerthrows WLFormatException {
         WLData data = null;
         try {
             JAXBContext context = JAXBContext.newInstance(WLData.class);
             Unmarshaller unmarshaller = context.createUnmarshaller();
             data = ((WLDataunmarshaller.unmarshal(reader));
         } catch (JAXBException e) {
             throw new WLFormatException(e.getMessage(), e);
         }
         return data;
     }
 
     public static WLData read(File filethrows WLFormatException {
         WLData data = null;
         FileInputStream fis = null;
         try {
             fis = new FileInputStream(file);
             data = read(fis);
         } catch (IOException e) {
             throw new WLFormatException(e);
         } finally {
             if (fis != null) {
                 try {
                     fis.close();
                 } catch (IOException e) {
                     throw new WLFormatException(e);
                 }
             }
         }
         return data;
     }
 
     public static void write(WLData wlDataOutputStream outputStreamthrows WLFormatException {
         write(wlData.getMetaData(), wlData.getTextCorpus(), outputStreamfalse);
     }
 
     public static void write(WLData wlDataFile filethrows WLFormatException {
        write(wlData.getMetaData(), wlData.getTextCorpus(), filefalse);
    }
    public static void write(WLData wlDataOutputStream outputStreamboolean outputAsXmlFragmentthrows WLFormatException {
        write(wlData.getMetaData(), wlData.getTextCorpus(), outputStreamoutputAsXmlFragment);
    }
    public static void write(WLData wlDataFile fileboolean outputAsXmlFragmentthrows WLFormatException {
        write(wlData.getMetaData(), wlData.getTextCorpus(), fileoutputAsXmlFragment);
    }
    public static void write(MetaData mdTextCorpus tcFile fileboolean outputAsXmlFragmentthrows WLFormatException {
        // IMPORTANT: using JAXB marshaller for marshalling directly into File or FileOutputStream
        // replaces quotes with &quot; entities which is not desirable for linguistic data, therefore
        // XMLEventWriter should be used...
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            write(mdtcoutputStreamoutputAsXmlFragment);
        } catch (Exception e) {
            throw new WLFormatException(e);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new WLFormatException(e);
                }
            }
        }
    }
    public static void write(MetaData mdTextCorpus tc,
            OutputStream outputStreamboolean outputAsXmlFragment)
            throws WLFormatException {
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLOutputFactory xmlOututFactory = XMLOutputFactory.newInstance();
        XMLEvent e;
        XMLEventWriter xmlEventWriter = null;
        
        try {
            xmlEventWriter = xmlOututFactory.createXMLEventWriter(outputStream"UTF-8");
            if (!outputAsXmlFragment) {
                e = eventFactory.createStartDocument("UTF-8");
                xmlEventWriter.add(e);
                e = eventFactory.createIgnorableSpace(.);
                xmlEventWriter.add(e);
                e = eventFactory.createProcessingInstruction(
                        .,
                        .);
                xmlEventWriter.add(e);
                e = eventFactory.createIgnorableSpace(.);
                xmlEventWriter.add(e);
            }
            Attribute versionAttr = eventFactory.createAttribute("version".);
            List<Attributeattrs = new ArrayList<Attribute>();
            attrs.add(versionAttr);
            Namespace ns = eventFactory.createNamespace(.);
            List<Namespacenss = new ArrayList<Namespace>();
            nss.add(ns);
            e = eventFactory.createStartElement(""..attrs.iterator(), nss.iterator());
            xmlEventWriter.add(e);
            e = eventFactory.createIgnorableSpace(.);
            xmlEventWriter.add(e);
            JAXBContext mdContext = JAXBContext.newInstance(MetaData.class);
            Marshaller mdMarshaller = mdContext.createMarshaller();
            //does not work with XMLEventWriter:
            //mdMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            mdMarshaller.setProperty(.true);
            mdMarshaller.marshal(mdxmlEventWriter);
            
            e = eventFactory.createIgnorableSpace(.);
            xmlEventWriter.add(e);
            JAXBContext tcContext = JAXBContext.newInstance(TextCorpusStored.class);
            Marshaller tcMarshaller = tcContext.createMarshaller();
            //does not work with XMLEventWriter:
            //tcMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            tcMarshaller.setProperty(.true);
            tcMarshaller.marshal(tcxmlEventWriter);
            
            e = eventFactory.createIgnorableSpace(.);
            xmlEventWriter.add(e);
            e = eventFactory.createEndElement(""..);
            xmlEventWriter.add(e);
            if (!outputAsXmlFragment) {
                e = eventFactory.createEndDocument();
                xmlEventWriter.add(e);
            }
        } catch (Exception ex) {
            throw new WLFormatException(ex.getMessage(), ex);
        } finally {
            if (xmlEventWriter != null) {
                try {
                    xmlEventWriter.flush();
                    xmlEventWriter.close();
                } catch (XMLStreamException ex2) {
                    throw new WLFormatException(ex2.getMessage(), ex2);
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X