htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/ProcessPage.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 24 Sep 2012 15:06:43 +0200
changeset 26 03e4aaa4ef3d
parent 25 e214f04abb98
child 106 346633cd13d6
permissions -rw-r--r--
Can extract 'id' elements from a page and generate appropriate HTML page
jaroslav@26
     1
/**
jaroslav@26
     2
 * Java 4 Browser Bytecode Translator
jaroslav@26
     3
 * Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@26
     4
 *
jaroslav@26
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@26
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@26
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@26
     8
 *
jaroslav@26
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@26
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@26
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@26
    12
 * GNU General Public License for more details.
jaroslav@26
    13
 *
jaroslav@26
    14
 * You should have received a copy of the GNU General Public License
jaroslav@26
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@26
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@26
    17
 */
jaroslav@25
    18
package org.apidesign.bck2brwsr.htmlpage;
jaroslav@25
    19
jaroslav@25
    20
import java.io.IOException;
jaroslav@25
    21
import java.io.InputStream;
jaroslav@25
    22
import java.util.Collections;
jaroslav@25
    23
import java.util.Map;
jaroslav@25
    24
import java.util.Set;
jaroslav@25
    25
import java.util.TreeMap;
jaroslav@25
    26
import javax.xml.parsers.DocumentBuilder;
jaroslav@25
    27
import javax.xml.parsers.DocumentBuilderFactory;
jaroslav@25
    28
import org.w3c.dom.Document;
jaroslav@25
    29
import org.w3c.dom.Element;
jaroslav@25
    30
import org.w3c.dom.Node;
jaroslav@25
    31
import org.w3c.dom.NodeList;
jaroslav@25
    32
jaroslav@25
    33
class ProcessPage {
jaroslav@25
    34
    private final Map<String,String> ids2Elems = new TreeMap<String, String>();
jaroslav@25
    35
    
jaroslav@25
    36
    public Set<String> ids() {
jaroslav@25
    37
        return Collections.unmodifiableSet(ids2Elems.keySet());
jaroslav@25
    38
    }
jaroslav@25
    39
    
jaroslav@25
    40
    public String tagNameForId(String id) {
jaroslav@25
    41
        return ids2Elems.get(id);
jaroslav@25
    42
    }
jaroslav@25
    43
    
jaroslav@25
    44
    public static ProcessPage readPage(InputStream is) throws IOException {
jaroslav@25
    45
        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
jaroslav@25
    46
        f.setValidating(false);
jaroslav@25
    47
        f.setIgnoringComments(true);
jaroslav@25
    48
        
jaroslav@25
    49
        Document doc = null;
jaroslav@25
    50
        try {
jaroslav@25
    51
            DocumentBuilder b = f.newDocumentBuilder();
jaroslav@25
    52
            doc = b.parse(is);
jaroslav@25
    53
        } catch (IOException ex) {
jaroslav@25
    54
            throw ex;
jaroslav@25
    55
        } catch (Exception e) {
jaroslav@25
    56
            throw new IOException(e);
jaroslav@25
    57
        }
jaroslav@25
    58
        Element root = doc.getDocumentElement();
jaroslav@25
    59
        
jaroslav@25
    60
        ProcessPage pp = new ProcessPage();
jaroslav@25
    61
        pp.seekForIds(root);
jaroslav@25
    62
        return pp;
jaroslav@25
    63
    }
jaroslav@25
    64
jaroslav@25
    65
    private void seekForIds(Element e) {
jaroslav@25
    66
        String val = e.getAttribute("id");
jaroslav@25
    67
        if (val != null && !val.isEmpty()) {
jaroslav@25
    68
            String prev = ids2Elems.put(val, e.getTagName());
jaroslav@25
    69
        }
jaroslav@25
    70
        NodeList arr = e.getChildNodes();
jaroslav@25
    71
        for (int i = 0; i < arr.getLength(); i++) {
jaroslav@25
    72
            final Node n = arr.item(i);
jaroslav@25
    73
            if (n instanceof Element) {
jaroslav@25
    74
                seekForIds((Element)n);
jaroslav@25
    75
            }
jaroslav@25
    76
        }
jaroslav@25
    77
    }
jaroslav@25
    78
}