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) University of Tübingen. 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 ExternalDataWithTextCorpusStreamed represents TCF TextCorpus and ExternalData annotations. The class is used for accessing specified annotation layers and (optionally) adding any new annotation layers from/to TextCorpus and/or ExternalData. Only specified in the constructor annotation layers are loaded into the memory. In case all the annotation layers should be loaded into the memory, use eu.clarin.weblicht.wlfxb.xb.WLData class.

Author(s):
Yana Panchenko
 
 
     private EnumSet<TextCorpusLayerTagtcLayersToRead = EnumSet.noneOf(TextCorpusLayerTag.class);
     private TextCorpusStored textCorpus;
     private boolean hasTextCorpus = false;
     private ExternalDataStored extData;
     private boolean hasExtData = false;
     private XMLEventReader xmlEventReader;
     private File tempFile;
     private XMLEventWriter xmlEventWriter;
     private XmlReaderWriter xmlReaderWriter;
     private static final int LAYER_INDENT_RELATIVE = 1;
     private boolean closed = false;

    
Creates a ExternalDataWithTextCorpusStreamed from the given TCF input stream and specified annotation layers.

Parameters:
inputStream the underlying input stream with linguistic annotations in TCF format.
edLayersToRead the annotation layers of ExternalData that should be read into this ExternalDataWithTextCorpusStreamed.
tcLayersToRead the annotation layers of TextCorpus that should be read into this ExternalDataWithTextCorpusStreamed.
Throws:
WLFormatException if an error in input format or an I/O error occurs.
 
     public ExternalDataWithTextCorpusStreamed(InputStream inputStream,
             EnumSet<ExternalDataLayerTagedLayersToRead,
             EnumSet<TextCorpusLayerTagtcLayersToRead)
             throws WLFormatException {
         getLayersToReadWithDependencies(edLayersToReadtcLayersToRead);
          = new ExternalDataStored();
          = new TextCorpusStored("unknown");
         try {
             initializeReaderAndWriter(inputStreamnullfalse);
            readLayers();
        } catch (WLFormatException e) {
            Logger.getLogger(ExternalDataWithTextCorpusStreamed.class.getName()).log(.nulle);
            cleanup();
            throw e;
        }
    }

    
Creates a ExternalDataWithTextCorpusStreamed from the given TCF input stream, specified annotation layers and the output stream.

Parameters:
inputStream the underlying input stream with linguistic annotations in TCF format.
edLayersToRead the annotation layers of ExternalData that should be read into this ExternalDataWithTextCorpusStreamed.
tcLayersToRead the annotation layers of TextCorpus that should be read into this ExternalDataWithTextCorpusStreamed.
outputStream the underlying output stream into which the annotations from the input stream and any new created annotations will be written (in TCF format).
Throws:
WLFormatException if an error in input format or an I/O error occurs.
            EnumSet<ExternalDataLayerTagedLayersToRead,
            EnumSet<TextCorpusLayerTagtcLayersToRead,
            OutputStream outputStream)
            throws WLFormatException {
         = new ExternalDataStored();
         = new TextCorpusStored("unknown");
        getLayersToReadWithDependencies(edLayersToReadtcLayersToRead);
        OutputStream osTemp = null;
        InputStream isTemp = null;
        try {
            osTemp = getTempOutputStream();
            initializeReaderAndWriter(inputStreamosTempfalse);
            readLayers();
            isTemp = getTempInputStream();
            initializeReaderAndWriter(isTempoutputStreamfalse);
        } catch (WLFormatException e) {
            Logger.getLogger(ExternalDataWithTextCorpusStreamed.class.getName()).log(.nulle);
            cleanup();
            throw e;
        }
    }

    
Gets TextCorpus.

Returns:
TextCorpus
    public TextCorpus getTextCorpus() {
        return ;
    }

    
Gets ExternalData.

