Example usage for javax.xml.stream XMLStreamReader nextTag

List of usage examples for javax.xml.stream XMLStreamReader nextTag

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader nextTag.

Prototype

public int nextTag() throws XMLStreamException;

Source Link

Document

Skips any white space (isWhiteSpace() returns true), COMMENT, or PROCESSING_INSTRUCTION, until a START_ELEMENT or END_ELEMENT is reached.

Usage

From source file:org.deegree.style.se.parser.GraphicSymbologyParser.java

private Pair<Mark, Continuation<Mark>> parseMark(XMLStreamReader in) throws XMLStreamException {
    in.require(START_ELEMENT, null, "Mark");

    Mark base = new Mark();
    Continuation<Mark> contn = null;

    in.nextTag();

    while (!(in.isEndElement() && in.getLocalName().equals("Mark"))) {
        if (in.isEndElement()) {
            in.nextTag();//from w  w  w.j  av  a 2  s  .  c  o m
        }

        if (in.getLocalName().equals("WellKnownName")) {
            String wkn = in.getElementText();
            try {
                base.wellKnown = SimpleMark.valueOf(wkn.toUpperCase());
            } catch (IllegalArgumentException e) {
                LOG.warn("Specified unsupported WellKnownName of '{}', using square instead.", wkn);
                base.wellKnown = SimpleMark.SQUARE;
            }
        } else
            sym: if (in.getLocalName().equals("OnlineResource") || in.getLocalName().equals("InlineContent")) {
                LOG.debug("Loading mark from external file.");
                Triple<InputStream, String, Continuation<StringBuffer>> pair = getOnlineResourceOrInlineContent(
                        in);
                if (pair == null) {
                    in.nextTag();
                    break sym;
                }
                InputStream is = pair.first;
                in.nextTag();

                in.require(START_ELEMENT, null, "Format");
                String format = in.getElementText();
                in.require(END_ELEMENT, null, "Format");

                in.nextTag();
                if (in.getLocalName().equals("MarkIndex")) {
                    base.markIndex = Integer.parseInt(in.getElementText());
                }

                if (is != null) {
                    try {
                        java.awt.Font font = null;
                        if (format.equalsIgnoreCase("ttf")) {
                            font = createFont(TRUETYPE_FONT, is);
                        }
                        if (format.equalsIgnoreCase("type1")) {
                            font = createFont(TYPE1_FONT, is);
                        }

                        if (format.equalsIgnoreCase("svg")) {
                            base.shape = ShapeHelper.getShapeFromSvg(is, pair.second);
                        }

                        if (font == null && base.shape == null) {
                            LOG.warn("Mark was not loaded, because the format '{}' is not supported.", format);
                            break sym;
                        }

                        if (font != null && base.markIndex >= font.getNumGlyphs() - 1) {
                            LOG.warn("The font only contains {} glyphs, but the index given was {}.",
                                    font.getNumGlyphs(), base.markIndex);
                            break sym;
                        }

                        base.font = font;
                    } catch (FontFormatException e) {
                        LOG.debug("Stack trace:", e);
                        LOG.warn("The file was not a valid '{}' file: '{}'", format, e.getLocalizedMessage());
                    } catch (IOException e) {
                        LOG.debug("Stack trace:", e);
                        LOG.warn("The file could not be read: '{}'.", e.getLocalizedMessage());
                    } finally {
                        closeQuietly(is);
                    }
                }
            } else if (in.getLocalName().equals("Fill")) {
                final Pair<Fill, Continuation<Fill>> fill = context.fillParser.parseFill(in);
                base.fill = fill.first;
                if (fill.second != null) {
                    contn = new Continuation<Mark>(contn) {
                        @Override
                        public void updateStep(Mark base, Feature f, XPathEvaluator<Feature> evaluator) {
                            fill.second.evaluate(base.fill, f, evaluator);
                        }
                    };
                }
            } else if (in.getLocalName().equals("Stroke")) {
                final Pair<Stroke, Continuation<Stroke>> stroke = context.strokeParser.parseStroke(in);
                base.stroke = stroke.first;
                if (stroke.second != null) {
                    contn = new Continuation<Mark>(contn) {
                        @Override
                        public void updateStep(Mark base, Feature f, XPathEvaluator<Feature> evaluator) {
                            stroke.second.evaluate(base.stroke, f, evaluator);
                        }
                    };
                }
            } else if (in.isStartElement()) {
                Location loc = in.getLocation();
                LOG.error("Found unknown element '{}' at line {}, column {}, skipping.",
                        new Object[] { in.getLocalName(), loc.getLineNumber(), loc.getColumnNumber() });
                skipElement(in);
            }
    }

    in.require(END_ELEMENT, null, "Mark");

    return new Pair<Mark, Continuation<Mark>>(base, contn);
}

