Utils.java 11.8 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  javax.jcr.Binary
 *  javax.jcr.ItemExistsException
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.jcr.ValueFactory
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.html.HtmlParser
 */
package com.day.cq.wcm.siteimporter.internal.util;

import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.siteimporter.ImporterContext;
import com.day.cq.wcm.siteimporter.ProgressTracker;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import javax.jcr.Binary;
import javax.jcr.ItemExistsException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.html.HtmlParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Utils {
    private static long idCounter = 0;

    public static javax.jcr.Node createNode(javax.jcr.Node parent, String title, String type, ProgressTracker out) throws RepositoryException {
        return Utils.createNode(parent, title, type, new Hashtable<String, Object>(), out);
    }

    public static javax.jcr.Node createNode(javax.jcr.Node parent, String title, String type, Hashtable<String, Object> properties, ProgressTracker out) throws RepositoryException {
        return Utils.createNode(parent, title, type, properties, true, false, out);
    }

    public static javax.jcr.Node createNode(javax.jcr.Node parent, String title, String type, boolean doSave, boolean ensureName, ProgressTracker out) throws RepositoryException {
        return Utils.createNode(parent, title, type, new Hashtable<String, Object>(), doSave, ensureName, out);
    }

    public static javax.jcr.Node createNode(javax.jcr.Node parent, String title, String type, Hashtable<String, Object> properties, boolean doSave, boolean ensureName, ProgressTracker out) throws RepositoryException {
        if (ensureName) {
            title = JcrUtil.createValidName((String)title);
        }
        if (parent.hasNode(title)) {
            throw new ItemExistsException("Node " + parent.getPath() + " already has child " + title);
        }
        javax.jcr.Node newNode = parent.addNode(title, type);
        Enumeration<String> propertyNames = properties.keys();
        while (propertyNames.hasMoreElements()) {
            String property = propertyNames.nextElement();
            Object value = properties.get(property);
            if (value instanceof Boolean) {
                newNode.setProperty(property, ((Boolean)value).booleanValue());
                continue;
            }
            if (value instanceof Double) {
                newNode.setProperty(property, ((Double)value).doubleValue());
                continue;
            }
            if (value instanceof Long) {
                newNode.setProperty(property, ((Long)value).longValue());
                continue;
            }
            if (value instanceof String) {
                newNode.setProperty(property, (String)value);
                continue;
            }
            if (value instanceof Calendar) {
                newNode.setProperty(property, (Calendar)value);
                continue;
            }
            if (value instanceof javax.jcr.Node) {
                newNode.setProperty(property, (javax.jcr.Node)value);
                continue;
            }
            if (value instanceof InputStream) {
                ValueFactory vf = newNode.getSession().getValueFactory();
                newNode.setProperty(property, vf.createBinary((InputStream)value));
                continue;
            }
            if (value instanceof Value) {
                newNode.setProperty(property, (Value)value);
                continue;
            }
            if (value instanceof ArrayList) {
                String[] listValue = new String[((ArrayList)value).size()];
                ((ArrayList)value).toArray(listValue);
                newNode.setProperty(property, listValue);
                continue;
            }
            out.error("Couldn't attach property " + property + " to node " + newNode.getPath() + " - skipping.");
        }
        if (doSave) {
            parent.getSession().save();
        }
        return newNode;
    }

    public static String readFileNode(String path, ResourceResolver resolver, ProgressTracker out) {
        Resource filePath = resolver.getResource(path + "/jcr:content");
        if (filePath == null) {
            out.error("Couldn't find file content of file " + path);
            return null;
        }
        ValueMap fileContent = (ValueMap)filePath.adaptTo(ValueMap.class);
        return (String)fileContent.get("jcr:data", (Object)"");
    }

    public static javax.jcr.Node createFile(javax.jcr.Node parent, String title, String content, String mimeType, String charset, ProgressTracker out) throws RepositoryException {
        return Utils.createFile(parent, title, content, mimeType, charset, false, out);
    }

    public static javax.jcr.Node createFile(javax.jcr.Node parent, String title, String content, String mimeType, String charset, boolean ensureName, ProgressTracker out) throws RepositoryException {
        ByteArrayInputStream data;
        if (charset != null) {
            try {
                data = new ByteArrayInputStream(content.getBytes(charset));
            }
            catch (UnsupportedEncodingException e) {
                data = new ByteArrayInputStream(content.getBytes());
            }
        } else {
            data = new ByteArrayInputStream(content.getBytes());
        }
        return Utils.createFile(parent, title, data, mimeType, charset, ensureName, out);
    }

    public static javax.jcr.Node createFile(javax.jcr.Node parent, String title, InputStream content, String mimeType, ProgressTracker out) throws RepositoryException {
        return Utils.createFile(parent, title, content, mimeType, null, false, out);
    }

    public static javax.jcr.Node createFile(javax.jcr.Node parent, String title, InputStream content, String mimeType, String charset, ProgressTracker out) throws RepositoryException {
        return Utils.createFile(parent, title, content, mimeType, charset, false, out);
    }

    public static javax.jcr.Node createFile(javax.jcr.Node parent, String title, InputStream content, String mimeType, String charset, boolean ensureName, ProgressTracker out) throws RepositoryException {
        return Utils.createFile(parent, title, content, mimeType, charset, true, ensureName, out);
    }

    public static javax.jcr.Node createFile(javax.jcr.Node parent, String title, InputStream content, String mimeType, String charset, boolean doSave, boolean ensureName, ProgressTracker out) throws RepositoryException {
        javax.jcr.Node fileNode = Utils.createNode(parent, title.trim(), "nt:file", false, ensureName, out);
        Hashtable<String, Object> properties = new Hashtable<String, Object>();
        properties.put("jcr:data", content);
        if (charset != null) {
            properties.put("jcr:encoding", charset);
        }
        properties.put("jcr:mimeType", mimeType);
        properties.put("jcr:lastModified", new GregorianCalendar());
        Utils.createNode(fileNode, "jcr:content", "nt:resource", properties, doSave, false, out);
        return fileNode;
    }

    public static javax.jcr.Node createPath(Resource root, String path, ImporterContext ctx, boolean ensureName) {
        javax.jcr.Node current = (javax.jcr.Node)root.adaptTo(javax.jcr.Node.class);
        if (current != null) {
            String[] steps = path.split("/");
            for (int i = 1; i < steps.length - 1; ++i) {
                if ("".equals(steps[i]) || ".".equals(steps[i])) continue;
                String name = ensureName ? JcrUtil.createValidName((String)steps[i]) : steps[i];
                try {
                    if (!current.hasNode(name)) {
                        current = Utils.createNode(current, name, "sling:Folder", true, ensureName, ctx);
                        continue;
                    }
                    current = current.getNode(name);
                    continue;
                }
                catch (RepositoryException e) {
                    ctx.error("Couldn't create subelement " + steps[i] + " for " + path + ": " + e.getMessage(), (Exception)e);
                    return null;
                }
            }
        }
        return current;
    }

    public static Document parse(String html, String charset, HtmlParser parser, ProgressTracker out) {
        if (charset != null) {
            try {
                return Utils.parse(new ByteArrayInputStream(html.getBytes(charset)), charset, parser, out);
            }
            catch (UnsupportedEncodingException e) {
                return Utils.parse(new ByteArrayInputStream(html.getBytes()), charset, parser, out);
            }
        }
        return Utils.parse(new ByteArrayInputStream(html.getBytes()), charset, parser, out);
    }

    public static Document parse(InputStream html, String charset, HtmlParser parser, ProgressTracker out) {
        Document doc;
        try {
            doc = parser.parse(null, html, charset);
        }
        catch (IOException e) {
            out.error("Couldn't parse original HTML: " + e.getMessage(), e);
            return null;
        }
        return doc;
    }

    public static void removeInsertIds(Element el) {
        if (el.hasAttribute("componentinsertid")) {
            el.removeAttribute("componentinsertid");
        }
        Utils.removeInsertIds(el.getChildNodes());
    }

    public static void removeInsertIds(NodeList nodes) {
        for (int i = 0; i < nodes.getLength(); ++i) {
            Node node = nodes.item(i);
            if (node.getNodeType() != 1) continue;
            Element el = (Element)node;
            if (el.hasAttribute("componentinsertid")) {
                el.removeAttribute("componentinsertid");
            }
            Utils.removeInsertIds(node.getChildNodes());
        }
    }

    public static void injectInsertIds(NodeList nodes) {
        for (int i = 0; i < nodes.getLength(); ++i) {
            Node node = nodes.item(i);
            if (node.getNodeType() != 1) continue;
            Element el = (Element)node;
            el.setAttribute("componentInsertId", String.valueOf(++idCounter));
            Utils.injectInsertIds(node.getChildNodes());
        }
    }

    public static String stripTag(String source, String tagName, boolean hasContent, String mustContain) {
        int curpos = 0;
        while (source.toUpperCase().lastIndexOf("<" + tagName.toUpperCase()) > -1 && curpos < source.toUpperCase().lastIndexOf("<" + tagName.toUpperCase())) {
            int next = source.toUpperCase().indexOf("<" + tagName.toUpperCase(), curpos);
            int end = source.indexOf(">", next);
            if (hasContent) {
                end = source.toUpperCase().indexOf("</" + tagName.toUpperCase());
                end = source.indexOf(">", end);
            }
            if ("".equals(mustContain) || source.substring(next, end).toUpperCase().indexOf(mustContain.toUpperCase()) > 0) {
                source = source.substring(0, next) + source.substring(end + 1);
                continue;
            }
            curpos = Math.max(next + 1, end);
        }
        return source;
    }
}