Example usage for org.w3c.dom Document getNamespaceURI

List of usage examples for org.w3c.dom Document getNamespaceURI

Introduction

In this page you can find the example usage for org.w3c.dom Document getNamespaceURI.

Prototype

public String getNamespaceURI();

Source Link

Document

The namespace URI of this node, or null if it is unspecified (see ).

Usage

From source file:com.amalto.core.history.accessor.UnaryFieldAccessor.java

private Element internalCreate() {
    parent.create();//from www .java 2  s . c  o  m
    Document domDocument = document.asDOM();
    Element element = getElement();
    if (element == null) {
        Element newElement = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName);
        Node parentNode = parent.getNode();
        Node lastAccessedNode = document.getLastAccessedNode();
        if (parentNode == lastAccessedNode) {
            parentNode.insertBefore(newElement, parentNode.getFirstChild());
        } else if (lastAccessedNode != null && lastAccessedNode.getParentNode() == parentNode) {
            parentNode.insertBefore(newElement, lastAccessedNode.getNextSibling());
        } else {
            parentNode.appendChild(newElement);
        }
        element = newElement;
        document.setLastAccessedNode(element);
    }
    return element;
}

From source file:com.amalto.core.history.accessor.ManyFieldAccessor.java

public void insert() {
    Document domDocument = document.asDOM();
    Element parentNode = (Element) parent.getNode();
    NodeList children = parentNode.getElementsByTagName(fieldName);
    Node refNode = children.item(index);
    Node node = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName);
    parentNode.insertBefore(node, refNode);
    cachedCollectionItemNode = (Element) node;
}

From source file:com.jkoolcloud.tnt4j.streams.parsers.ActivityXmlParserTest.java

@Test
public void testNamespaceAwareXpathResolveWithPreparser() throws Exception {
    NamespaceTestSuite testSuite = new NamespaceTestSuite().invoke();
    DocumentBuilderFactory domFactory = testSuite.getDomFactory();
    String xmlString = testSuite.getXmlString();
    XPath xPath = testSuite.getxPath();
    Map<String, String> namespaces = testSuite.getNamespaces();

    DocumentBuilder builder = domFactory.newDocumentBuilder();
    // Document document = builder.parse(IOUtils.toInputStream(xmlString, Utils.UTF8));
    XMLFromBinDataPreParser xmlFromBinDataPreParser = new XMLFromBinDataPreParser();
    Document document = xmlFromBinDataPreParser.preParse(xmlString);

    NamespaceMap nsContext = new NamespaceMap();
    xPath.setNamespaceContext(nsContext);

    nsContext.addPrefixUriMappings(namespaces);

    Document tDoc = document.getOwnerDocument();
    Element docElem = tDoc == null ? null : tDoc.getDocumentElement();
    if (tDoc == null || StringUtils.isEmpty(tDoc.getNamespaceURI())) {
        document = builder//ww w .  j  a v a  2 s .  co m
                .parse(new ReaderInputStream(new StringReader(Utils.documentToString(document)), Utils.UTF8));
    }

    NamespaceMap documentNamespaces = new NamespaceMap();
    StreamsXMLUtils.resolveDocumentNamespaces(document, documentNamespaces, false);

    String evaluate = xPath.compile("/soapenv:Envelope/soapenv:Header/ch:TSYSprofileInq/ch:clientData")
            .evaluate(document);
    assertEquals("xxxxxx-343e-46af-86aa-634a3688cf30", evaluate);
    evaluate = xPath.compile("/Envelope/Header/TSYSprofileInq/clientData").evaluate(document);
    assertEquals("", evaluate);
}

From source file:com.amalto.core.history.accessor.ManyFieldAccessor.java

