OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.utils.doml;

import java.io.BufferedReader;
import java.io.IOException;

public class DOMLStreamReader {
    public static final int EOF = 0;
    public static final int TAG_START = 1;
    public static final int TAG_END = 2;
    public static final int TAG_END_COMPACT = 3;
    public static final int NO_TAG_CHARACTER = 4;
    private final PushBackReader br;
    final StringBuilder bElementName = new StringBuilder(2048);

    public DOMLStreamReader(BufferedReader r) {
        this.br = new PushBackReader(r, 2);
    }

    public final int readNextToken() throws IOException {
        char c = (char) this.br.read();
        while (c > 0) {
            if (c == ' ' || c == '\t' || c == '\n') {
                c = (char) this.br.read();
            } else {
                if (c == '<') {
                    return TAG_START;
                } else if (c == '>') {
                    return TAG_END;
                } else if (c == '/') {
                    c = (char) this.br.read();
                    if (c == '>') {
                        return TAG_END_COMPACT;
                    } else {
                        unread(c);
                    }
                } else {
                    this.br.unread(c);
                    return NO_TAG_CHARACTER;
                }
            }
        }
        return EOF;
    }

    public final String readTagName() throws IOException {
        this.bElementName.setLength(0);
        char c = (char) this.br.read();
        while (c > 0) {
            if (c == '>') {
                this.br.unread('>');
                return this.bElementName.toString();
            } else if (c == ' ' || c == '\t') {
                return this.bElementName.toString();
            } else {
                this.bElementName.append(c);
            }
            c = (char) this.br.read();
        }

        return this.bElementName.toString();
    }

    public final void unread(char c) throws IOException {
        this.br.unread(c);
    }

    public final char readNextChar() throws IOException {
        return (char) this.br.read();
    }

    public final String readAttributeName() throws IOException {
        this.bElementName.setLength(0);
        char c = (char) this.br.read();
        while (c > 0) {
            if (c == '>') {
                this.br.unread('>');
                return this.bElementName.toString();
            } else if (c == '=' || c == ' ' || c == '\t' || c == '\n') {
                return this.bElementName.toString();
            } else {
                this.bElementName.append(c);
            }
            c = (char) this.br.read();
        }

        return this.bElementName.toString();
    }

    public final String readAttributeValue() throws IOException {
        this.bElementName.setLength(0);
        boolean open = false;
        char c = (char) this.br.read();
        if (c != '\"') {
            throw new IllegalStateException("missing \"");
        }
        while (c > 0) {
            if (c == '\\') {
                c = (char) this.br.read();
                if (c == '\\' || c == '"') {
                    this.bElementName.append(c);
                } else if (c == 'r') {
                    this.bElementName.append('\r');
                } else if (c == 't') {
                    this.bElementName.append('\t');
                } else if (c == 'n') {
                    this.bElementName.append('\n');
                } else {
                    throw new IllegalStateException("invalid escaped character :" + c);
                }
            } else if (c == '"') {
                if (open) {
                    return this.bElementName.toString();
                }
                open = true;
            } else if (c < 32) {
                continue;
            } else {
                this.bElementName.append(c);
            }
            c = (char) this.br.read();
        }
        return this.bElementName.toString();
    }

}