public void startElement()

in src/main/java/org/apache/cxf/cwiki/ConfluenceCleanupWriter.java [136:315]


    public void startElement(String uri, String localName, String qName, final Attributes atts)
        throws SAXException {
        AttributesWrapper newAtts = new AttributesWrapper(atts);
        if ("a".equals(localName.toLowerCase())
            || "a".equals(qName.toLowerCase())) {
            String href = atts.getValue("href");
            //Confluence sticks this on links from blog entries, but it's invalid
            newAtts.remove("data-username");
            if (href != null) {
                href = href.trim();
            }
            if (href != null && href.startsWith("/confluence/display/")) {
                String params = "";
                if (href.indexOf('#') != -1) {
                    params = href.substring(href.indexOf('#'));
                    href = href.substring(0, href.indexOf('#'));
                }
                if (href.indexOf('?') != -1) {
                    if (params.length() > 0) {
                        params = href.substring(href.indexOf('?')) + "#" + params;
                    } else {
                        params = href.substring(href.indexOf('?'));
                    }
                    href = href.substring(0, href.indexOf('?'));
                }
                try {
                    String p = findPageWithURL(href);
                    if (p != null) {
                        newAtts.addMapping("href", p + params);
                    } else {
                        if (href.indexOf('~') == -1) {
                            //link to a user page is OK, don't warn about it
                            System.out.println("Could not find page for " + href 
                                               + " linked from " + page.getTitle());
                        }
                        newAtts.addMapping("href", SiteExporter.ROOT + href.substring(11));
                    }
                } catch (Exception e) {
                    throw new SAXException(e);
                }
            } else if (href != null && href.startsWith("/confluence/plugins/")) {
                newAtts.addMapping("href", SiteExporter.ROOT + href.substring(11));
            } else if (href != null && href.contains("/confluence/pages/viewpage.action")) {
                String params = "";
                if (href.indexOf('#') != -1) {
                    params = href.substring(href.indexOf('#'));
                    href = href.substring(0, href.indexOf('#'));
                }
                int idx = href.indexOf("pageId=");
                String id = href.substring(idx + 7);
                try {
                    String location = findPageByID(id);
                    if (location != null) {
                          newAtts.addMapping("href", location + params);
                    } else {
                        System.out.println("Could not find page for id: " + id 
                                           + " linked from " + page.getTitle());
                    }   
                } catch (Exception e) {
                    throw new SAXException(e);
                }
            } else if (href != null && href.contains("/confluence/download/attachments")) {
                href = href.substring(href.lastIndexOf("/"));
                String dirName = page.createFileName();
                dirName = dirName.substring(0, dirName.lastIndexOf(".")) + ".data";

                newAtts.addMapping("href", dirName + href);
            } else if (href != null && href.contains("/confluence/pages/createpage.action")) {
                System.out.println("Adding createpage link for " + href + " from " + page.getTitle());
                newAtts.addMapping("href", SiteExporter.HOST + href);
            } else if (href != null 
                && (href.startsWith("http://")
                    || href.startsWith("https://"))) {
                URL url;
                try {
                    url = new URL(href);
                    if (url.getHost().contains("apache.org")) {
                        newAtts.remove("rel");
                    }
                    if (url.getHost().equals("cxf.apache.org")
                        && "external-link".equals(newAtts.getValue("class"))) {
                        newAtts.remove("class");
                    }
                } catch (MalformedURLException e) {
                    //ignore
                }
            }
        } else if ("img".equals(localName.toLowerCase())
            || "img".equals(qName.toLowerCase())) {
            String href = exporter.stripHost(atts.getValue("src"));
            if ("absmiddle".equalsIgnoreCase(atts.getValue("align"))) {
                newAtts.addMapping("align", "middle");
            }
            String cls = atts.getValue("class");
            if (href != null && href.startsWith("/confluence/images/")) {
                newAtts.addMapping("src", href.replaceFirst("^/confluence/images/", "/images/confluence/"));
            } else if (href != null && href.startsWith("/confluence/download/attachments")) {
                if (cls == null || cls.contains("confluence-embedded-image")) {
                    href = href.substring(0, href.lastIndexOf('?'));
                    href = href.substring(href.lastIndexOf('/'));
                    String dirName = page.createFileName();
                    dirName = dirName.substring(0, dirName.lastIndexOf(".")) + ".data";

                    newAtts.addMapping("src", dirName + href.replaceAll("\\+", "-"));
                } else if (cls.contains("userLogo")) {
                    String name = href;
                    try {
                        name = exporter.loadUserImage(page, href);
                    } catch (Exception ex) {
                        System.out.println("Could not download userLogo " + href 
                                           + " linked from " + page.getTitle());                    
                    }
                    String dirName = page.createFileName();
                    dirName = dirName.substring(0, dirName.lastIndexOf(".")) + ".userimage/";

                    newAtts.addMapping("src", dirName + name);                    
                } else {
                    newAtts.addMapping("src", SiteExporter.HOST + href.replaceAll("\\+", "-"));
                }
            } else if (href != null && href.startsWith("/confluence/download/thumbnails")) {
                String name = href;
                try {
                    name = exporter.loadThumbnail(page, href);
                } catch (Exception ex) {
                    System.out.println("Could not download thumbnail " + href 
                                       + " linked from " + page.getTitle());                    
                }
                String dirName = page.createFileName();
                dirName = dirName.substring(0, dirName.lastIndexOf(".")) + ".thumbs/";

                newAtts.addMapping("src", dirName + name);
            } else if (href != null && href.startsWith("/confluence")) {
                newAtts.addMapping("src", SiteExporter.HOST + href);
            }
        } else if ("th".equals(localName.toLowerCase())
            || "th".equals(qName.toLowerCase())) {
            curTrCount++;
        } else if ("td".equals(localName.toLowerCase())
            || "td".equals(qName.toLowerCase())) {
            curTrCount++;
            if (newAtts.getIndex("nowrap") != -1) {
                //make sure nowrap attribute is set to nowrap per HTML spec
                newAtts.addMapping("nowrap", "nowrap");
            }
        } else if ("tr".equals(localName.toLowerCase())
            || "tr".equals(qName.toLowerCase())) {
            trStack.push(curTrCount);
            curTrCount = 0;
        } else if ("div".equals(localName.toLowerCase())
            || "div".equals(qName.toLowerCase())) {
            String id = atts.getValue("id");
            if ("ConfluenceContent".equals(id)) {
                if (divCls != null) {
                    newAtts.addMapping("class", divCls);
                    newAtts.remove("id");
                }
                if (divId != null) {
                    newAtts.addMapping("id", divId);
                } 
            }
        } else if ("input".equals(localName.toLowerCase())
            || "input".equals(qName.toLowerCase())) {
            String value = atts.getValue("value");
            if (value != null && value.startsWith("/confluence/")) {
                newAtts.addMapping("value", SiteExporter.ROOT + value.substring(11));
            }
        } else if ("pre".equals(localName.toLowerCase())
                   || "pre".equals(qName.toLowerCase())) {
            String cls = atts.getValue("class");
            if ("syntaxhighlighter-pre".equalsIgnoreCase(cls)) {
                String brush = atts.getValue("data-syntaxhighlighter-params");
                if (brush.toLowerCase().startsWith("brush")) {
                    newAtts.remove("data-syntaxhighlighter-params");
                    newAtts.remove("data-theme");
                    newAtts.addMapping("class", brush);
                }
            }
        }
        super.startElement(uri, localName, qName, newAtts);
    }