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;
 
Class XmlReaderWriter helps to work with TCF XML underlying streams. Should not be used directly.

Author(s):
Yana Panchenko
 
 public class XmlReaderWriter {
 
     public static final String XML_WL1_MODEL_PI_NAME = "xml-model";
     public static final String NEW_LINE = System.getProperty("line.separator");
     private static final String INDENT = "  ";
     private int indent = -1;
     private boolean charsWritten = false;
     private boolean startWritten = false;
     private boolean ispaceWritten = true;
     private final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
     private XMLEventReader xmlEventReader;
     private XMLEventWriter xmlEventWriter;
     private boolean fragment = false;
 
     public XmlReaderWriter(XMLEventReader xmlEventReaderXMLEventWriter xmlEventWriter) {
         this. = xmlEventReader;
         this. = xmlEventWriter;
     }
 
     void setOutputAsXmlFragment(boolean outputAsXmlFragment) {
         this. = outputAsXmlFragment;
     }
 
     XMLEvent readEvent() throws WLFormatException {
         try {
             return .nextEvent();
         } catch (XMLStreamException e) {
             throw new WLFormatException(e.getMessage(), e);
         }
     }
 
     void startExternalFragment(int incIndentthrows XMLStreamException {
         if (!) {
             XMLEvent e = .createIgnorableSpace();
             .add(e);
              = true;
         }
         for (int i = 0; i < incIndenti++) {
             incIndent();
         }
         addIndent();
     }
 
     void endExternalFragment(int decIndentthrows XMLStreamException {
         .add(e);
          = true;
         for (int i = 0; i < decIndenti++) {
             decIndent();
         }
     }
 
     void add(XMLEvent xmlEventthrows XMLStreamException {
 
         if ( == null) {
             return;
         }
         XMLEvent e;
         switch (xmlEvent.getEventType()) {
             case .:
                 if (!) {
                     e = .createIgnorableSpace();
                     .add(e);
                 }
                 incIndent();
                 addIndent();
                 .add(xmlEvent);
                 = false;
                 = true;
                 = false;
                break;
            case .:
                if (! && !) {
                    if (!) {
                        e = .createIgnorableSpace();
                        .add(e);
                    }
                    addIndent();
                }
                decIndent();
                .add(xmlEvent);
                 = false;
                 = false;
                 = false;
                break;
            case .//TODO check on big data
                //go back to previous version if doesn't work on big data:
                // xmlEventWriter.add(eventFactory.createCharacters(characters.getData()));
                Characters characters = xmlEvent.asCharacters();
                if (characters.isWhiteSpace()) {
                    if (!) {
                        e = .createIgnorableSpace();
                        .add(e);
                         = true;
                         = false;
                    }
                } else {
                    .add(xmlEvent);
                     = false;
                     = true;
                }
                 = false;
                break;
            case .:
                if (!) {
                    e = .createIgnorableSpace();
                    .add(e);
                }
                incIndent();
                addIndent();
                .add(xmlEvent);
                decIndent();
                 = false;
                 = false;
                 = false;
                break;
            case .:
                if (!) {
                    .add(xmlEvent);
                }
                 = false;
                break;
            case .:
                if (!) {
                    if (!) {
                        e = .createIgnorableSpace();
                        .add(e);
                    }
                    .add(xmlEvent);
                }
                 = false;
                break;
            default:
                .add(xmlEvent);
                 = false;
                 = false;
                 = false;
                break;
        }
    }
    void incIndent() {
        ++;
    }
    void decIndent() {
        --;
    }
    void addIndent() throws XMLStreamException {
        XMLEvent xmlEvent;
        for (int i = 0; i < i++) {
            xmlEvent = .createIgnorableSpace();
            .add(xmlEvent);
        }
    }
    void close() throws WLFormatException {
        try {
            if ( != null) {
                .close();
            }
        } catch (XMLStreamException e) {
            try {
                if ( != null) {
                    .close();
                }
            } catch (XMLStreamException e2) {
                throw new WLFormatException(e2);
            }
            throw new WLFormatException(e);
        }
    }
    public void readWriteToTheEnd() throws WLFormatException {
        try {
            while (.hasNext()) {
                add(.nextEvent());
            }
            if ( != null) {
                .flush();
            }
        } catch (XMLStreamException e) {
            throw new WLFormatException(e.getMessage(), e);
        } finally {
            close();
        }
    }
    // precondition read pointer is just before the start tag with the local name tagName
    // postcondition read pointer is just after the end of the tag with the local name tagName
    void readWriteElement(String tagNamethrows WLFormatException {
        try {
            while (.hasNext()) {
                XMLEvent event = .nextEvent();
                switch (event.getEventType()) {
                    case .:
                        add(event);
                        if (event.asEndElement().getName().getLocalPart().equals(tagName)) {
                            return;
                        }
                        break;
                    default:
                        add(event);
                        break;
                }
            }
        } catch (XMLStreamException e) {
            throw new WLFormatException(e.getMessage(), e);
        }
    }
    // precondition read pointer is at any place before start tag with the local name startTag
    // postcondition read pointer is just before the start of the tag with the local name startTag
    public void readWriteUpToStartElement(String startTagthrows XMLStreamException {
        boolean startTagIsNext = false;
        while (.hasNext() && !startTagIsNext) {
            XMLEvent peekedEvent = .peek();
            switch (peekedEvent.getEventType()) {
                case .:
                    String elementName = peekedEvent.asStartElement().getName().getLocalPart();
                    if (elementName.equals(startTag)) {
                        startTagIsNext = true;
                    } else {
                        XMLEvent readEvent = .nextEvent();
                        add(readEvent);
                    }
                    break;
                default:
                    XMLEvent readEvent = .nextEvent();
                    add(readEvent);
                    break;
            }
        }
    }
    public void readWriteUpToEndElement(String endTagthrows XMLStreamException {
        boolean endTagIsNext = false;
        while (.hasNext() && !endTagIsNext) {
            XMLEvent peekedEvent = .peek();
            switch (peekedEvent.getEventType()) {
                case .:
                    String elementName = peekedEvent.asEndElement().getName().getLocalPart();
                    if (elementName.equals(endTag)) {
                        endTagIsNext = true;
                    } else {
                        XMLEvent readEvent = .nextEvent();
                        add(readEvent);
                    }
                    break;
                default:
                    XMLEvent readEvent = .nextEvent();
                    add(readEvent);
                    break;
            }
        }
    }
New to GrepCode? Check out our FAQ X