Пример #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;
      }
    }
  }
Пример #2
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;
  }
Пример #3
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);
  }
Пример #4
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();
  }