org.magicwerk.brownies.html.content.HtmlReload Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2017 by Thomas Mauch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.magicwerk.brownies.html.content;
import org.jdom2.Element;
import org.magicwerk.brownies.collections.IList;
import org.magicwerk.brownies.core.ObjectTools;
import org.magicwerk.brownies.core.files.FileTools;
import org.magicwerk.brownies.core.files.PathTools;
import org.magicwerk.brownies.html.HtmlElement;
import org.magicwerk.brownies.jdom.JdomTools;
import org.magicwerk.brownies.jdom.JdomXpath;
/**
* Visualizes CAR as HTML.
*
* @author Thomas Mauch
* @version $Id$
*/
public class HtmlReload {
public static class ReloadHtml {
Element doc;
Element data;
String reloadFile;
ReloadHtml(HtmlElement content, String reloadFile) {
doc = content.getElement();
this.reloadFile = reloadFile;
}
void makeIdReloadable(String id, String relXPath) {
String reloadId = "reload-" + id;
//String xpath = ".//*[@id='" + id + "']";
//Element docElem2 = JdomTools.selectSingleNode(doc, xpath, Element.class);
Element docElem2 = JdomTools.selectFirstElement(doc, e -> id.equals(e.getAttributeValue("id")));
String text = JdomTools.getText(docElem2);
Element docElem = JdomXpath.selectSingleNode(docElem2, relXPath, Element.class);
Element dataElem = docElem.clone();
// Fix data
dataElem.setAttribute("id", reloadId);
dataElem.detach();
data = dataElem;
// Fix doc
Element reloadElem = new Element("div");
reloadElem.setAttribute("id", id);
reloadElem.setAttribute("class", "reload");
String reloadLink = reloadFile + "#" + reloadId;
reloadElem.setAttribute("data-reload", reloadLink);
JdomTools.addText(reloadElem, text);
JdomTools.replace(docElem, reloadElem);
}
}
public static final String RELOAD_MARKER = "data-reload-marker";
/**
* Create a separate file for all elements marked with attribute "data-reload-marker".
* The element will be replaced with dummy elements having class="reload" and data-reload="{url}" pointing to the file to reload.
* The data file will have name "{base}-data-{id}.html" with htmlFile being "{base}.html"
*
* @param htmlFile name of main HTML file (used as base for data files)
* @param content full HTML content (will be modified)
*/
public static void writeReloadFiles(String htmlFile, HtmlElement content) {
// Get all ids marked
IList ids = JdomTools.selectElements(content.getElement(),
e -> ObjectTools.equals(e.getAttributeValue(RELOAD_MARKER), "true"))
.map(e -> e.getAttributeValue("id"));
for (String id : ids) {
String dataFile = PathTools.getNameBase(htmlFile) + "-data-" + id + "." + PathTools.getNameSuffix(htmlFile);
ReloadHtml rh = new ReloadHtml(content, PathTools.getName(dataFile));
rh.makeIdReloadable(id, "../..");
//HtmlBlock body = new HtmlBlock(null, HtmlConst.ELEM_BODY);
//body.getElement().addContent(content.getElement());
boolean pretty = true;
String dataOutput = JdomTools.print(rh.data, pretty);
FileTools.writeFile().setCreateDirs(true).setFile(dataFile).setText(dataOutput).write();
}
}
}