All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xmlresolver.catalog.query.QueryPublic Maven / Gradle / Ivy

There is a newer version: 6.0.11
Show newest version
package org.xmlresolver.catalog.query;

import org.xmlresolver.CatalogManager;
import org.xmlresolver.catalog.entry.Entry;
import org.xmlresolver.catalog.entry.EntryCatalog;
import org.xmlresolver.catalog.entry.EntryDelegatePublic;
import org.xmlresolver.catalog.entry.EntryPublic;

import java.net.URI;
import java.util.ArrayList;

/**
 * Query for public entries in the catalog.
 */
public class QueryPublic extends QueryCatalog {
    /** The system identifier. */
    public final String systemId;
    /** The public identifier. */
    public final String publicId;

    /**
     * QueryPublic constructor.
     * @param systemId The system identifier.
     * @param publicId The public identifier.
     */
    public QueryPublic(String systemId, String publicId) {
        super();
        this.systemId = systemId;
        this.publicId = publicId;
    }

    @Override
    protected QueryResult lookup(CatalogManager manager, EntryCatalog catalog) {
        if (systemId != null) {
            QuerySystem query = new QuerySystem(systemId);
            QueryResult result = query.lookup(manager, catalog);
            if (result.resolved()) {
                return result;
            }
        }

        if (publicId != null) {
            // 
            for (Entry raw : catalog.entries(Entry.Type.PUBLIC)) {
                EntryPublic entry = (EntryPublic) raw;
                if (entry.preferPublic || systemId == null) {
                    if (entry.publicId.equals(publicId)) {
                        return new QueryResult(entry.uri);
                    }
                }
            }

            // 
            ArrayList delegated = new ArrayList<>();
            for (Entry raw : catalog.entries(Entry.Type.DELEGATE_PUBLIC)) {
                EntryDelegatePublic entry = (EntryDelegatePublic) raw;
                if (entry.preferPublic || systemId == null) {
                    if (publicId.startsWith(entry.publicIdStart)) {
                        int pos = 0;
                        while (pos < delegated.size() && entry.publicIdStart.length() <= delegated.get(pos).publicIdStart.length()) {
                            pos += 1;
                        }
                        delegated.add(pos, entry);
                    }
                }
            }
            if (!delegated.isEmpty()) {
                ArrayList catalogs = new ArrayList<>();
                for (EntryDelegatePublic entry : delegated) {
                    catalogs.add(entry.catalog);
                }
                return new QueryDelegatePublic(systemId, publicId, catalogs);
            }
        }

        return QueryResult.EMPTY_RESULT;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy