Search in sources :

Example 31 with Characters

use of javax.xml.stream.events.Characters in project omegat by omegat-org.

the class TMXReader2 method parseSegExtLevel2.

/**
 * External TMX - level 2. Replace all tags into shortcuts.
 */
protected void parseSegExtLevel2() throws Exception {
    segContent.setLength(0);
    segInlineTag.setLength(0);
    inlineTagHandler.reset();
    int inlineLevel = 0;
    while (true) {
        XMLEvent e = xml.nextEvent();
        switch(e.getEventType()) {
            case XMLEvent.START_ELEMENT:
                StartElement eStart = e.asStartElement();
                if ("hi".equals(eStart.getName().getLocalPart())) {
                    // tag should be skipped
                    break;
                }
                inlineLevel++;
                segInlineTag.setLength(0);
                if ("bpt".equals(eStart.getName().getLocalPart())) {
                    inlineTagHandler.startBPT(getAttributeValue(eStart, "i"), getAttributeValue(eStart, "x"));
                    inlineTagHandler.setTagShortcutLetter(StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
                } else if ("ept".equals(eStart.getName().getLocalPart())) {
                    inlineTagHandler.startEPT(getAttributeValue(eStart, "i"));
                } else if ("it".equals(eStart.getName().getLocalPart())) {
                    inlineTagHandler.startOTHER();
                    inlineTagHandler.setOtherTagShortcutLetter(StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
                    inlineTagHandler.setCurrentPos(getAttributeValue(eStart, "pos"));
                } else if ("ph".equals(eStart.getName().getLocalPart())) {
                    inlineTagHandler.startOTHER();
                    inlineTagHandler.setOtherTagShortcutLetter(StringUtil.getFirstLetterLowercase(getAttributeValue(eStart, "type")));
                } else {
                    inlineTagHandler.startOTHER();
                }
                break;
            case XMLEvent.END_ELEMENT:
                EndElement eEnd = e.asEndElement();
                if ("hi".equals(eEnd.getName().getLocalPart())) {
                    // tag should be skipped
                    break;
                }
                inlineLevel--;
                if ("seg".equals(eEnd.getName().getLocalPart())) {
                    return;
                }
                boolean slashBefore = false;
                boolean slashAfter = false;
                int tagName = StringUtil.getFirstLetterLowercase(segInlineTag.toString());
                Integer tagN;
                if ("bpt".equals(eEnd.getName().getLocalPart())) {
                    if (tagName != 0) {
                        inlineTagHandler.setTagShortcutLetter(tagName);
                    } else {
                        tagName = inlineTagHandler.getTagShortcutLetter();
                    }
                    tagN = inlineTagHandler.endBPT();
                } else if ("ept".equals(eEnd.getName().getLocalPart())) {
                    slashBefore = true;
                    tagName = inlineTagHandler.getTagShortcutLetter();
                    tagN = inlineTagHandler.endEPT();
                } else if ("it".equals(eEnd.getName().getLocalPart())) {
                    if (tagName != 0) {
                        inlineTagHandler.setOtherTagShortcutLetter(tagName);
                    } else {
                        tagName = inlineTagHandler.getOtherTagShortcutLetter();
                    }
                    tagN = inlineTagHandler.endOTHER();
                    if ("end".equals(inlineTagHandler.getCurrentPos())) {
                        slashBefore = true;
                    }
                } else if ("ph".equals(eEnd.getName().getLocalPart())) {
                    if (tagName != 0) {
                        inlineTagHandler.setOtherTagShortcutLetter(tagName);
                    } else {
                        tagName = inlineTagHandler.getOtherTagShortcutLetter();
                    }
                    tagN = inlineTagHandler.endOTHER();
                    if (useSlash) {
                        slashAfter = true;
                    }
                } else {
                    tagN = inlineTagHandler.endOTHER();
                    if (useSlash) {
                        slashAfter = true;
                    }
                }
                if (tagName == 0) {
                    tagName = 'f';
                }
                if (tagN == null) {
                    // check error of TMX reading
                    Log.logErrorRB("TMX_ERROR_READING_LEVEL2", e.getLocation().getLineNumber(), e.getLocation().getColumnNumber());
                    errorsCount++;
                    segContent.setLength(0);
                    // wait for end seg
                    while (true) {
                        XMLEvent ev = xml.nextEvent();
                        switch(ev.getEventType()) {
                            case XMLEvent.END_ELEMENT:
                                EndElement evEnd = (EndElement) ev;
                                if ("seg".equals(evEnd.getName().getLocalPart())) {
                                    return;
                                }
                        }
                    }
                }
                segContent.append('<');
                if (slashBefore) {
                    segContent.append('/');
                }
                segContent.appendCodePoint(tagName);
                segContent.append(Integer.toString(tagN));
                if (slashAfter) {
                    segContent.append('/');
                }
                segContent.append('>');
                break;
            case XMLEvent.CHARACTERS:
                Characters c = (Characters) e;
                if (inlineLevel == 0) {
                    segContent.append(c.getData());
                } else {
                    segInlineTag.append(c.getData());
                }
                break;
        }
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) EndElement(javax.xml.stream.events.EndElement) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 32 with Characters

use of javax.xml.stream.events.Characters in project hibernate-orm by hibernate.

the class BaseXMLEventReader method getElementText.

/* (non-Javadoc)
	 * @see javax.xml.stream.XMLEventReader#getElementText()
	 */
@Override
public final String getElementText() throws XMLStreamException {
    XMLEvent event = this.previousEvent;
    if (event == null) {
        throw new XMLStreamException("Must be on START_ELEMENT to read next text, element was null");
    }
    if (!event.isStartElement()) {
        throw new XMLStreamException("Must be on START_ELEMENT to read next text", event.getLocation());
    }
    final StringBuilder text = new StringBuilder();
    while (!event.isEndDocument()) {
        switch(event.getEventType()) {
            case XMLStreamConstants.CHARACTERS:
            case XMLStreamConstants.SPACE:
            case XMLStreamConstants.CDATA:
                {
                    final Characters characters = event.asCharacters();
                    text.append(characters.getData());
                    break;
                }
            case XMLStreamConstants.ENTITY_REFERENCE:
                {
                    final EntityReference entityReference = (EntityReference) event;
                    final EntityDeclaration declaration = entityReference.getDeclaration();
                    text.append(declaration.getReplacementText());
                    break;
                }
            case XMLStreamConstants.COMMENT:
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                {
                    // Ignore
                    break;
                }
            default:
                {
                    throw new XMLStreamException("Unexpected event type '" + XMLStreamConstantsUtils.getEventName(event.getEventType()) + "' encountered. Found event: " + event, event.getLocation());
                }
        }
        event = this.nextEvent();
    }
    return text.toString();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) EntityDeclaration(javax.xml.stream.events.EntityDeclaration) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent) EntityReference(javax.xml.stream.events.EntityReference)

Example 33 with Characters

use of javax.xml.stream.events.Characters in project spring-framework by spring-projects.

the class ListBasedXMLEventReader method getElementText.

@Override
public String getElementText() throws XMLStreamException {
    checkIfClosed();
    if (this.currentEvent == null || !this.currentEvent.isStartElement()) {
        throw new XMLStreamException("Not at START_ELEMENT: " + this.currentEvent);
    }
    StringBuilder builder = new StringBuilder();
    while (true) {
        XMLEvent event = nextEvent();
        if (event.isEndElement()) {
            break;
        } else if (!event.isCharacters()) {
            throw new XMLStreamException("Unexpected non-text event: " + event);
        }
        Characters characters = event.asCharacters();
        if (!characters.isIgnorableWhiteSpace()) {
            builder.append(event.asCharacters().getData());
        }
    }
    return builder.toString();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent)

Aggregations

Characters (javax.xml.stream.events.Characters)33 XMLEvent (javax.xml.stream.events.XMLEvent)21 XMLStreamException (javax.xml.stream.XMLStreamException)20 EndElement (javax.xml.stream.events.EndElement)11 StartElement (javax.xml.stream.events.StartElement)11 XMLEventReader (javax.xml.stream.XMLEventReader)6 QName (javax.xml.namespace.QName)5 XMLInputFactory (javax.xml.stream.XMLInputFactory)5 Test (org.junit.Test)5 Attribute (javax.xml.stream.events.Attribute)4 EntityDeclaration (javax.xml.stream.events.EntityDeclaration)4 EntityReference (javax.xml.stream.events.EntityReference)4 HashMap (java.util.HashMap)3 Namespace (javax.xml.stream.events.Namespace)3 StartDocument (javax.xml.stream.events.StartDocument)3 InputStream (java.io.InputStream)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 Base64 (java.util.Base64)2 ProcessingInstruction (javax.xml.stream.events.ProcessingInstruction)2