public void create() {
    parent.create();//from  w ww.j  ava2 s .  c  o m

    // TODO Refactor this
    Document domDocument = document.asDOM();
    Node node = getCollectionItemNode();
    if (node == null) {
        Element parentNode = (Element) parent.getNode();
        NodeList children = parentNode.getElementsByTagName(fieldName);
        int currentCollectionSize = children.getLength();
        if (currentCollectionSize > 0) {
            Node refNode = children.item(currentCollectionSize - 1).getNextSibling();
            while (currentCollectionSize <= index) {
                node = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName);
                parentNode.insertBefore(node, refNode);
                currentCollectionSize++;
            }
        } else {
            // Collection is not present at all, append at the end of parent element.
            Node lastAccessedNode = document.getLastAccessedNode();
            if (lastAccessedNode != null) {
                Node refNode = lastAccessedNode.getNextSibling();
                while (refNode != null && !(refNode instanceof Element)) {
                    refNode = refNode.getNextSibling();
                }
                while (currentCollectionSize <= index) {
                    node = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName);
                    if (lastAccessedNode == parentNode) {
                        if (lastAccessedNode == document.asDOM().getDocumentElement()
                                && lastAccessedNode.getChildNodes().getLength() > 0)
                            parentNode.insertBefore(node, parentNode.getFirstChild());
                        else
                            parentNode.appendChild(node);
                    } else if (refNode != null && refNode.getParentNode() == parentNode) {
                        parentNode.insertBefore(node, refNode);
                    } else {
                        parentNode.appendChild(node);
                    }
                    currentCollectionSize++;
                }
            } else {
                while (currentCollectionSize <= index) {
                    node = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName);
                    parentNode.appendChild(node);
                    currentCollectionSize++;
                }
            }
        }
        document.setLastAccessedNode(node);
    } else if (node.getChildNodes().getLength() == 0) {
        // This accessor creates (n-1) empty elements when accessing first collection element at index n.
        // This setLastAccessedNode call allows all (n-1) elements to find their parent.
        if (!(node.getLocalName().equals(document.getLastAccessedNode().getLocalName())
                && document.getLastAccessedNode().getParentNode() == node.getParentNode())) {
            // if last accessed node is parallel with this node, can't modify last accessed node. eg, last accessed
            // node=/feature/vendor[2], this node=/feature/vendor[1], the last accessed is still /feature/vendor[2]
            document.setLastAccessedNode(node);
        }
    }
}

From source file:org.commonjava.aprox.core.content.MavenMetadataGenerator.java

private boolean writeVersionMetadata(final List<StoreResource> firstLevelFiles, final ArtifactStore store,
        final String path) throws AproxWorkflowException {
    ArtifactPathInfo samplePomInfo = null;

    // first level will contain version directories...for each directory, we need to verify the presence of a .pom file before including 
    // as a valid version
    final List<SingleVersion> versions = new ArrayList<SingleVersion>();
    nextTopResource: for (final StoreResource topResource : firstLevelFiles) {
        final String topPath = topResource.getPath();
        if (topPath.endsWith("/")) {
            final List<StoreResource> secondLevelListing = fileManager.list(store, topPath);
            for (final StoreResource fileResource : secondLevelListing) {
                if (fileResource.getPath().endsWith(".pom")) {
                    versions.add(VersionUtils.createSingleVersion(new File(topPath).getName()));
                    if (samplePomInfo == null) {
                        samplePomInfo = ArtifactPathInfo.parse(fileResource.getPath());
                    }/*from   w  w  w . ja  v a  2s  .  c o  m*/

                    continue nextTopResource;
                }
            }
        }
    }

    if (versions.isEmpty()) {
        return false;
    }

    Collections.sort(versions);

    final Transfer metadataFile = fileManager.getStorageReference(store, path);
    OutputStream stream = null;
    try {
        final Document doc = xml.newDocumentBuilder().newDocument();

        final Map<String, String> coordMap = new HashMap<String, String>();
        coordMap.put(ARTIFACT_ID, samplePomInfo.getArtifactId());
        coordMap.put(GROUP_ID, samplePomInfo.getGroupId());

        final String lastUpdated = SnapshotUtils.generateUpdateTimestamp(SnapshotUtils.getCurrentTimestamp());

        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);

        final Map<String, String> versioningMap = new HashMap<String, String>();
        versioningMap.put(LAST_UPDATED, lastUpdated);

        final SingleVersion latest = versions.get(versions.size() - 1);
        versioningMap.put(LATEST, latest.renderStandard());

        SingleVersion release = null;
        for (int i = versions.size() - 1; i >= 0; i--) {
            final SingleVersion r = versions.get(i);
            if (r.isRelease()) {
                release = r;
                break;
            }
        }

        if (release != null) {
            versioningMap.put(RELEASE, release.renderStandard());
        }

        xml.createElement(doc, "versioning", versioningMap);
        final Element versionsElem = xml.createElement(doc, "versioning/versions",
                Collections.<String, String>emptyMap());

        for (final SingleVersion version : versions) {
            final Element vElem = doc.createElement(VERSION);
            vElem.setTextContent(version.renderStandard());

            versionsElem.appendChild(vElem);
        }

        final String xmlStr = xml.toXML(doc, true);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE);
        stream.write(xmlStr.getBytes("UTF-8"));
    } catch (final GalleyMavenXMLException e) {
        throw new AproxWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path,
                e.getMessage());
    } catch (final IOException e) {
        throw new AproxWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e,
                metadataFile, e.getMessage());
    } finally {
        closeQuietly(stream);
    }

    return true;
}

