Example #1
0
  /**
   * Skips the remainder of a comment. It is assumed that <!- is already read.
   *
   * @param reader the reader
   * @throws java.io.IOException if an error occurred reading the data
   */
  static void skipComment(IXMLReader reader) throws IOException, XMLParseException {
    if (reader.read() != '-') {
      XMLUtil.errorExpectedInput(reader.getSystemID(), reader.getLineNr(), "<!--");
    }

    int dashesRead = 0;

    for (; ; ) {
      char ch = reader.read();

      switch (ch) {
        case '-':
          dashesRead++;
          break;

        case '>':
          if (dashesRead == 2) {
            return;
          }
          dashesRead = 0;
          break;
        default:
          dashesRead = 0;
      }
    }
  }
Example #2
0
  /**
   * Skips whitespace from the reader.
   *
   * @param reader the reader
   * @param buffer where to put the whitespace; null if the whitespace does not have to be stored.
   * @throws java.io.IOException if an error occurred reading the data
   */
  static void skipWhitespace(IXMLReader reader, StringBuffer buffer) throws IOException {
    char ch;

    if (buffer == null) {
      do {
        ch = reader.read();
      } while ((ch == ' ') || (ch == '\t') || (ch == '\n'));
    } else {
      for (; ; ) {
        ch = reader.read();

        if ((ch != ' ') && (ch != '\t') && (ch != '\n')) {
          break;
        }

        if (ch == '\n') {
          buffer.append('\n');
        } else {
          buffer.append(' ');
        }
      }
    }

    reader.unread(ch);
  }
Example #3
0
  /**
   * Reads a character from the reader disallowing entities.
   *
   * @param reader the reader
   * @param entityChar the escape character (&amp; or %) used to indicate an entity
   */
  static char readChar(IXMLReader reader, char entityChar) throws IOException, XMLParseException {
    String str = XMLUtil.read(reader, entityChar);
    char ch = str.charAt(0);

    if (ch == entityChar) {
      XMLUtil.errorUnexpectedEntity(reader.getSystemID(), reader.getLineNr(), str);
    }

    return ch;
  }
Example #4
0
  /**
   * Processes an entity.
   *
   * @param entity the entity
   * @param reader the reader
   * @param entityResolver the entity resolver
   * @throws java.io.IOException if an error occurred reading the data
   */
  static void processEntity(String entity, IXMLReader reader, IXMLEntityResolver entityResolver)
      throws IOException, XMLParseException {
    entity = entity.substring(1, entity.length() - 1);
    Reader entityReader = entityResolver.getEntity(reader, entity);

    if (entityReader == null) {
      XMLUtil.errorInvalidEntity(reader.getSystemID(), reader.getLineNr(), entity);
    }

    boolean externalEntity = entityResolver.isExternalEntity(entity);
    reader.startNewStream(entityReader, !externalEntity);
  }
Example #5
0
  /**
   * Reads a character from the reader.
   *
   * @param reader the reader
   * @param entityChar the escape character (&amp; or %) used to indicate an entity
   * @return the character, or an entity expression (like e.g. &amp;lt;)
   * @throws java.io.IOException if an error occurred reading the data
   */
  static String read(IXMLReader reader, char entityChar) throws IOException, XMLParseException {
    char ch = reader.read();
    StringBuffer buf = new StringBuffer();
    buf.append(ch);

    if (ch == entityChar) {
      while (ch != ';') {
        ch = reader.read();
        buf.append(ch);
      }
    }

    return buf.toString();
  }
Example #6
0
  /**
   * Returns true if the data starts with <I>literal</I>. Enough chars are read to determine this
   * result.
   *
   * @param reader the reader
   * @param literal the literal to check
   * @throws java.io.IOException if an error occurred reading the data
   */
  static boolean checkLiteral(IXMLReader reader, String literal)
      throws IOException, XMLParseException {
    for (int i = 0; i < literal.length(); i++) {
      if (reader.read() != literal.charAt(i)) {
        return false;
      }
    }

    return true;
  }
Example #7
0
  /**
   * Retrieves an identifier from the data.
   *
   * @param reader the reader
   * @throws java.io.IOException if an error occurred reading the data
   */
  static String scanIdentifier(IXMLReader reader) throws IOException, XMLParseException {
    StringBuffer result = new StringBuffer();

    for (; ; ) {
      char ch = reader.read();

      if ((ch == '_')
          || (ch == ':')
          || (ch == '-')
          || (ch == '.')
          || ((ch >= 'a') && (ch <= 'z'))
          || ((ch >= 'A') && (ch <= 'Z'))
          || ((ch >= '0') && (ch <= '9'))
          || (ch > '\u007E')) {
        result.append(ch);
      } else {
        reader.unread(ch);
        break;
      }
    }

    return result.toString();
  }
Example #8
0
  /**
   * Skips the remainder of the current XML tag.
   *
   * @param reader the reader
   * @throws java.io.IOException if an error occurred reading the data
   */
  static void skipTag(IXMLReader reader) throws IOException, XMLParseException {
    int level = 1;

    while (level > 0) {
      char ch = reader.read();

      switch (ch) {
        case '<':
          ++level;
          break;

        case '>':
          --level;
          break;
      }
    }
  }
Example #9
0
  /**
   * Retrieves a delimited string from the data.
   *
   * @param reader the reader
   * @param entityChar the escape character (&amp; or %)
   * @param entityResolver the entity resolver
   * @throws java.io.IOException if an error occurred reading the data
   */
  static String scanString(IXMLReader reader, char entityChar, IXMLEntityResolver entityResolver)
      throws IOException, XMLParseException {
    StringBuffer result = new StringBuffer();
    int startingLevel = reader.getStreamLevel();
    char delim = reader.read();

    if ((delim != '\'') && (delim != '"')) {
      XMLUtil.errorExpectedInput(reader.getSystemID(), reader.getLineNr(), "delimited string");
    }

    for (; ; ) {
      String str = XMLUtil.read(reader, entityChar);
      char ch = str.charAt(0);

      if (ch == entityChar) {
        if (str.charAt(1) == '#') {
          result.append(XMLUtil.processCharLiteral(str));
        } else {
          XMLUtil.processEntity(str, reader, entityResolver);
        }
      } else if (ch == '&') {
        reader.unread(ch);
        str = XMLUtil.read(reader, '&');
        if (str.charAt(1) == '#') {
          result.append(XMLUtil.processCharLiteral(str));
        } else {
          result.append(str);
        }
      } else if (reader.getStreamLevel() == startingLevel) {
        if (ch == delim) {
          break;
        } else if ((ch == 9) || (ch == 10) || (ch == 13)) {
          result.append(' ');
        } else {
          result.append(ch);
        }
      } else {
        result.append(ch);
      }
    }

    return result.toString();
  }
 /**
  * Creates a new reader using a file as input.
  *
  * @param filename the name of the file containing the XML data
  * @throws java.io.FileNotFoundException if the file could not be found
  * @throws java.io.IOException if an I/O error occurred
  */
 public static IXMLReader fileReader(String filename) throws FileNotFoundException, IOException {
   IXMLReader reader = new StdXMLReader(new FileInputStream(filename));
   reader.setSystemID(filename);
   return reader;
 }