package javax.xml.catalog;
import java.io.StringReader;
import javax.xml.catalog.BaseEntry.CatalogEntryType;
import javax.xml.parsers.SAXParser;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.Attributes;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
class CatalogReader extends DefaultHandler implements EntityResolver, URIResolver {
public static final String xmlCatalogXSD = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.xsd";
public static final String xmlCatalogPubId = "-//OASIS//DTD XML Catalogs V1.0//EN";
public static final String NAMESPACE_OASIS = "urn:oasis:names:tc:entity:xmlns:xml:catalog";
boolean seenRoot;
boolean inGroup;
CatalogImpl catalog;
SAXParser parser;
CatalogEntry catalogEntry;
GroupEntry group;
BaseEntry entry;
boolean ignoreTheCatalog = false;
@SuppressWarnings("unchecked")
public CatalogReader(Catalog catalog, SAXParser parser) {
this.catalog = (CatalogImpl) catalog;
this.parser = parser;
}
@Override
public void startElement(String namespaceURI,
String localName,
String qName,
Attributes atts)
throws SAXException {
if (ignoreTheCatalog) return;
if (!NAMESPACE_OASIS.equals(namespaceURI)) {
ignoreTheCatalog = true;
return;
}
CatalogEntryType type = CatalogEntryType.getType(localName);
if (type == null) {
CatalogMessages.reportError(CatalogMessages.ERR_INVALID_ENTRY_TYPE,
new Object[]{localName});
}
if (type != CatalogEntryType.CATALOGENTRY) {
if (!seenRoot) {
CatalogMessages.reportError(CatalogMessages.ERR_INVALID_CATALOG);
}
}
String base = atts.getValue("xml:base");
if (base == null) {
if (inGroup) {
base = group.getBaseURI().toString();
} else {
if (type == CatalogEntryType.CATALOGENTRY) {
base = catalog.getBaseURI().toString();
} else {
base = catalogEntry.getBaseURI().toString();
}
}
} else {
base = Normalizer.normalizeURI(base);
}
if (type == CatalogEntryType.CATALOGENTRY
|| type == CatalogEntryType.GROUP) {
String prefer = atts.getValue("prefer");
if (prefer == null) {
if (type == CatalogEntryType.CATALOGENTRY) {
prefer = catalog.isPreferPublic() ?
CatalogFeatures.PREFER_PUBLIC : CatalogFeatures.PREFER_SYSTEM;
} else {
prefer = catalogEntry.isPreferPublic() ?
CatalogFeatures.PREFER_PUBLIC : CatalogFeatures.PREFER_SYSTEM;
}
}
if (type == CatalogEntryType.CATALOGENTRY) {
seenRoot = true;
if (catalog.isTop()) {
String defer = atts.getValue("defer");
String resolve = atts.getValue("resolve");
if (defer == null) {
defer = catalog.isDeferred() ?
CatalogFeatures.DEFER_TRUE : CatalogFeatures.DEFER_FALSE;
}
if (resolve == null) {
resolve = catalog.getResolve().literal;
}
catalog.setResolve(resolve);
catalog.setDeferred(defer);
catalogEntry = new CatalogEntry(base, prefer, defer, resolve);
} else {
catalogEntry = new CatalogEntry(base, prefer);
}
catalog.setPrefer(prefer);
return;
} else {
inGroup = true;
group = new GroupEntry(catalog, base, prefer);
catalog.addEntry(group);
return;
}
}
switch (type) {
case PUBLIC:
entry = new PublicEntry(base, atts.getValue("publicId"), atts.getValue("uri"));
break;
case SYSTEM:
entry = new SystemEntry(base, atts.getValue("systemId"), atts.getValue("uri"));
break;
case REWRITESYSTEM:
entry = new RewriteSystem(base, atts.getValue("systemIdStartString"), atts.getValue("rewritePrefix"));
break;
case SYSTEMSUFFIX:
entry = new SystemSuffix(base, atts.getValue("systemIdSuffix"), atts.getValue("uri"));
break;
case DELEGATEPUBLIC:
entry = new DelegatePublic(base, atts.getValue("publicIdStartString"), atts.getValue("catalog"));
break;
case DELEGATESYSTEM:
entry = new DelegateSystem(base, atts.getValue("systemIdStartString"), atts.getValue("catalog"));
break;
case URI:
entry = new UriEntry(base, atts.getValue("name"), atts.getValue("uri"));
break;
case REWRITEURI:
entry = new RewriteUri(base, atts.getValue("uriStartString"), atts.getValue("rewritePrefix"));
break;
case URISUFFIX:
entry = new UriSuffix(base, atts.getValue("uriSuffix"), atts.getValue("uri"));
break;
case DELEGATEURI:
entry = new DelegateUri(base, atts.getValue("uriStartString"), atts.getValue("catalog"));
break;
case NEXTCATALOG:
entry = new NextCatalog(base, atts.getValue("catalog"));
break;
}
if (type == CatalogEntryType.NEXTCATALOG) {
catalog.addNextCatalog((NextCatalog) entry);
} else if (inGroup) {
group.addEntry(entry);
} else {
catalog.addEntry(entry);