javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ProcessPage.java
changeset 140 590958fcb7d7
parent 106 346633cd13d6
child 1505 706b66d8c481
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ProcessPage.java	Fri Nov 09 11:47:00 2012 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.htmlpage;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.io.InputStream;
    1.25 +import java.util.Collections;
    1.26 +import java.util.Map;
    1.27 +import java.util.Set;
    1.28 +import java.util.TreeMap;
    1.29 +import javax.xml.parsers.DocumentBuilder;
    1.30 +import javax.xml.parsers.DocumentBuilderFactory;
    1.31 +import org.w3c.dom.Document;
    1.32 +import org.w3c.dom.Element;
    1.33 +import org.w3c.dom.Node;
    1.34 +import org.w3c.dom.NodeList;
    1.35 +
    1.36 +class ProcessPage {
    1.37 +    private final Map<String,String> ids2Elems = new TreeMap<String, String>();
    1.38 +    
    1.39 +    public Set<String> ids() {
    1.40 +        return Collections.unmodifiableSet(ids2Elems.keySet());
    1.41 +    }
    1.42 +    
    1.43 +    public String tagNameForId(String id) {
    1.44 +        return ids2Elems.get(id);
    1.45 +    }
    1.46 +    
    1.47 +    public static ProcessPage readPage(InputStream is) throws IOException {
    1.48 +        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    1.49 +        f.setValidating(false);
    1.50 +        f.setIgnoringComments(true);
    1.51 +        
    1.52 +        Document doc = null;
    1.53 +        try {
    1.54 +            DocumentBuilder b = f.newDocumentBuilder();
    1.55 +            doc = b.parse(is);
    1.56 +        } catch (IOException ex) {
    1.57 +            throw ex;
    1.58 +        } catch (Exception e) {
    1.59 +            throw new IOException(e);
    1.60 +        }
    1.61 +        Element root = doc.getDocumentElement();
    1.62 +        
    1.63 +        ProcessPage pp = new ProcessPage();
    1.64 +        pp.seekForIds(root);
    1.65 +        return pp;
    1.66 +    }
    1.67 +
    1.68 +    private void seekForIds(Element e) {
    1.69 +        String val = e.getAttribute("id");
    1.70 +        if (val != null && !val.isEmpty()) {
    1.71 +            String prev = ids2Elems.put(val, e.getTagName());
    1.72 +        }
    1.73 +        NodeList arr = e.getChildNodes();
    1.74 +        for (int i = 0; i < arr.getLength(); i++) {
    1.75 +            final Node n = arr.item(i);
    1.76 +            if (n instanceof Element) {
    1.77 +                seekForIds((Element)n);
    1.78 +            }
    1.79 +        }
    1.80 +    }
    1.81 +}