Search in sources :

Example 1 with AlignmentType

use of com.pratilipi.data.type.PratilipiContentDoc.AlignmentType in project pratilipi by Pratilipi.

the class PratilipiDocUtil method _createPageletList.

private static List<Object[]> _createPageletList(Pratilipi pratilipi, Node node) throws UnexpectedServerException {
    List<Object[]> pageletList = new LinkedList<>();
    Object[] currPagelet = null;
    for (Node childNode : node.childNodes()) {
        if (childNode.nodeName().equals("body") || childNode.nodeName().equals("div") || childNode.nodeName().equals("p")) {
            currPagelet = null;
            List<Object[]> pList = _createPageletList(pratilipi, childNode);
            if (pList.size() == 0) {
                pageletList.add(new Object[] { PratilipiContentDoc.PageletType.HTML, "<br/>", null });
            } else {
                AlignmentType alignment = _getAlignment(childNode);
                if (alignment != null)
                    for (Object[] pagelet : pList) if (pagelet[2] == null && (pagelet[0] == PratilipiContentDoc.PageletType.TEXT || pagelet[0] == PratilipiContentDoc.PageletType.HTML))
                        pagelet[2] = alignment;
                pageletList.addAll(pList);
            }
        } else if (childNode.nodeName().equals("h1") || childNode.nodeName().equals("h2")) {
            String text = _extractText(childNode);
            if (text == null)
                continue;
            if (currPagelet != null && currPagelet[0] == PratilipiContentDoc.PageletType.HEAD) {
                currPagelet[1] = currPagelet[1] + " - " + text;
            } else {
                currPagelet = new Object[] { PratilipiContentDoc.PageletType.HEAD, text, null };
                pageletList.add(currPagelet);
            }
        } else if (childNode.nodeName().equals("img")) {
            currPagelet = null;
            BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
            BlobEntry blobEntry = null;
            String imageUrl = childNode.attr("src");
            String imageName = null;
            if (imageUrl.indexOf("name=") != -1) {
                imageName = imageUrl.substring(imageUrl.indexOf("name=") + 5);
                if (imageName.indexOf('&') != -1)
                    imageName = imageName.substring(0, imageName.indexOf('&'));
                imageName = imageName.replace("%20", " ");
                String fileName = _createImageFullName(pratilipi.getId(), imageName);
                blobEntry = blobAccessor.getBlob(fileName);
                if (blobEntry == null) {
                    // Copying from old resource location
                    blobEntry = blobAccessor.getBlob("pratilipi-resource/" + pratilipi.getId() + "/" + imageName);
                    if (blobEntry != null) {
                        blobEntry.setName(fileName);
                        blobAccessor.createOrUpdateBlob(blobEntry);
                    }
                }
                if (blobEntry == null && imageUrl.indexOf("pratilipiId=") != -1) {
                    // Copying from old resource location of another Pratilipi
                    String pratilipiIdStr = imageUrl.substring(imageUrl.indexOf("pratilipiId=") + 12);
                    if (pratilipiIdStr.indexOf('&') != -1)
                        pratilipiIdStr = pratilipiIdStr.substring(0, pratilipiIdStr.indexOf('&'));
                    blobEntry = blobAccessor.getBlob("pratilipi-resource/" + pratilipiIdStr + "/" + imageName);
                    if (blobEntry != null) {
                        blobEntry.setName(fileName);
                        blobAccessor.createOrUpdateBlob(blobEntry);
                    }
                }
                if (blobEntry == null)
                    continue;
            } else if (imageUrl.startsWith("http")) {
                imageName = imageUrl.replaceAll("[:/.?=&+]+", "_");
                String fileName = _createImageFullName(pratilipi.getId(), imageName);
                blobEntry = blobAccessor.getBlob(fileName);
                if (blobEntry == null) {
                    blobEntry = HttpUtil.doGet(imageUrl);
                    if (!blobEntry.getMimeType().startsWith("image/"))
                        continue;
                    blobEntry.setName(fileName);
                    blobAccessor.createOrUpdateBlob(blobEntry);
                }
            } else if (imageUrl.startsWith("data:") && imageUrl.indexOf("base64") != -1) {
                imageName = UUID.randomUUID().toString();
                String mimeType = imageUrl.substring(5, imageUrl.indexOf(';'));
                String base64String = imageUrl.substring(imageUrl.indexOf("base64,") + 7);
                blobEntry = blobAccessor.newBlob(_createImageFullName(pratilipi.getId(), imageName), Base64.decodeBase64(base64String), mimeType);
                blobAccessor.createOrUpdateBlob(blobEntry);
            } else if (imageUrl.startsWith("file:///") || imageUrl.startsWith("C:")) {
                continue;
            }
            JsonObject imgData = new JsonObject();
            imgData.addProperty("name", imageName);
            imgData.addProperty("height", ImageUtil.getHeight(blobEntry));
            imgData.addProperty("width", ImageUtil.getWidth(blobEntry));
            pageletList.add(new Object[] { PratilipiContentDoc.PageletType.IMAGE, imgData, null });
        } else if (childNode.nodeName().equals("br")) {
            if (currPagelet != null && currPagelet[0] == PratilipiContentDoc.PageletType.HTML)
                currPagelet[1] = currPagelet[1] + "<br/>";
        } else {
            String text = _extractText(childNode);
            if (text == null)
                continue;
            if (childNode.nodeName().equals("b") || childNode.nodeName().equals("strong") || childNode.nodeName().equals("h3") || childNode.nodeName().equals("h4") || childNode.nodeName().equals("h5") || childNode.nodeName().equals("h6"))
                text = "<b>" + text + "</b>";
            if (currPagelet == null || currPagelet[0] != PratilipiContentDoc.PageletType.HTML) {
                currPagelet = new Object[] { PratilipiContentDoc.PageletType.HTML, text, null };
                pageletList.add(currPagelet);
            } else {
                currPagelet[1] = currPagelet[1] + " " + text;
            }
        }
    }
    return pageletList;
}
Also used : AlignmentType(com.pratilipi.data.type.PratilipiContentDoc.AlignmentType) Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode) BlobEntry(com.pratilipi.data.type.BlobEntry) BlobAccessor(com.pratilipi.data.BlobAccessor) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) LinkedList(java.util.LinkedList)

Aggregations

JsonObject (com.google.gson.JsonObject)1 BlobAccessor (com.pratilipi.data.BlobAccessor)1 BlobEntry (com.pratilipi.data.type.BlobEntry)1 AlignmentType (com.pratilipi.data.type.PratilipiContentDoc.AlignmentType)1 LinkedList (java.util.LinkedList)1 Node (org.jsoup.nodes.Node)1 TextNode (org.jsoup.nodes.TextNode)1