Search in sources :

Example 1 with ContextualSAXHandler

use of org.apache.ivy.util.ContextualSAXHandler in project ant-ivy by apache.

the class IBiblioResolver method findTimestampedSnapshotVersion.

private String findTimestampedSnapshotVersion(final ModuleRevisionId mrid) {
    if (!isM2compatible()) {
        return null;
    }
    if (!shouldUseMavenMetadata(getWholePattern())) {
        return null;
    }
    try {
        final String metadataLocation = IvyPatternHelper.substitute(root + "[organisation]/[module]/[revision]/maven-metadata.xml", mrid);
        final Resource metadata = getRepository().getResource(metadataLocation);
        if (!metadata.exists()) {
            Message.verbose("\tmaven-metadata not available for: " + mrid);
            return null;
        }
        try (final InputStream metadataStream = metadata.openStream()) {
            final StringBuilder timestamp = new StringBuilder();
            final StringBuilder buildNumber = new StringBuilder();
            XMLHelper.parse(metadataStream, null, new ContextualSAXHandler() {

                @Override
                public void endElement(String uri, String localName, String qName) throws SAXException {
                    if ("metadata/versioning/snapshot/timestamp".equals(getContext())) {
                        timestamp.append(getText());
                    }
                    if ("metadata/versioning/snapshot/buildNumber".equals(getContext())) {
                        buildNumber.append(getText());
                    }
                    super.endElement(uri, localName, qName);
                }
            }, null);
            if (timestamp.length() > 0) {
                // we have found a timestamp, so this is a snapshot unique version
                String rev = mrid.getRevision();
                rev = rev.substring(0, rev.length() - "SNAPSHOT".length());
                rev += timestamp.toString() + "-" + buildNumber.toString();
                return rev;
            }
        }
    } catch (IOException | SAXException | ParserConfigurationException e) {
        Message.debug("impossible to access maven metadata file, ignored", e);
    }
    return null;
}
Also used : ContextualSAXHandler(org.apache.ivy.util.ContextualSAXHandler) InputStream(java.io.InputStream) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) Resource(org.apache.ivy.plugins.repository.Resource) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 2 with ContextualSAXHandler

use of org.apache.ivy.util.ContextualSAXHandler in project ant-ivy by apache.

the class IBiblioResolver method listRevisionsWithMavenMetadata.

private List<String> listRevisionsWithMavenMetadata(Repository repository, String metadataLocation) {
    List<String> revs = null;
    InputStream metadataStream = null;
    try {
        Resource metadata = repository.getResource(metadataLocation);
        if (metadata.exists()) {
            Message.verbose("\tlisting revisions from maven-metadata: " + metadata);
            final List<String> metadataRevs = new ArrayList<>();
            metadataStream = metadata.openStream();
            XMLHelper.parse(metadataStream, null, new ContextualSAXHandler() {

                @Override
                public void endElement(String uri, String localName, String qName) throws SAXException {
                    if ("metadata/versioning/versions/version".equals(getContext())) {
                        metadataRevs.add(getText().trim());
                    }
                    super.endElement(uri, localName, qName);
                }
            }, null);
            revs = metadataRevs;
        } else {
            Message.verbose("\tmaven-metadata not available: " + metadata);
        }
    } catch (IOException e) {
        Message.verbose("impossible to access maven metadata file, ignored", e);
    } catch (SAXException | ParserConfigurationException e) {
        Message.verbose("impossible to parse maven metadata file, ignored", e);
    } finally {
        if (metadataStream != null) {
            try {
                metadataStream.close();
            } catch (IOException e) {
            // ignored
            }
        }
    }
    return revs;
}
Also used : ContextualSAXHandler(org.apache.ivy.util.ContextualSAXHandler) InputStream(java.io.InputStream) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) Resource(org.apache.ivy.plugins.repository.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Aggregations

IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Resource (org.apache.ivy.plugins.repository.Resource)2 ResolvedResource (org.apache.ivy.plugins.resolver.util.ResolvedResource)2 ContextualSAXHandler (org.apache.ivy.util.ContextualSAXHandler)2 SAXException (org.xml.sax.SAXException)2 ArrayList (java.util.ArrayList)1