Skip to content

Commit

Permalink
ttools: add entity declaration options to Formatter
Browse files Browse the repository at this point in the history
Provide the option to declare some internal entities to the Formatter
class for it to use while parsing XML it's going to format.
I ended up not using this, but it might possibly come in useful at
a later date, so leave it in.
  • Loading branch information
mbtaylor committed Oct 3, 2014
1 parent 75abe74 commit da2be5b
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions ttools/src/main/uk/ac/starlink/ttools/Formatter.java
Expand Up @@ -3,13 +3,16 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
Expand All @@ -23,6 +26,7 @@
public class Formatter {

private final DocumentBuilder db_;
private final Map<String,String> entityMap_;
private String manualName_ = "SUN/256";

/**
Expand All @@ -35,6 +39,7 @@ public Formatter() {
catch ( ParserConfigurationException e ) {
throw new RuntimeException( e.getMessage(), e );
}
entityMap_ = new LinkedHashMap<String,String>();
}

/**
Expand Down Expand Up @@ -67,6 +72,18 @@ public String getManualName() {
return manualName_;
}

/**
* Adds an internal entity declaration to this formatter.
* Any entities added here are declared in the document declaration
* of XML parsed by the {@link #formatXML formatXML} method.
*
* @param entName internal entity name
* @param entValue entity value
*/
public void addEntity( String entName, String entValue ) {
entityMap_.put( entName, entValue );
}

/**
* Returns a string which is a formatted version of a DOM.
* The result is suitable for output on the terminal.
Expand Down Expand Up @@ -149,9 +166,12 @@ else if ( tag.equals( "ref" ) ) {
else if ( child instanceof Text ) {
result.appendWords( ((Text) child).getData() );
}
else if ( child instanceof DocumentType ) {
}
else {
throw new IllegalArgumentException( "Can't serialize node " +
child.getClass() );
child.getClass()
.getName() );
}
}
}
Expand All @@ -164,7 +184,23 @@ else if ( child instanceof Text ) {
* @return DOM
*/
private Document readDOM( String xml ) throws SAXException {
String dxml = "<DOC>" + xml + "</DOC>";
StringBuffer sbuf = new StringBuffer()
.append( "<?xml version='1.0'?>" )
.append( "<!DOCTYPE doc [" );
for ( Map.Entry<String,String> entry : entityMap_.entrySet() ) {
sbuf.append( "<!ENTITY " )
.append( entry.getKey() )
.append( " " )
.append( "'" )
.append( entry.getValue() )
.append( "'" )
.append( ">" );
}
sbuf.append( "]>" )
.append( "<DOC>" )
.append( xml )
.append( "</DOC>" );
String dxml = sbuf.toString();
try {
InputStream in = new ByteArrayInputStream( dxml.getBytes() );
Document doc = db_.parse( in );
Expand Down

0 comments on commit da2be5b

Please sign in to comment.