From source file:org.commonjava.aprox.core.content.MavenMetadataGenerator.java

private boolean writeSnapshotMetadata(final ArtifactPathInfo info, final List<StoreResource> files,
        final ArtifactStore store, final String path) throws AproxWorkflowException {
    // first level will contain files that have the timestamp-buildnumber version suffix...for each, we need to parse this info.
    final Map<SnapshotPart, Set<ArtifactPathInfo>> infosBySnap = new HashMap<SnapshotPart, Set<ArtifactPathInfo>>();
    for (final StoreResource resource : files) {
        final ArtifactPathInfo resInfo = ArtifactPathInfo.parse(resource.getPath());
        if (resInfo != null) {
            final SnapshotPart snap = resInfo.getSnapshotInfo();
            Set<ArtifactPathInfo> infos = infosBySnap.get(snap);
            if (infos == null) {
                infos = new HashSet<ArtifactPathInfo>();
                infosBySnap.put(snap, infos);
            }//from  w ww. j  a  va 2  s . c om

            infos.add(resInfo);
        }
    }

    if (infosBySnap.isEmpty()) {
        return false;
    }

    final List<SnapshotPart> snaps = new ArrayList<SnapshotPart>(infosBySnap.keySet());
    Collections.sort(snaps);

    final Transfer metadataFile = fileManager.getStorageReference(store, path);
    OutputStream stream = null;
    try {
        final Document doc = xml.newDocumentBuilder().newDocument();

        final Map<String, String> coordMap = new HashMap<String, String>();
        coordMap.put(ARTIFACT_ID, info.getArtifactId());
        coordMap.put(GROUP_ID, info.getGroupId());
        coordMap.put(VERSION, info.getVersion());

        final String lastUpdated = SnapshotUtils.generateUpdateTimestamp(SnapshotUtils.getCurrentTimestamp());

        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);

        xml.createElement(doc, "versioning",
                Collections.<String, String>singletonMap(LAST_UPDATED, lastUpdated));

        SnapshotPart snap = snaps.get(snaps.size() - 1);
        Map<String, String> snapMap = new HashMap<String, String>();
        if (snap.isLocalSnapshot()) {
            snapMap.put(LOCAL_COPY, Boolean.TRUE.toString());
        } else {
            snapMap.put(TIMESTAMP, SnapshotUtils.generateSnapshotTimestamp(snap.getTimestamp()));

            snapMap.put(BUILD_NUMBER, Integer.toString(snap.getBuildNumber()));
        }

        xml.createElement(doc, "versioning/snapshot", snapMap);

        for (int i = 0; i < snaps.size(); i++) {
            snap = snaps.get(i);

            // the last one is the most recent.
            final Set<ArtifactPathInfo> infos = infosBySnap.get(snap);
            for (final ArtifactPathInfo pathInfo : infos) {
                snapMap = new HashMap<String, String>();

                final TypeAndClassifier tc = new TypeAndClassifier(pathInfo.getType(),
                        pathInfo.getClassifier());
                final TypeMapping mapping = typeMapper.lookup(tc);

                final String classifier = mapping == null ? pathInfo.getClassifier() : mapping.getClassifier();
                if (classifier != null && classifier.length() > 0) {
                    snapMap.put(CLASSIFIER, classifier);
                }

                snapMap.put(EXTENSION, mapping == null ? pathInfo.getType() : mapping.getExtension());
                snapMap.put(VALUE, pathInfo.getVersion());
                snapMap.put(UPDATED, lastUpdated);

                xml.createElement(doc, "versioning/snapshotVersions/snapshotVersion", snapMap);
            }
        }

        final String xmlStr = xml.toXML(doc, true);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE);
        stream.write(xmlStr.getBytes("UTF-8"));
    } catch (final GalleyMavenXMLException e) {
        throw new AproxWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path,
                e.getMessage());
    } catch (final IOException e) {
        throw new AproxWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e,
                metadataFile, e.getMessage());
    } finally {
        closeQuietly(stream);
    }

    return true;
}

From source file:org.commonjava.indy.core.content.MavenMetadataGenerator.java

private boolean writeVersionMetadata(final List<StoreResource> firstLevelFiles, final ArtifactStore store,
        final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    ArtifactPathInfo samplePomInfo = null;

    // first level will contain version directories...for each directory, we need to verify the presence of a .pom file before including
    // as a valid version
    final List<SingleVersion> versions = new ArrayList<SingleVersion>();
    nextTopResource: for (final StoreResource topResource : firstLevelFiles) {
        final String topPath = topResource.getPath();
        if (topPath.endsWith("/")) {
            final List<StoreResource> secondLevelListing = fileManager.listRaw(store, topPath);
            for (final StoreResource fileResource : secondLevelListing) {
                if (fileResource.getPath().endsWith(".pom")) {
                    ArtifactPathInfo filePomInfo = ArtifactPathInfo.parse(fileResource.getPath());
                    // check if the pom is valid for the path
                    if (filePomInfo != null) {
                        versions.add(VersionUtils.createSingleVersion(new File(topPath).getName()));
                        if (samplePomInfo == null) {
                            samplePomInfo = filePomInfo;
                        }//from  w w  w  .  j  av  a 2s  .  com

                        continue nextTopResource;
                    }
                }
            }
        }
    }

    if (versions.isEmpty()) {
        return false;
    }

    Collections.sort(versions);

    final Transfer metadataFile = fileManager.getTransfer(store, path);
    OutputStream stream = null;
    try {
        final Document doc = xml.newDocumentBuilder().newDocument();

        final Map<String, String> coordMap = new HashMap<String, String>();
        coordMap.put(ARTIFACT_ID, samplePomInfo.getArtifactId());
        coordMap.put(GROUP_ID, samplePomInfo.getGroupId());

        final String lastUpdated = SnapshotUtils.generateUpdateTimestamp(SnapshotUtils.getCurrentTimestamp());

        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);

        final Map<String, String> versioningMap = new HashMap<String, String>();
        versioningMap.put(LAST_UPDATED, lastUpdated);

        final SingleVersion latest = versions.get(versions.size() - 1);
        versioningMap.put(LATEST, latest.renderStandard());

        SingleVersion release = null;
        for (int i = versions.size() - 1; i >= 0; i--) {
            final SingleVersion r = versions.get(i);
            if (r.isRelease()) {
                release = r;
                break;
            }
        }

        if (release != null) {
            versioningMap.put(RELEASE, release.renderStandard());
        }

        xml.createElement(doc, "versioning", versioningMap);
        final Element versionsElem = xml.createElement(doc, "versioning/versions",
                Collections.<String, String>emptyMap());

        for (final SingleVersion version : versions) {
            final Element vElem = doc.createElement(VERSION);
            vElem.setTextContent(version.renderStandard());

            versionsElem.appendChild(vElem);
        }

        final String xmlStr = xml.toXML(doc, true);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
        stream.write(xmlStr.getBytes("UTF-8"));
    } catch (final GalleyMavenXMLException e) {
        throw new IndyWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path,
                e.getMessage());
    } catch (final IOException e) {
        throw new IndyWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e,
                metadataFile, e.getMessage());
    } finally {
        closeQuietly(stream);
    }

    return true;
}

