Java Reference
In-Depth Information
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
class LocalRecipeML implements EntityResolver
{
private Map<String, String> mappings = new HashMap<>();
LocalRecipeML()
{
mappings.put("-//FormatData//DTD RecipeML 0.5//EN",
"recipeml.dtd");
}
@Override
public InputSource resolveEntity(String publicId, String
systemId)
{
if (mappings.containsKey(publicId))
{
System.out.println("obtaining
cached
re-
cipeml.dtd");
systemId = mappings.get(publicId);
InputSource
localSource
=
new
In-
putSource(systemId);
return localSource;
}
return null;
}
}
Listing10-13 declares LocalRecipeML .Thisclass'sconstructorstorestheformal
publicidentifierfortheRecipeMLDTDandthesystemidentifierforalocalcopyofthis
DTD'sdocumentinamap—noticetheuseofJava7'sdiamondoperator( <> )tosimpli-
fy the hashmap instantiation expression.
Note Although it's unnecessary to use a map in this example (an if
(publicId.equals("-//FormatData//DTD RecipeML 0.5//EN"))
Search WWH ::




Custom Search