1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/ProcessPage.java Mon Sep 24 12:39:21 2012 +0200
1.3 @@ -0,0 +1,61 @@
1.4 +package org.apidesign.bck2brwsr.htmlpage;
1.5 +
1.6 +import java.io.IOException;
1.7 +import java.io.InputStream;
1.8 +import java.util.Collections;
1.9 +import java.util.Map;
1.10 +import java.util.Set;
1.11 +import java.util.TreeMap;
1.12 +import javax.xml.parsers.DocumentBuilder;
1.13 +import javax.xml.parsers.DocumentBuilderFactory;
1.14 +import org.w3c.dom.Document;
1.15 +import org.w3c.dom.Element;
1.16 +import org.w3c.dom.Node;
1.17 +import org.w3c.dom.NodeList;
1.18 +
1.19 +class ProcessPage {
1.20 + private final Map<String,String> ids2Elems = new TreeMap<String, String>();
1.21 +
1.22 + public Set<String> ids() {
1.23 + return Collections.unmodifiableSet(ids2Elems.keySet());
1.24 + }
1.25 +
1.26 + public String tagNameForId(String id) {
1.27 + return ids2Elems.get(id);
1.28 + }
1.29 +
1.30 + public static ProcessPage readPage(InputStream is) throws IOException {
1.31 + DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
1.32 + f.setValidating(false);
1.33 + f.setIgnoringComments(true);
1.34 +
1.35 + Document doc = null;
1.36 + try {
1.37 + DocumentBuilder b = f.newDocumentBuilder();
1.38 + doc = b.parse(is);
1.39 + } catch (IOException ex) {
1.40 + throw ex;
1.41 + } catch (Exception e) {
1.42 + throw new IOException(e);
1.43 + }
1.44 + Element root = doc.getDocumentElement();
1.45 +
1.46 + ProcessPage pp = new ProcessPage();
1.47 + pp.seekForIds(root);
1.48 + return pp;
1.49 + }
1.50 +
1.51 + private void seekForIds(Element e) {
1.52 + String val = e.getAttribute("id");
1.53 + if (val != null && !val.isEmpty()) {
1.54 + String prev = ids2Elems.put(val, e.getTagName());
1.55 + }
1.56 + NodeList arr = e.getChildNodes();
1.57 + for (int i = 0; i < arr.getLength(); i++) {
1.58 + final Node n = arr.item(i);
1.59 + if (n instanceof Element) {
1.60 + seekForIds((Element)n);
1.61 + }
1.62 + }
1.63 + }
1.64 +}