jaroslav@26: /** jaroslav@26: * Java 4 Browser Bytecode Translator jaroslav@26: * Copyright (C) 2012-2012 Jaroslav Tulach jaroslav@26: * jaroslav@26: * This program is free software: you can redistribute it and/or modify jaroslav@26: * it under the terms of the GNU General Public License as published by jaroslav@26: * the Free Software Foundation, version 2 of the License. jaroslav@26: * jaroslav@26: * This program is distributed in the hope that it will be useful, jaroslav@26: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@26: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@26: * GNU General Public License for more details. jaroslav@26: * jaroslav@26: * You should have received a copy of the GNU General Public License jaroslav@26: * along with this program. Look for COPYING file in the top folder. jaroslav@26: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@26: */ jaroslav@25: package org.apidesign.bck2brwsr.htmlpage; jaroslav@25: jaroslav@25: import java.io.IOException; jaroslav@25: import java.io.InputStream; jaroslav@25: import java.util.Collections; jaroslav@25: import java.util.Map; jaroslav@25: import java.util.Set; jaroslav@25: import java.util.TreeMap; jaroslav@25: import javax.xml.parsers.DocumentBuilder; jaroslav@25: import javax.xml.parsers.DocumentBuilderFactory; jaroslav@25: import org.w3c.dom.Document; jaroslav@25: import org.w3c.dom.Element; jaroslav@25: import org.w3c.dom.Node; jaroslav@25: import org.w3c.dom.NodeList; jaroslav@25: jaroslav@25: class ProcessPage { jaroslav@25: private final Map ids2Elems = new TreeMap(); jaroslav@25: jaroslav@25: public Set ids() { jaroslav@25: return Collections.unmodifiableSet(ids2Elems.keySet()); jaroslav@25: } jaroslav@25: jaroslav@25: public String tagNameForId(String id) { jaroslav@25: return ids2Elems.get(id); jaroslav@25: } jaroslav@25: jaroslav@25: public static ProcessPage readPage(InputStream is) throws IOException { jaroslav@25: DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); jaroslav@25: f.setValidating(false); jaroslav@25: f.setIgnoringComments(true); jaroslav@25: jaroslav@25: Document doc = null; jaroslav@25: try { jaroslav@25: DocumentBuilder b = f.newDocumentBuilder(); jaroslav@25: doc = b.parse(is); jaroslav@25: } catch (IOException ex) { jaroslav@25: throw ex; jaroslav@25: } catch (Exception e) { jaroslav@25: throw new IOException(e); jaroslav@25: } jaroslav@25: Element root = doc.getDocumentElement(); jaroslav@25: jaroslav@25: ProcessPage pp = new ProcessPage(); jaroslav@25: pp.seekForIds(root); jaroslav@25: return pp; jaroslav@25: } jaroslav@25: jaroslav@25: private void seekForIds(Element e) { jaroslav@25: String val = e.getAttribute("id"); jaroslav@25: if (val != null && !val.isEmpty()) { jaroslav@25: String prev = ids2Elems.put(val, e.getTagName()); jaroslav@25: } jaroslav@25: NodeList arr = e.getChildNodes(); jaroslav@25: for (int i = 0; i < arr.getLength(); i++) { jaroslav@25: final Node n = arr.item(i); jaroslav@25: if (n instanceof Element) { jaroslav@25: seekForIds((Element)n); jaroslav@25: } jaroslav@25: } jaroslav@25: } jaroslav@25: }