xmlutil.java

来自「Nano的XML解析器」· Java 代码 · 共 764 行 · 第 1/2 页

JAVA
764
字号
   /**    * 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;   }   /**    * 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;   }      /**    * Throws an XMLParseException to indicate that an expected string is not    * encountered.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param expectedString the string that is expected    */   static void errorExpectedInput(String systemID,                                  int    lineNr,                                  String expectedString)      throws XMLParseException   {      throw new XMLParseException(systemID, lineNr,                                  "Expected: " + expectedString);   }   /**    * Throws an XMLParseException to indicate that an entity could not be    * resolved.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param entity    the name of the entity    */   static void errorInvalidEntity(String systemID,                                  int    lineNr,                                  String     entity)      throws XMLParseException   {      throw new XMLParseException(systemID, lineNr,                                  "Invalid entity: `&" + entity + ";'");   }   /**    * Throws an XMLParseException to indicate that an entity reference is    * unexpected at this point.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param entity    the name of the entity    */   static void errorUnexpectedEntity(String systemID,                                     int    lineNr,                                     String entity)      throws XMLParseException   {      throw new XMLParseException(systemID, lineNr,                                  "No entity reference is expected here ("                                  + entity + ")");   }   /**    * Throws an XMLParseException to indicate that a CDATA section is    * unexpected at this point.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    */   static void errorUnexpectedCDATA(String systemID,                                    int    lineNr)      throws XMLParseException   {      throw new XMLParseException(systemID, lineNr,                                  "No CDATA section is expected here");   }   /**    * Throws an XMLParseException to indicate that a string is not expected    * at this point.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param unexpectedString the string that is unexpected    */   static void errorInvalidInput(String systemID,                                 int    lineNr,                                 String     unexpectedString)      throws XMLParseException   {      throw new XMLParseException(systemID, lineNr,                                  "Invalid input: " + unexpectedString);   }   /**    * Throws an XMLParseException to indicate that the closing tag of an    * element does not match the opening tag.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param expectedName the name of the opening tag    * @param wrongName    the name of the closing tag    */   static void errorWrongClosingTag(String systemID,                                    int    lineNr,                                    String     expectedName,                                    String     wrongName)      throws XMLParseException   {      throw new XMLParseException(systemID, lineNr,                                  "Closing tag does not match opening tag: `"                                  + wrongName + "' != `" + expectedName                                  + "'");   }   /**    * Throws an XMLParseException to indicate that extra data is encountered    * in a closing tag.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    */   static void errorClosingTagNotEmpty(String systemID,                                       int    lineNr)      throws XMLParseException   {      throw new XMLParseException(systemID, lineNr,                                  "Closing tag must be empty");   }   /**    * Throws an XMLValidationException to indicate that an element is missing.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param parentElementName the name of the parent element    * @param missingElementName the name of the missing element    */   static void errorMissingElement(String systemID,                                   int    lineNr,                                   String parentElementName,                                   String missingElementName)      throws XMLValidationException   {      throw new XMLValidationException(                              XMLValidationException.MISSING_ELEMENT,                              systemID, lineNr,                              missingElementName,                              /*attributeName*/ null,                              /*attributeValue*/ null,                              "Element " + parentElementName                              + " expects to have a " + missingElementName);   }   /**    * Throws an XMLValidationException to indicate that an element is    * unexpected.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param parentElementName the name of the parent element    * @param unexpectedElementName the name of the unexpected element    */   static void errorUnexpectedElement(String systemID,                                      int    lineNr,                                      String parentElementName,                                      String unexpectedElementName)      throws XMLValidationException   {      throw new XMLValidationException(                              XMLValidationException.UNEXPECTED_ELEMENT,                              systemID, lineNr,                              unexpectedElementName,                              /*attributeName*/ null,                              /*attributeValue*/ null,                              "Unexpected " + unexpectedElementName + " in a "                              + parentElementName);   }   /**    * Throws an XMLValidationException to indicate that an attribute is    * missing.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param elementName    the name of the element    * @param attributeName  the name of the missing attribute    */   static void errorMissingAttribute(String systemID,                                     int    lineNr,                                     String elementName,                                     String attributeName)      throws XMLValidationException   {      throw new XMLValidationException(                     XMLValidationException.MISSING_ATTRIBUTE,                     systemID, lineNr,                     elementName,                     attributeName,                     /*attributeValue*/ null,                     "Element " + elementName + " expects an attribute named "                     + attributeName);   }   /**    * Throws an XMLValidationException to indicate that an attribute is    * unexpected.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param elementName    the name of the element    * @param attributeName  the name of the unexpected attribute    */   static void errorUnexpectedAttribute(String systemID,                                        int    lineNr,                                        String elementName,                                        String attributeName)      throws XMLValidationException   {      throw new XMLValidationException(                     XMLValidationException.UNEXPECTED_ATTRIBUTE,                     systemID, lineNr,                     elementName,                     attributeName,                     /*attributeValue*/ null,                     "Element " + elementName + " did not expect an attribute "                     + "named " + attributeName);   }   /**    * Throws an XMLValidationException to indicate that an attribute has an    * invalid value.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param elementName    the name of the element    * @param attributeName  the name of the attribute    * @param attributeValue the value of that attribute    */   static void errorInvalidAttributeValue(String systemID,                                          int    lineNr,                                          String elementName,                                          String attributeName,                                          String attributeValue)      throws XMLValidationException   {      throw new XMLValidationException(                           XMLValidationException.ATTRIBUTE_WITH_INVALID_VALUE,                           systemID, lineNr,                           elementName,                           attributeName,                           attributeValue,                           "Invalid value for attribute " + attributeName);   }   /**    * Throws an XMLValidationException to indicate that a #PCDATA element was    * missing.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param parentElementName the name of the parent element    */   static void errorMissingPCData(String systemID,                                  int    lineNr,                                  String parentElementName)      throws XMLValidationException   {      throw new XMLValidationException(                           XMLValidationException.MISSING_PCDATA,                           systemID, lineNr,                           /*elementName*/ null,                           /*attributeName*/ null,                           /*attributeValue*/ null,                           "Missing #PCDATA in element " + parentElementName);   }      /**    * Throws an XMLValidationException to indicate that a #PCDATA element was    * unexpected.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param parentElementName the name of the parent element    */   static void errorUnexpectedPCData(String systemID,                                     int    lineNr,                                     String parentElementName)      throws XMLValidationException   {      throw new XMLValidationException(                        XMLValidationException.UNEXPECTED_PCDATA,                        systemID, lineNr,                        /*elementName*/ null,                        /*attributeName*/ null,                        /*attributeValue*/ null,                        "Unexpected #PCDATA in element " + parentElementName);   }   /**    * Throws an XMLValidationException.    *    * @param systemID       the system ID of the data source    * @param lineNr         the line number in the data source    * @param message        the error message    * @param elementName    the name of the element    * @param attributeName  the name of the attribute    * @param attributeValue the value of that attribute    */   static void validationError(String systemID,                               int    lineNr,                               String message,                               String elementName,                               String attributeName,                               String attributeValue)      throws XMLValidationException   {      throw new XMLValidationException(XMLValidationException.MISC_ERROR,                                       systemID, lineNr,                                       elementName,                                       attributeName,                                       attributeValue,                                       message);   }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?