use of org.h2.build.doc.XMLParser in project h2database by h2database.
the class PrepareTranslation method extract.
private static String extract(String documentName, File f, String target) throws Exception {
String xml = IOUtils.readStringAndClose(new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8), -1);
// the template contains ${} instead of text
StringBuilder template = new StringBuilder(xml.length());
int id = 0;
SortedProperties prop = new SortedProperties();
XMLParser parser = new XMLParser(xml);
StringBuilder buff = new StringBuilder();
Stack<String> stack = new Stack<>();
String tag = "";
boolean ignoreEnd = false;
String nextKey = "";
// for debugging
boolean templateIsCopy = false;
while (true) {
int event = parser.next();
if (event == XMLParser.END_DOCUMENT) {
break;
} else if (event == XMLParser.CHARACTERS) {
String s = parser.getText();
if (s.trim().length() == 0) {
if (buff.length() > 0) {
buff.append(s);
} else {
template.append(s);
}
} else if ("p".equals(tag) || "li".equals(tag) || "a".equals(tag) || "td".equals(tag) || "th".equals(tag) || "h1".equals(tag) || "h2".equals(tag) || "h3".equals(tag) || "h4".equals(tag) || "body".equals(tag) || "b".equals(tag) || "code".equals(tag) || "form".equals(tag) || "span".equals(tag) || "em".equals(tag) || "div".equals(tag) || "strong".equals(tag) || "label".equals(tag)) {
if (buff.length() == 0) {
nextKey = documentName + "_" + (1000 + id++) + "_" + tag;
template.append(getSpace(s, true));
} else if (templateIsCopy) {
buff.append(getSpace(s, true));
}
buff.append(s);
} else if ("pre".equals(tag) || "title".equals(tag) || "script".equals(tag) || "style".equals(tag)) {
// ignore, don't translate
template.append(s);
} else {
System.out.println(f.getName() + " invalid wrapper tag for text: " + tag + " text: " + s);
System.out.println(parser.getRemaining());
throw new Exception();
}
} else if (event == XMLParser.START_ELEMENT) {
stack.add(tag);
String name = parser.getName();
if ("code".equals(name) || "a".equals(name) || "b".equals(name) || "span".equals(name)) {
// keep tags if wrapped, but not if this is the wrapper
if (buff.length() > 0) {
buff.append(parser.getToken());
ignoreEnd = false;
} else {
ignoreEnd = true;
template.append(parser.getToken());
}
} else if ("p".equals(tag) || "li".equals(tag) || "td".equals(tag) || "th".equals(tag) || "h1".equals(tag) || "h2".equals(tag) || "h3".equals(tag) || "h4".equals(tag) || "body".equals(tag) || "form".equals(tag)) {
if (buff.length() > 0) {
if (templateIsCopy) {
template.append(buff.toString());
} else {
template.append("${" + nextKey + "}");
}
add(prop, nextKey, buff);
}
template.append(parser.getToken());
} else {
template.append(parser.getToken());
}
tag = name;
} else if (event == XMLParser.END_ELEMENT) {
String name = parser.getName();
if ("code".equals(name) || "a".equals(name) || "b".equals(name) || "span".equals(name) || "em".equals(name) || "strong".equals(name)) {
if (ignoreEnd) {
if (buff.length() > 0) {
if (templateIsCopy) {
template.append(buff.toString());
} else {
template.append("${" + nextKey + "}");
}
add(prop, nextKey, buff);
}
template.append(parser.getToken());
} else {
if (buff.length() > 0) {
buff.append(parser.getToken());
}
}
} else {
if (buff.length() > 0) {
if (templateIsCopy) {
template.append(buff.toString());
} else {
template.append("${" + nextKey + "}");
}
add(prop, nextKey, buff);
}
template.append(parser.getToken());
}
tag = stack.pop();
} else if (event == XMLParser.DTD) {
template.append(parser.getToken());
} else if (event == XMLParser.COMMENT) {
template.append(parser.getToken());
} else {
int eventType = parser.getEventType();
throw new Exception("Unexpected event " + eventType + " at " + parser.getRemaining());
}
// if(!xml.startsWith(template.toString())) {
// System.out.println(nextKey);
// System.out.println(template.substring(template.length()-60)
// +";");
// System.out.println(xml.substring(template.length()-60,
// template.length()));
// System.out.println(template.substring(template.length()-55)
// +";");
// System.out.println(xml.substring(template.length()-55,
// template.length()));
// break;
// }
}
new File(target).mkdirs();
String propFileName = target + "/_docs_" + MAIN_LANGUAGE + ".properties";
Properties old = load(propFileName, false);
prop.putAll(old);
store(prop, propFileName, false);
String t = template.toString();
if (templateIsCopy && !t.equals(xml)) {
for (int i = 0; i < Math.min(t.length(), xml.length()); i++) {
if (t.charAt(i) != xml.charAt(i)) {
int start = Math.max(0, i - 30), end = Math.min(i + 30, xml.length());
t = t.substring(start, end);
xml = xml.substring(start, end);
}
}
System.out.println("xml--------------------------------------------------: ");
System.out.println(xml);
System.out.println("t---------------------------------------------------: ");
System.out.println(t);
System.exit(1);
}
return t;
}
use of org.h2.build.doc.XMLParser in project h2database by h2database.
the class Build method getLocalMavenDir.
@Override
protected String getLocalMavenDir() {
String userHome = System.getProperty("user.home", "");
File file = new File(userHome, ".m2/settings.xml");
if (!file.exists()) {
return super.getLocalMavenDir();
}
XMLParser p = new XMLParser(new String(BuildBase.readFile(file)));
HashMap<String, String> prop = new HashMap<>();
for (String name = ""; p.hasNext(); ) {
int event = p.next();
if (event == XMLParser.START_ELEMENT) {
name += "/" + p.getName();
} else if (event == XMLParser.END_ELEMENT) {
name = name.substring(0, name.lastIndexOf('/'));
} else if (event == XMLParser.CHARACTERS) {
String text = p.getText().trim();
if (text.length() > 0) {
prop.put(name, text);
}
}
}
String local = prop.get("/settings/localRepository");
if (local == null) {
local = "${user.home}/.m2/repository";
}
local = replaceAll(local, "${user.home}", userHome);
return local;
}
use of org.h2.build.doc.XMLParser in project h2database by h2database.
the class ResourceDoclet method addResource.
private void addResource(String key, Doc doc) {
if (!isResource(doc)) {
return;
}
String xhtml = doc.commentText();
XMLParser p = new XMLParser(xhtml);
StringBuilder buff = new StringBuilder();
int column = 0;
int firstColumnSize = 0;
boolean inColumn = false;
while (p.hasNext()) {
String s;
switch(p.next()) {
case XMLParser.END_ELEMENT:
s = p.getName();
if ("p".equals(s) || "tr".equals(s) || "br".equals(s)) {
buff.append('\n');
}
break;
case XMLParser.START_ELEMENT:
s = p.getName();
if ("table".equals(s)) {
buff.append('\n');
} else if ("tr".equals(s)) {
column = 0;
} else if ("td".equals(s)) {
inColumn = true;
column++;
if (column == 2) {
buff.append('\t');
}
}
break;
case XMLParser.CHARACTERS:
s = HtmlConverter.convertHtmlToString(p.getText().trim());
if (inColumn && column == 1) {
firstColumnSize = Math.max(s.length(), firstColumnSize);
}
buff.append(s);
break;
}
}
for (int i = 0; i < buff.length(); i++) {
if (buff.charAt(i) == '\t') {
buff.deleteCharAt(i);
int length = i - buff.lastIndexOf("\n", i - 1);
for (int k = length; k < firstColumnSize + 3; k++) {
buff.insert(i, ' ');
}
}
}
String text = buff.toString().trim();
resources.setProperty(key, text);
}
Aggregations