From source file:org.commonjava.indy.core.content.MavenMetadataGenerator.java

private boolean writeSnapshotMetadata(final ArtifactPathInfo info, final List<StoreResource> files,
        final ArtifactStore store, final String path, final EventMetadata eventMetadata)
        throws IndyWorkflowException {
    // first level will contain files that have the timestamp-buildnumber version suffix...for each, we need to parse this info.
    final Map<SnapshotPart, Set<ArtifactPathInfo>> infosBySnap = new HashMap<SnapshotPart, Set<ArtifactPathInfo>>();
    for (final StoreResource resource : files) {
        final ArtifactPathInfo resInfo = ArtifactPathInfo.parse(resource.getPath());
        if (resInfo != null) {
            final SnapshotPart snap = resInfo.getSnapshotInfo();
            Set<ArtifactPathInfo> infos = infosBySnap.get(snap);
            if (infos == null) {
                infos = new HashSet<ArtifactPathInfo>();
                infosBySnap.put(snap, infos);
            }/*from  ww  w  . j av  a  2s .  com*/

            infos.add(resInfo);
        }
    }

    if (infosBySnap.isEmpty()) {
        return false;
    }

    final List<SnapshotPart> snaps = new ArrayList<SnapshotPart>(infosBySnap.keySet());
    Collections.sort(snaps);

    final Transfer metadataFile = fileManager.getTransfer(store, path);
    OutputStream stream = null;
    try {
        final Document doc = xml.newDocumentBuilder().newDocument();

        final Map<String, String> coordMap = new HashMap<String, String>();
        coordMap.put(ARTIFACT_ID, info.getArtifactId());
        coordMap.put(GROUP_ID, info.getGroupId());
        coordMap.put(VERSION, info.getVersion());

        final String lastUpdated = SnapshotUtils.generateUpdateTimestamp(SnapshotUtils.getCurrentTimestamp());

        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);

        xml.createElement(doc, "versioning",
                Collections.<String, String>singletonMap(LAST_UPDATED, lastUpdated));

        SnapshotPart snap = snaps.get(snaps.size() - 1);
        Map<String, String> snapMap = new HashMap<String, String>();
        if (snap.isLocalSnapshot()) {
            snapMap.put(LOCAL_COPY, Boolean.TRUE.toString());
        } else {
            snapMap.put(TIMESTAMP, SnapshotUtils.generateSnapshotTimestamp(snap.getTimestamp()));

            snapMap.put(BUILD_NUMBER, Integer.toString(snap.getBuildNumber()));
        }

        xml.createElement(doc, "versioning/snapshot", snapMap);

        for (int i = 0; i < snaps.size(); i++) {
            snap = snaps.get(i);

            // the last one is the most recent.
            final Set<ArtifactPathInfo> infos = infosBySnap.get(snap);
            for (final ArtifactPathInfo pathInfo : infos) {
                snapMap = new HashMap<String, String>();

                final TypeAndClassifier tc = new SimpleTypeAndClassifier(pathInfo.getType(),
                        pathInfo.getClassifier());

                final TypeMapping mapping = typeMapper.lookup(tc);

                final String classifier = mapping == null ? pathInfo.getClassifier() : mapping.getClassifier();
                if (classifier != null && classifier.length() > 0) {
                    snapMap.put(CLASSIFIER, classifier);
                }

                snapMap.put(EXTENSION, mapping == null ? pathInfo.getType() : mapping.getExtension());
                snapMap.put(VALUE, pathInfo.getVersion());
                snapMap.put(UPDATED, lastUpdated);

                xml.createElement(doc, "versioning/snapshotVersions/snapshotVersion", snapMap);
            }
        }

        final String xmlStr = xml.toXML(doc, true);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
        stream.write(xmlStr.getBytes("UTF-8"));
    } catch (final GalleyMavenXMLException e) {
        throw new IndyWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path,
                e.getMessage());
    } catch (final IOException e) {
        throw new IndyWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e,
                metadataFile, e.getMessage());
    } finally {
        closeQuietly(stream);
    }

    return true;
}