Returns:
ExternalData
    public ExternalData getExternalData() {
        return ;
    }
    private OutputStream getTempOutputStream() throws WLFormatException {
        OutputStream os = null;
        try {
             = File.createTempFile("wlftemp"null);
            os = new FileOutputStream();
        } catch (IOException e) {
            Logger.getLogger(ExternalDataWithTextCorpusStreamed.class.getName()).log(.nulle);
            if (os != null) {
                try {
                    os.close();
                } catch (IOException ex) {
                    Logger.getLogger(ExternalDataWithTextCorpusStreamed.class.getName()).log(.nullex);
                }
            }
            cleanup();
            throw new WLFormatException(e);
        }
        return os;
    }
    private InputStream getTempInputStream() throws WLFormatException {
        InputStream is = null;
        try {
            is = new FileInputStream();
        } catch (IOException e) {
            Logger.getLogger(ExternalDataWithTextCorpusStreamed.class.getName()).log(.nulle);
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ex) {
                    Logger.getLogger(ExternalDataWithTextCorpusStreamed.class.getName()).log(.nullex);
                }
            }
            cleanup();
            throw new WLFormatException(e);
        }
        return is;
    }
    private void initializeReaderAndWriter(
            InputStream inputStream,
            OutputStream outputStreamboolean outputAsXmlFragmentthrows WLFormatException {
        if (inputStream != null) {
            try {
                XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
                 = xmlInputFactory.createXMLEventReader(inputStream"UTF-8");
            } catch (XMLStreamException e) {
                throw new WLFormatException(e.getMessage(), e);
            }
        }
        if (outputStream != null) {
            try {
                XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
                 = new IndentingXMLEventWriter(xmlOutputFactory.createXMLEventWriter(outputStream"UTF-8"));
            } catch (XMLStreamException e) {
                throw new WLFormatException(e.getMessage(), e);
            }
        }
        .setOutputAsXmlFragment(outputAsXmlFragment);
    }
    private void readLayers() throws WLFormatException {
        if () {
            readExternalDataLayers();
        }
        if () {
            readTextCorpusLayers();
        }
    }
    private void readUpToExtDataOrTextCorpus() throws WLFormatException {
        XMLEvent peekedEvent;
        try {
            peekedEvent = .peek();
            while (! || !) {
                if (peekedEvent.getEventType() == .
                        && ((peekedEvent.asStartElement().getName().getLocalPart() == null ? . == null : peekedEvent.asStartElement().getName().getLocalPart().equals(.))
                        || (peekedEvent.asStartElement().getName().getLocalPart() == null ? . == null : peekedEvent.asStartElement().getName().getLocalPart().equals(.)))) {
                    if (peekedEvent.asStartElement().getName().getLocalPart() == null ? . == null : peekedEvent.asStartElement().getName().getLocalPart().equals(.)) {
                        this. = true;
                    } else if (peekedEvent.asStartElement().getName().getLocalPart() == null ? . == null : peekedEvent.asStartElement().getName().getLocalPart().equals(.)) {
                        this. = true;
                    }
                    return;
                } else {
                    XMLEvent readEvent = .readEvent();
                    .add(readEvent);
                    peekedEvent = .peek();
                }
            }
        } catch (XMLStreamException e) {
            throw new WLFormatException(e.getMessage(), e);
        }
    }
    private void readExternalDataLayers() throws WLFormatException {
        try {
            XMLEvent event = .nextEvent();
            .add(event);
            // read layers requested stopping before ExternalData end element
            ExternalDataLayerStored[] layers = new ExternalDataLayerStored[this..size()];
            int counter = 0;
            boolean extDataEnd = false;
            XMLEvent peekedEvent;
            peekedEvent = .peek();
            while (!extDataEnd && peekedEvent != null) {
                if (peekedEvent.getEventType() == .
                        && peekedEvent.asEndElement().getName().getLocalPart().equals(.)) {
                    extDataEnd = true;
                } else if (peekedEvent.getEventType() == .
                        && this..contains(ExternalDataLayerTag.getFromXmlName(peekedEvent.asStartElement().getName().getLocalPart()))) {
                    ExternalDataLayerStored layer = readExternalDataLayer();
                    layers[counter++] = layer;
                    peekedEvent = .peek();
                } else {
                    XMLEvent readEvent = .readEvent();
                    .add(readEvent);
                    peekedEvent = .peek();
                }
            }
            if (!extDataEnd) {
                throw new WLFormatException(. + " end tag not found");
            }
             = ExternalDataStored.compose(layers);
        } catch (XMLStreamException e) {
            throw new WLFormatException(e.getMessage(), e);
        } catch (NoSuchElementException e) {
            throw new WLFormatException(e.getMessage(), e);
        }
    }
        XMLEvent peekedEvent;
        ExternalDataLayerStored layer = null;
        try {
            peekedEvent = .peek();
            // now we assume that this event is start of a TextCorpus layer
            String tagName = peekedEvent.asStartElement().getName().getLocalPart();
            ExternalDataLayerTag layerTag = ExternalDataLayerTag.getFromXmlName(tagName);
            if (layerTag == null) { // unknown layer, just add it to output
                .readWriteElement(tagName);
            } else if (this..contains(layerTag)) { // known layer, and is requested for reading
                // add it to the output, but store its data
                layer = readExternalLayerData(layerTag);
            } else { // known layer, and is not requested for reading
                // just add it to the output
                .readWriteElement(tagName);
            }
        } catch (XMLStreamException e) {
            throw new WLFormatException(e.getMessage(), e);
        }
        return layer;
    }
        JAXBContext context;
        Unmarshaller unmarshaller;
        ExternalDataLayerStored layer;
        try {
            context = JAXBContext.newInstance(layerTag.getLayerClass());
            unmarshaller = context.createUnmarshaller();
            layer = (ExternalDataLayerStoredunmarshaller.unmarshal();
            // marshall it back to xml
            marshall(layer);
        } catch (JAXBException e) {
            throw new WLFormatException(e.getMessage(), e);
        }
        return layer;
    }
    private void readTextCorpusLayers() throws WLFormatException {
        try {
            // process TextCorpus start element
            XMLEvent event = .nextEvent();
            String lang = event.asStartElement().getAttributeByName(new QName("lang")).getValue();
            // add processed TextCorpus start back
            .add(event);
            // read layers requested stopping before ExternalData end element
            TextCorpusLayerStoredAbstract[] layers = new TextCorpusLayerStoredAbstract[this..size()];
            int counter = 0;
            boolean tcEnd = false;
            XMLEvent peekedEvent;
            peekedEvent = .peek();
            while (!tcEnd && peekedEvent != null) {
                if (peekedEvent.getEventType() == .
                        && peekedEvent.asEndElement().getName().getLocalPart().equals(.)) {
                    tcEnd = true;
                } else if (peekedEvent.getEventType() == .
                        && this..contains(TextCorpusLayerTag.getFromXmlName(peekedEvent.asStartElement().getName().getLocalPart()))) {
                    TextCorpusLayerStoredAbstract layer = readTextCorpusLayer();
                    layers[counter++] = layer;
                    peekedEvent = .peek();
                } else {
                    XMLEvent readEvent = .readEvent();
                    .add(readEvent);
                    peekedEvent = .peek();
                }
            }
            if (!tcEnd) {
                throw new WLFormatException(. + " end tag not found");
            }
            if (!filled(layers)) {
                throw new WLFormatException(. + " not all requested layers are found");
            }
             = TextCorpusStored.compose(langlayers);
        } catch (XMLStreamException e) {
            throw new WLFormatException(e.getMessage(), e);
        } catch (NoSuchElementException e) {
            throw new WLFormatException(e.getMessage(), e);
        }
    }
    private boolean filled(Object[] layers) {
        for (Object o : layers) {
            if (o == null) {
                return false;
            }
        }
        return true;
    }
        XMLEvent peekedEvent;
        TextCorpusLayerStoredAbstract layer = null;
        try {
            peekedEvent = .peek();
            // now we assume that this event is start of a TextCorpus layer
            String tagName = peekedEvent.asStartElement().getName().getLocalPart();
            TextCorpusLayerTag layerTag = TextCorpusLayerTag.getFromXmlName(tagName);
            if (layerTag == null) { // unknown layer, just add it to output
                .readWriteElement(tagName);
            } else if (this..contains(layerTag)) { // known layer, and is requested for reading
                // add it to the output, but store its data
                layer = readTextCorpusLayerData(layerTag);
            } else { // known layer, and is not requested for reading
                // just add it to the output
                .readWriteElement(tagName);
            }
        } catch (XMLStreamException e) {
            throw new WLFormatException(e.getMessage(), e);
        }
        return layer;
    }
        JAXBContext context;
        Unmarshaller unmarshaller;
        TextCorpusLayerStoredAbstract layer = null;
        try {
            context = JAXBContext.newInstance(layerTag.getLayerClass());
            unmarshaller = context.createUnmarshaller();
            layer = (TextCorpusLayerStoredAbstractunmarshaller.unmarshal();
            // marshall it back to xml
            marshall(layer);
        } catch (JAXBException e) {
            throw new WLFormatException(e.getMessage(), e);
        }
        return layer;
    }
    private void marshall(Object layerthrows WLFormatException {
        if ( == null) {
            return;
        }
        JAXBContext context;
        try {
            context = JAXBContext.newInstance(layer.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(.true);
            marshaller.setProperty(.true);
            marshaller.marshal(layer);
        } catch (Exception e) {
            Logger.getLogger(ExternalDataWithTextCorpusStreamed.class.getName()).log(.nulle);
            cleanup();
            throw new WLFormatException(e.getMessage(), e);
        }
    }

    
Closes the input and output streams associated with this object and releases any associated system resources. Before the streams are closed, all in-memory annotations of the ExternalDataWithTextCorpusStreamed and not-processed part of the input stream are written to the output stream. Therefore, it's important to call close() method, so that all the in-memory annotations are saved to the output stream. Once the ExternalDataWithTextCorpusStreamed has been closed, adding further annotations will have no effect on the output stream.

Throws:
WLFormatException if an error in input format or an I/O error occurs.
    public void close() throws WLFormatException {
        
        if () {
            return;
        }
         = true;
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent e;
        
        try {
            if (this. != null) { 
                // rewrite all before end of ExternalData or, if ExternalData is not present, before start of TextCorpus
                if (!this.) {
                    // up to TextCorpus start
                    .readWriteUpToStartElement(.);
                    // create the ExternalData start element
                    List<Attributeattrs = new ArrayList<Attribute>(0);
                    Namespace ns = eventFactory.createNamespace(.);
                    List<Namespacenss = new ArrayList<Namespace>();
                    nss.add(ns);
                    e = eventFactory.createStartElement("",
                            ..,
                            attrs.iterator(), nss.iterator());
                    .add(e);
                    e = eventFactory.createIgnorableSpace(.);
                    .add(e);
                } else {
                    .readWriteUpToEndElement(.);
                }
                // write new layers of ExternalData
                List<ExternalDataLayeredLayers = .getLayers();
                for (ExternalDataLayer layer : edLayers) {
                    if (!.contains(ExternalDataLayerTag.getFromClass(layer.getClass()))) {
                        marshall(layer);
                    }
                }
                if (!this.) {
                    // create the ExternalData end element
                    e = eventFactory.createEndElement("",
                            ..);
                    .add(e);
                    e = eventFactory.createIgnorableSpace(.);
                    .add(e);
                }
                if (!this.) {
                    // up to end D-Spin
                    .readWriteUpToStartElement(.);
                    // create the TextCorpus start element
                    List<Attributeattrs = new ArrayList<Attribute>(0);
                    Namespace ns = eventFactory.createNamespace(.);
                    List<Namespacenss = new ArrayList<Namespace>();
                    nss.add(ns);
                    e = eventFactory.createStartElement("",
                            ..,
                            attrs.iterator(), nss.iterator());
                    .add(e);
                    e = eventFactory.createIgnorableSpace(.);
                    .add(e);
                } else {
                    // rewrite all before end of TextCorpus
                    .readWriteUpToEndElement(.);
                }
                //  write new layers of TextCorpus:
                List<TextCorpusLayertcLayers = .getLayers();
                for (TextCorpusLayer layer : tcLayers) {
                    if (!.contains(TextCorpusLayerTag.getFromClass(layer.getClass()))) {
                        marshall(layer);
                    }
                }
                if (!this.) {
                    // create the TextCorpus end element
                    e = eventFactory.createEndElement("",
                            ..);
                    .add(e);
                    e = eventFactory.createIgnorableSpace(.);
                    .add(e);
                }
                // write to the end
                .readWriteToTheEnd();
            }
        } catch (XMLStreamException ex) {
            throw new WLFormatException(ex.getMessage(), ex);
        } finally {
            cleanup();
        }
    }
    @Override
    public String toString() {
        return this..toString() + "\n" + this..toString();
    }
    private void cleanup() {
        if ( != null) {
            try {
                .close();
            } catch (XMLStreamException ex) {
                Logger.getLogger(ExternalDataWithTextCorpusStreamed.class.getName()).log(.nullex);
            }
        }
        if ( != null) {
            try {
                .close();
            } catch (XMLStreamException ex) {
                Logger.getLogger(ExternalDataWithTextCorpusStreamed.class.getName()).log(.nullex);
            }
        }
        if ( != null) {
            try {
                .close();
            } catch (WLFormatException ex) {
                Logger.getLogger(ExternalDataWithTextCorpusStreamed.class.getName()).log(.nullex);
            }
        }
        if ( != null) {
             .delete();
        }
    }
    private void getLayersToReadWithDependencies(EnumSet<ExternalDataLayerTagedLayersToReadEnumSet<TextCorpusLayerTagtcLayersToRead) {
        
        if (edLayersToRead != null) {
            this. = EnumSet.copyOf(edLayersToRead);
        }
        if (tcLayersToRead != null) {
            this. = EnumSet.copyOf(tcLayersToRead);
            for (TextCorpusLayerTag tag : tcLayersToRead) {
                this..addAll(tag.withDependentLayers());
            }
        }
    }
New to GrepCode? Check out our FAQ X