From source file:org.deegree.style.se.parser.GraphicSymbologyParser.java

private Triple<BufferedImage, String, Continuation<List<BufferedImage>>> parseExternalGraphic(
        final XMLStreamReader in) throws IOException, XMLStreamException {
    // TODO color replacement

    in.require(START_ELEMENT, null, "ExternalGraphic");

    String format = null;/* w  w w . ja va2  s  . c o m*/
    BufferedImage img = null;
    String url = null;
    Triple<InputStream, String, Continuation<StringBuffer>> pair = null;
    Continuation<List<BufferedImage>> contn = null; // needs to be list to be updateable by reference...

    while (!(in.isEndElement() && in.getLocalName().equals("ExternalGraphic"))) {
        in.nextTag();

        if (in.getLocalName().equals("Format")) {
            format = in.getElementText();
        } else if (in.getLocalName().equals("OnlineResource") || in.getLocalName().equals("InlineContent")) {
            pair = getOnlineResourceOrInlineContent(in);
        } else if (in.isStartElement()) {
            Location loc = in.getLocation();
            LOG.error("Found unknown element '{}' at line {}, column {}, skipping.",
                    new Object[] { in.getLocalName(), loc.getLineNumber(), loc.getColumnNumber() });
            skipElement(in);
        }
    }

    try {
        if (pair != null) {
            if (pair.first != null && format != null && (format.toLowerCase().indexOf("svg") == -1)) {
                img = ImageIO.read(pair.first);
            }
            url = pair.second;

            final Continuation<StringBuffer> sbcontn = pair.third;

            if (pair.third != null) {
                final LinkedHashMap<String, BufferedImage> cache = new LinkedHashMap<String, BufferedImage>(
                        256) {
                    private static final long serialVersionUID = -6847956873232942891L;

                    @Override
                    protected boolean removeEldestEntry(Map.Entry<String, BufferedImage> eldest) {
                        return size() > 256; // yeah, hardcoded max size... TODO
                    }
                };
                contn = new Continuation<List<BufferedImage>>() {
                    @Override
                    public void updateStep(List<BufferedImage> base, Feature f,
                            XPathEvaluator<Feature> evaluator) {
                        StringBuffer sb = new StringBuffer();
                        sbcontn.evaluate(sb, f, evaluator);
                        String file = sb.toString();
                        if (cache.containsKey(file)) {
                            base.add(cache.get(file));
                            return;
                        }
                        try {
                            BufferedImage i;
                            if (context.location != null) {
                                i = ImageIO.read(context.location.resolve(file));
                            } else {
                                i = ImageIO.read(resolve(file, in));
                            }
                            base.add(i);
                            cache.put(file, i);
                        } catch (MalformedURLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                };
            }
        }
    } finally {
        if (pair != null) {
            try {
                pair.first.close();
            } catch (Exception e) {
                LOG.trace("Stack trace when closing input stream:", e);
            }
        }
    }

    return new Triple<BufferedImage, String, Continuation<List<BufferedImage>>>(img, url, contn);
}

From source file:org.deegree.style.se.parser.GraphicSymbologyParser.java

private Triple<InputStream, String, Continuation<StringBuffer>> getOnlineResourceOrInlineContent(
        XMLStreamReader in) throws XMLStreamException {
    if (in.getLocalName().equals("OnlineResource")) {
        String str = in.getAttributeValue(XLNNS, "href");

        if (str == null) {
            Continuation<StringBuffer> contn = context.parser.updateOrContinue(in, "OnlineResource",
                    new StringBuffer(), SBUPDATER, null).second;
            return new Triple<InputStream, String, Continuation<StringBuffer>>(null, null, contn);
        }//from  ww  w . ja  v a  2s  . co m

        String strUrl = null;
        try {
            URL url;
            if (context.location != null) {
                url = context.location.resolveToUrl(str);
            } else {
                url = resolve(str, in);
            }
            strUrl = url.toExternalForm();
            LOG.debug("Loading from URL '{}'", url);
            in.nextTag();
            return new Triple<InputStream, String, Continuation<StringBuffer>>(url.openStream(), strUrl, null);
        } catch (IOException e) {
            LOG.debug("Stack trace:", e);
            LOG.warn("Could not retrieve content at URL '{}'.", str);
            return null;
        }
    } else if (in.getLocalName().equals("InlineContent")) {
        String format = in.getAttributeValue(null, "encoding");
        if (format.equalsIgnoreCase("base64")) {
            ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(in.getElementText()));
            return new Triple<InputStream, String, Continuation<StringBuffer>>(bis, null, null);
        }
        // if ( format.equalsIgnoreCase( "xml" ) ) {
        // // TODO
        // }
    } else if (in.isStartElement()) {
        Location loc = in.getLocation();
        LOG.error("Found unknown element '{}' at line {}, column {}, skipping.",
                new Object[] { in.getLocalName(), loc.getLineNumber(), loc.getColumnNumber() });
        skipElement(in);
    }

    return null;
}

From source file:org.eclipse.koneki.protocols.omadm.client.basic.DMBasicSession.java

private void readMessage(final InputStream in) throws XMLStreamException {
    final XMLStreamReader reader = this.dmClient.createXMLStreamReader(in, ENCODING, new StreamFilter() {

        @Override/*from w  w w.  ja  va 2s . com*/
        public boolean accept(final XMLStreamReader reader) {
            return !reader.isWhiteSpace() && !reader.isStandalone();
        }

    });

    jumpToStartTag(reader, "SyncHdr"); //$NON-NLS-1$

    readSyncHdr(reader);
    reader.nextTag();

    readSyncBody(reader);
    reader.nextTag();

    reader.close();
}

From source file:org.eclipse.koneki.protocols.omadm.client.basic.DMBasicSession.java

private void readSyncBody(final XMLStreamReader reader) throws XMLStreamException {
    boolean continueSyncBody = true;
    do {/*from  w  w w .  j  a  va2 s  .  c  o m*/

        int next = reader.nextTag();
        String name = reader.getLocalName();

        switch (next) {
        case XMLEvent.START_ELEMENT:
            switch (getKey(name)) {
            case STATUS:
                readStatus(reader);
                break;
            case ADD:
                readAdd(reader, new DMMeta());
                break;
            case COPY:
                readCopy(reader);
                break;
            case DELETE:
                readDelete(reader);
                break;
            case GET:
                readGet(reader);
                break;
            case REPLACE:
                readReplace(reader, new DMMeta());
                break;
            case EXEC:
                readExec(reader, new DMMeta());
                break;
            case SEQUENCE:
                readSequence(reader);
                break;
            case ATOMIC:
                readAtomic(reader);
                break;
            case FINAL:
                readFinal(reader);
                break;
            case ALERT:
                readAlert(reader);
                break;
            default:
                break;
            }
            break;
        case XMLEvent.END_ELEMENT:
            continueSyncBody = false;
            break;
        default:
            break;
        }
    } while (continueSyncBody);
}

From source file:org.eclipse.koneki.protocols.omadm.client.basic.DMBasicSession.java

private void readAlert(final XMLStreamReader reader) throws XMLStreamException {
    /*//from   w w w.  ja v  a  2  s.  co m
     * TODO : Manage Alert commands
     */
    reader.nextTag();
    // CmdID
    final String cmdID = reader.getElementText();
    reader.nextTag();

    this.statusManager.putStatus(this.currentServerMsgID, cmdID, "Alert", null, null, "406"); //$NON-NLS-1$ //$NON-NLS-2$

    jumpToEndTag(reader, "Alert"); //$NON-NLS-1$
}

From source file:org.eclipse.koneki.protocols.omadm.client.basic.DMBasicSession.java

private void readStatus(final XMLStreamReader reader) throws XMLStreamException {
    jumpToStartTag(reader, "CmdRef"); //$NON-NLS-1$

    // CmdRef/*w w  w .  j  a  v  a2  s  .  c om*/
    final String cmdRef = reader.getElementText();
    reader.nextTag();

    // Cmd
    final String cmd = reader.getElementText();
    jumpToStartTag(reader, "Data"); //$NON-NLS-1$

    // Data
    final int data = Integer.parseInt(reader.getElementText());
    jumpToEndTag(reader, "Status"); //$NON-NLS-1$

    // Performs the status
    if (cmd.equals("SyncHdr")) { //$NON-NLS-1$
        switch (data) {
        case 212:
            this.isClientAuthenticated = true;
            break;
        case 407:
            this.isClientAuthenticated = false;
            break;
        default:
            break;
        }
    }
    if (this.commandSends.containsKey(cmdRef)) {
        final Object[] objects = this.commandSends.get(cmdRef);
        if (cmd.equals("Alert")) { //$NON-NLS-1$
            for (final ProtocolListener messageListener : this.protocolLinsteners) {
                messageListener.clientAlert((String) objects[0], (String) objects[1], (DMItem[]) objects[2],
                        StatusCode.fromInt(data));
            }
        }
        this.commandSends.remove(cmdRef);
    }
}

From source file:org.eclipse.koneki.protocols.omadm.client.basic.DMBasicSession.java

private void readGet(final XMLStreamReader reader) throws XMLStreamException {
    reader.nextTag();

    // CmdID/*from   w ww  .  j av a2 s.  co m*/
    final String cmdID = reader.getElementText();
    reader.nextTag();

    // Meta?
    if (reader.getLocalName().equals("Meta")) { //$NON-NLS-1$
        jumpToEndTag(reader, "Meta"); //$NON-NLS-1$
        reader.nextTag();
    }

    // Item+
    boolean continueGet = true;
    do {
        switch (reader.getEventType()) {
        case XMLStreamReader.START_ELEMENT:
            // Performs the get command
            final DMItem item = readItem(reader, new DMMeta());
            reader.nextTag();
            final Status status = this.commandHandler.get(item.getTargetURI());
            final DMNode results = status.getResult();
            if (results != null) {
                this.statusManager.putStatus(this.currentServerMsgID, cmdID, "Get", item.getTargetURI(), null, //$NON-NLS-1$
                        String.valueOf(status.getCode()), results.getFormat(), results.getType(), results.getData());
            } else {
                this.statusManager.putStatus(this.currentServerMsgID, cmdID, "Get", item.getTargetURI(), null, //$NON-NLS-1$
                        String.valueOf(status.getCode()));
            }

            // Fire get event
            for (final ProtocolListener messageListener : this.protocolLinsteners) {
                messageListener.get(item.getTargetURI(), status);
            }
            break;
        case XMLStreamReader.END_ELEMENT:
            continueGet = false;
            break;
        default:
            break;
        }
    } while (continueGet);
}

From source file:org.eclipse.koneki.protocols.omadm.client.basic.DMBasicSession.java

private void readAdd(final XMLStreamReader reader, final DMMeta parentMeta) throws XMLStreamException {
    reader.nextTag();

    // CmdID//from  w  ww  . j av  a2 s  .c o  m
    final String cmdID = reader.getElementText();
    reader.nextTag();

    // Meta?
    final DMMeta globalMeta = new DMMeta(parentMeta);
    if (reader.getLocalName().equals("Meta")) { //$NON-NLS-1$
        globalMeta.putAll(readMeta(reader));
        reader.nextTag();
    }

    // Item+
    boolean continueAdd = true;
    do {
        switch (reader.getEventType()) {
        case XMLEvent.START_ELEMENT:
            // Performs the add command
            final DMItem item = readItem(reader, globalMeta);
            reader.nextTag();
            final Status status = this.commandHandler.add(item.getTargetURI(), item.getMeta().getFormat(),
                    item.getMeta().getType(), item.getData());
            this.statusManager.putStatus(this.currentServerMsgID, cmdID, "Add", item.getTargetURI(), null, //$NON-NLS-1$
                    String.valueOf(status.getCode()));

            // Fire add event
            for (final ProtocolListener messageListener : this.protocolLinsteners) {
                messageListener.add(item.getTargetURI(), item.getData(), status);
            }
            break;
        case XMLEvent.END_ELEMENT:
            continueAdd = false;
            break;
        default:
            break;
        }
    } while (continueAdd);
}

From source file:org.eclipse.koneki.protocols.omadm.client.basic.DMBasicSession.java

private void readDelete(final XMLStreamReader reader) throws XMLStreamException {
    reader.nextTag();

    // CmdID/*from w  w  w  . j  a v  a  2 s .  com*/
    final String cmdID = reader.getElementText();
    reader.nextTag();

    // Meta?
    if (reader.getLocalName().equals("Meta")) { //$NON-NLS-1$
        jumpToEndTag(reader, "Meta"); //$NON-NLS-1$
        reader.nextTag();
    }

    // Item+
    boolean continueDelete = true;
    do {
        switch (reader.getEventType()) {
        case XMLEvent.START_ELEMENT:
            final DMItem item = readItem(reader, new DMMeta());
            reader.nextTag();
            final Status status = this.commandHandler.delete(item.getTargetURI());
            this.statusManager.putStatus(this.currentServerMsgID, cmdID, "Delete", item.getTargetURI(), null, //$NON-NLS-1$
                    String.valueOf(status.getCode()));

            // Fire delete event
            for (final ProtocolListener messageListener : this.protocolLinsteners) {
                messageListener.delete(item.getTargetURI(), status);
            }
            break;
        case XMLEvent.END_ELEMENT:
            continueDelete = false;
            break;
        default:
            break;
        }
    } while (continueDelete);
}