From source file:org.commonjava.indy.pkg.maven.content.MavenMetadataGenerator.java

private boolean writeVersionMetadata(final List<StoreResource> firstLevelFiles, final ArtifactStore store,
        final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    ArtifactPathInfo samplePomInfo = null;

    logger.debug("writeVersionMetadata, firstLevelFiles:{}, store:{}", firstLevelFiles, store.getKey());

    // first level will contain version directories...for each directory, we need to verify the presence of a .pom file before including
    // as a valid version
    final List<SingleVersion> versions = new ArrayList<>();
    nextTopResource: for (final StoreResource topResource : firstLevelFiles) {
        final String topPath = topResource.getPath();
        if (topPath.endsWith("/")) {
            final List<StoreResource> secondLevelListing = fileManager.listRaw(store, topPath);
            for (final StoreResource fileResource : secondLevelListing) {
                if (fileResource.getPath().endsWith(".pom")) {
                    ArtifactPathInfo filePomInfo = ArtifactPathInfo.parse(fileResource.getPath());
                    // check if the pom is valid for the path
                    if (filePomInfo != null) {
                        versions.add(VersionUtils.createSingleVersion(new File(topPath).getName()));
                        if (samplePomInfo == null) {
                            samplePomInfo = filePomInfo;
                        }/*from   w  w w . ja  v a 2  s  .co  m*/

                        continue nextTopResource;
                    }
                }
            }
        }
    }

    if (versions.isEmpty()) {
        logger.debug("writeVersionMetadata, versions is empty, store:{}", store.getKey());
        return false;
    }

    logger.debug("writeVersionMetadata, versions: {}, store:{}", versions, store.getKey());

    Collections.sort(versions);

    final Transfer metadataFile = fileManager.getTransfer(store, path);
    OutputStream stream = null;
    try {
        final Document doc = xml.newDocumentBuilder().newDocument();

        final Map<String, String> coordMap = new HashMap<>();
        coordMap.put(ARTIFACT_ID, samplePomInfo == null ? null : samplePomInfo.getArtifactId());
        coordMap.put(GROUP_ID, samplePomInfo == null ? null : samplePomInfo.getGroupId());

        final String lastUpdated = SnapshotUtils.generateUpdateTimestamp(SnapshotUtils.getCurrentTimestamp());

        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);

        final Map<String, String> versioningMap = new HashMap<>();
        versioningMap.put(LAST_UPDATED, lastUpdated);

        final SingleVersion latest = versions.get(versions.size() - 1);
        versioningMap.put(LATEST, latest.renderStandard());

        SingleVersion release = null;
        for (int i = versions.size() - 1; i >= 0; i--) {
            final SingleVersion r = versions.get(i);
            if (r.isRelease()) {
                release = r;
                break;
            }
        }

        if (release != null) {
            versioningMap.put(RELEASE, release.renderStandard());
        }

        xml.createElement(doc, "versioning", versioningMap);
        final Element versionsElem = xml.createElement(doc, "versioning/versions", Collections.emptyMap());

        for (final SingleVersion version : versions) {
            final Element vElem = doc.createElement(VERSION);
            vElem.setTextContent(version.renderStandard());

            versionsElem.appendChild(vElem);
        }

        final String xmlStr = xml.toXML(doc, true);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
        stream.write(xmlStr.getBytes("UTF-8"));
    } catch (final GalleyMavenXMLException e) {
        throw new IndyWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path,
                e.getMessage());
    } catch (final IOException e) {
        throw new IndyWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e,
                metadataFile, e.getMessage());
    } finally {
        closeQuietly(stream);
    }

    logger.debug("writeVersionMetadata, DONE, store: {}", store.getKey());
    return true;
}

