protected Node importJcrXml()

in src/main/java/org/apache/sling/jcr/contentloader/internal/JcrXmlImporter.java [53:95]


    protected Node importJcrXml(Node parent, String name, InputStream contentStream, boolean replace) throws IOException {
        try {
            final String nodeName = (name.endsWith(EXT_JCR_XML)) ? name.substring(0, name.length() - EXT_JCR_XML.length()) : name;

            // ensure the name is not empty
            if (nodeName.length() == 0) {
                throw new IOException("Node name must not be empty (or extension only)");
            }

            // check for existence/replacement
            if (parent.hasNode(nodeName)) {
                Node existingNode = parent.getNode(nodeName);
                if (replace) {
                    logger.debug("importJcrXml: Removing existing node at {}", nodeName);
                    existingNode.remove();
                } else {
                    logger.debug("importJcrXml: Node {} for XML already exists, nothing to to", nodeName);
                    return existingNode;
                }
            }

            final int uuidBehavior;
            if (replace) {
                uuidBehavior = IMPORT_UUID_COLLISION_REPLACE_EXISTING;
            } else {
                uuidBehavior = IMPORT_UUID_CREATE_NEW;
            }

            Session session = parent.getSession();
            session.importXML(parent.getPath(), contentStream, uuidBehavior);

            // additionally check whether the expected child node exists
            return (parent.hasNode(nodeName)) ? parent.getNode(nodeName) : null;
        } catch (InvalidSerializedDataException isde) {
            // the xml might not be System or Document View export, fall back to old-style XML reading
            logger.info("importJcrXml: XML does not seem to be system or document view", isde);
            return null;
        } catch (RepositoryException re) {
            // any other repository related issue...
            logger.info("importJcrXml: Repository issue loading XML", re);
            return null;
        }
    }