edu.stanford.nlp.util
Class XMLUtils

java.lang.Object
  extended by edu.stanford.nlp.util.XMLUtils

public class XMLUtils
extends Object

Provides some utilities for dealing with XML files, both by properly parsing them and by using the methods of a desperate Perl hacker.

Author:
Teg Grenager

Nested Class Summary
static class XMLUtils.XMLTag
           
 
Field Summary
static Set<String> breakingTags
          Block-level HTML tags that are rendered with surrounding line breaks.
 
Method Summary
static String escapeStringForXML(String s)
          Returns a String in which all of the special characters of XML have been escaped.
static String escapeTextAroundXMLTags(String s)
           
static String escapeXML(String in)
          Returns a String in which all the XML special characters have been escaped.
static boolean isBreaking(String tag)
           
static boolean isBreaking(XMLUtils.XMLTag tag)
           
static void main(String[] args)
          Tests a few methods.
static XMLUtils.XMLTag parseTag(String tagString)
           
static XMLUtils.XMLTag readAndParseTag(Reader r)
           
static Document readDocumentFromFile(String filename)
           
static Document readDocumentFromString(String s)
           
static String readTag(Reader r)
          Reads all text of the XML tag and returns it as a String.
static String readUntilTag(Reader r)
          Reads all text up to next XML tag and returns it as a String.
static String stripTags(Reader r, List<Integer> mapBack, boolean markLineBreaks)
           
static String unescapeStringForXML(String s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

breakingTags

public static final Set<String> breakingTags
Block-level HTML tags that are rendered with surrounding line breaks.

Method Detail

stripTags

public static String stripTags(Reader r,
                               List<Integer> mapBack,
                               boolean markLineBreaks)
Parameters:
r - the reader to read the XML/HTML from
mapBack - a List of Integers mapping the positions in the result buffer to positions in the original Reader, will be cleared on receipt
Returns:
the String containing the resulting text

isBreaking

public static boolean isBreaking(String tag)

isBreaking

public static boolean isBreaking(XMLUtils.XMLTag tag)

readUntilTag

public static String readUntilTag(Reader r)
                           throws IOException
Reads all text up to next XML tag and returns it as a String.

Returns:
the String of the text read, which may be empty.
Throws:
IOException

readAndParseTag

public static XMLUtils.XMLTag readAndParseTag(Reader r)
                                       throws Exception
Returns:
the new XMLTag object, or null if couldn't be created
Throws:
Exception

unescapeStringForXML

public static String unescapeStringForXML(String s)

escapeStringForXML

public static String escapeStringForXML(String s)
Returns a String in which all of the special characters of XML have been escaped. The resulting String can be used as text in well-formed XML. WARNING: You probably shouldn't use this method!! It only deals with ASCII chars, unwisely deciding to delete all other characters! See escapeXML(String) for a decent method for doing this!

Parameters:
s - The string to escape
Returns:
A String in which all the special characters of XML have been escaped

escapeXML

public static String escapeXML(String in)
Returns a String in which all the XML special characters have been escaped. The resulting String is valid to print in an XML file as an attribute or element value in all circumstances. (Note that it may escape characters that didn't need to be escaped.)

Parameters:
in - The String to escape
Returns:
The escaped String

escapeTextAroundXMLTags

public static String escapeTextAroundXMLTags(String s)

readTag

public static String readTag(Reader r)
                      throws IOException
Reads all text of the XML tag and returns it as a String. Assumes that a '<' character has already been read.

Parameters:
r - The reader to read from
Returns:
The String representing the tag, or null if one couldn't be read (i.e., EOF). The returned item is a complete tag including angle brackets, such as <TXT>
Throws:
IOException

parseTag

public static XMLUtils.XMLTag parseTag(String tagString)
                                throws Exception
Throws:
Exception

readDocumentFromFile

public static Document readDocumentFromFile(String filename)
                                     throws Exception
Throws:
Exception

readDocumentFromString

public static Document readDocumentFromString(String s)
                                       throws Exception
Throws:
Exception

main

public static void main(String[] args)
                 throws Exception
Tests a few methods. If the first arg is -readDoc then this method tests readDocumentFromFile. Otherwise, it tests readTag/readUntilTag and slurpFile.

Throws:
Exception


Stanford NLP Group