From source file:org.commonjava.indy.pkg.maven.content.MavenMetadataGenerator.java

private boolean writeSnapshotMetadata(final ArtifactPathInfo info, final List<StoreResource> files,
        final ArtifactStore store, final String path, final EventMetadata eventMetadata)
        throws IndyWorkflowException {
    // first level will contain files that have the timestamp-buildnumber version suffix...for each, we need to parse this info.
    final Map<SnapshotPart, Set<ArtifactPathInfo>> infosBySnap = new HashMap<>();
    for (final StoreResource resource : files) {
        final ArtifactPathInfo resInfo = ArtifactPathInfo.parse(resource.getPath());
        if (resInfo != null) {
            final SnapshotPart snap = resInfo.getSnapshotInfo();
            Set<ArtifactPathInfo> infos = infosBySnap.computeIfAbsent(snap, k -> new HashSet<>());
            infos.add(resInfo);//from www.  ja  v a 2  s.c  o m
        }
    }

    if (infosBySnap.isEmpty()) {
        return false;
    }

    final List<SnapshotPart> snaps = new ArrayList<>(infosBySnap.keySet());
    Collections.sort(snaps);

    final Transfer metadataFile = fileManager.getTransfer(store, path);
    OutputStream stream = null;
    try {
        final Document doc = xml.newDocumentBuilder().newDocument();

        final Map<String, String> coordMap = new HashMap<>();
        coordMap.put(ARTIFACT_ID, info.getArtifactId());
        coordMap.put(GROUP_ID, info.getGroupId());
        coordMap.put(VERSION, info.getVersion());

        final String lastUpdated = SnapshotUtils.generateUpdateTimestamp(SnapshotUtils.getCurrentTimestamp());

        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);

        xml.createElement(doc, "versioning", Collections.singletonMap(LAST_UPDATED, lastUpdated));

        SnapshotPart snap = snaps.get(snaps.size() - 1);
        Map<String, String> snapMap = new HashMap<>();
        if (snap.isLocalSnapshot()) {
            snapMap.put(LOCAL_COPY, Boolean.TRUE.toString());
        } else {
            snapMap.put(TIMESTAMP, SnapshotUtils.generateSnapshotTimestamp(snap.getTimestamp()));

            snapMap.put(BUILD_NUMBER, Integer.toString(snap.getBuildNumber()));
        }

        xml.createElement(doc, "versioning/snapshot", snapMap);

        for (SnapshotPart snap1 : snaps) {
            snap = snap1;

            // the last one is the most recent.
            final Set<ArtifactPathInfo> infos = infosBySnap.get(snap);
            for (final ArtifactPathInfo pathInfo : infos) {
                snapMap = new HashMap<>();

                final TypeAndClassifier tc = new SimpleTypeAndClassifier(pathInfo.getType(),
                        pathInfo.getClassifier());

                final TypeMapping mapping = typeMapper.lookup(tc);

                final String classifier = mapping == null ? pathInfo.getClassifier() : mapping.getClassifier();
                if (classifier != null && classifier.length() > 0) {
                    snapMap.put(CLASSIFIER, classifier);
                }

                snapMap.put(EXTENSION, mapping == null ? pathInfo.getType() : mapping.getExtension());
                snapMap.put(VALUE, pathInfo.getVersion());
                snapMap.put(UPDATED, lastUpdated);

                xml.createElement(doc, "versioning/snapshotVersions/snapshotVersion", snapMap);
            }
        }

        final String xmlStr = xml.toXML(doc, true);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
        stream.write(xmlStr.getBytes("UTF-8"));
    } catch (final GalleyMavenXMLException e) {
        throw new IndyWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path,
                e.getMessage());
    } catch (final IOException e) {
        throw new IndyWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e,
                metadataFile, e.getMessage());
    } finally {
        closeQuietly(stream);
    }

    return true;
}