- /**
- * This class deals with the parsing of XML using
- * <a href="http://java.sun.com/dtd/properties.dtd">
- * http://java.sun.com/dtd/properties.dtd</a>.
- *
- * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
- * @since 1.5
- */
- private class PropertiesHandler
- extends DefaultHandler2
- {
-
- /**
- * The current key.
- */
- private String key;
-
- /**
- * The current value.
- */
- private String value;
-
- /**
- * A flag to check whether a valid DTD declaration has been seen.
- */
- private boolean dtdDeclSeen;
-
- /**
- * Constructs a new Properties handler.
- */
- public PropertiesHandler()
- {
- key = null;
- value = null;
- dtdDeclSeen = false;
- }
-
- /**
- * <p>
- * Captures the start of the DTD declarations, if they exist.
- * A valid properties file must declare the following doctype:
- * </p>
- * <p>
- * <code>!DOCTYPE properties SYSTEM
- * "http://java.sun.com/dtd/properties.dtd"</code>
- * </p>
- *
- * @param name the name of the document type.
- * @param publicId the public identifier that was declared, or
- * null if there wasn't one.
- * @param systemId the system identifier that was declared, or
- * null if there wasn't one.
- * @throws SAXException if some error occurs in parsing.
- */
- public void startDTD(String name, String publicId, String systemId)
- throws SAXException
- {
- if (name.equals("properties") &&
- publicId == null &&
- systemId.equals("http://java.sun.com/dtd/properties.dtd"))
- {
- dtdDeclSeen = true;
- }
- else
- throw new SAXException("Invalid DTD declaration: " + name);
- }
-
- /**
- * Captures the start of an XML element.
- *
- * @param uri the namespace URI.
- * @param localName the local name of the element inside the namespace.
- * @param qName the local name qualified with the namespace URI.
- * @param attributes the attributes of this element.
- * @throws SAXException if some error occurs in parsing.
- */
- public void startElement(String uri, String localName,
- String qName, Attributes attributes)
- throws SAXException
- {
- if (qName.equals("entry"))
- {
- int index = attributes.getIndex("key");
- if (index != -1)
- key = attributes.getValue(index);
- }
- else if (qName.equals("comment") || qName.equals("properties"))
- {
- /* Ignore it */
- }
- else
- throw new SAXException("Invalid tag: " + qName);
- }
-
- /**
- * Captures characters within an XML element.
- *
- * @param ch the array of characters.
- * @param start the start index of the characters to use.
- * @param length the number of characters to use from the start index on.
- * @throws SAXException if some error occurs in parsing.
- */
- public void characters(char[] ch, int start, int length)
- throws SAXException
- {
- if (key != null)
- value = new String(ch,start,length);
- }
-
- /**
- * Captures the end of an XML element.
- *
- * @param uri the namespace URI.
- * @param localName the local name of the element inside the namespace.
- * @param qName the local name qualified with the namespace URI.
- * @throws SAXException if some error occurs in parsing.
- */
- public void endElement(String uri, String localName,
- String qName)
- throws SAXException
- {
- if (qName.equals("entry"))
- {
- if (value == null)
- value = "";
- setProperty(key, value);
- key = null;
- value = null;
- }
- }
-
- /**
- * Captures the end of the XML document. If a DTD declaration has
- * not been seen, the document is erroneous and an exception is thrown.
- *
- * @throws SAXException if the correct DTD declaration didn't appear.
- */
- public void endDocument()
- throws SAXException
- {
- if (!dtdDeclSeen)
- throw new SAXException("No appropriate DTD declaration was seen.");
- }
-
- } // class PropertiesHandler
-