List of usage examples for org.dom4j Element appendContent
void appendContent(Branch branch);
From source file:tokyo.northside.jrst.JRSTReader.java
License:Open Source License
/** * <pre>/* ww w . j av a 2 s . co m*/ * | line block * | * | indent * </pre> * * @param lexer * @param item * @return Element * @throws Exception */ private Element composeLineBlock(JRSTLexer lexer, Element item) throws Exception { Element result = null; result = DocumentHelper.createElement(LINE_BLOCK); List<Element> lines = (List<Element>) item.selectNodes(LINE); int[] levels = new int[lines.size()]; int cnt = 0; for (Element l : lines) { levels[cnt] = Integer.parseInt(l.attributeValue(LEVEL)); cnt++; } cnt = 0; boolean[] lineDone = new boolean[lines.size()]; for (int i = 0; i < lineDone.length; i++) { lineDone[i] = false; } for (Element l : lines) { if (levels[cnt] == 0) { result.addElement(LINE).addAttribute(ATTR_INLINE, TRUE).setText(l.getText()); } else { if (!lineDone[cnt]) { Element newItem = DocumentHelper.createElement(LINE_BLOCK); Boolean done = false; for (int i = cnt; i < lines.size() && !done; i++) { if (levels[i] > 0) { Element eLine = newItem.addElement(LINE); eLine.addAttribute(LEVEL, "" + (levels[i] - 1)); eLine.setText(lines.get(i).getText()); lineDone[i] = true; } else { done = true; } } Element eLineBlock = result.addElement(LINE_BLOCK); // Appel recursif eLineBlock.appendContent(composeLineBlock(lexer, newItem)); } } cnt++; } return result; }
From source file:tokyo.northside.jrst.JRSTReader.java
License:Open Source License
/** * <pre>//from www. j av a2s. c om * As a great paleontologist once said, * * This theory, that is mine, is mine. * * -- Anne Elk (Miss) * </pre> * * @param item * @return Element * @throws Exception * */ private Element composeBlockQuote(Element item) throws Exception { Element result = null; result = DocumentHelper.createElement(BLOCK_QUOTE); String text = item.getText(); Document doc = newJRSTReader(new StringReader(text)); result.appendContent(doc.getRootElement()); String sAttribution = item.attributeValue(ATTRIBUTION); if (sAttribution != null) { Element attribution = result.addElement(ATTRIBUTION); attribution.setText(sAttribution); attribution.addAttribute(ATTR_INLINE, TRUE); } return result; }
From source file:tokyo.northside.jrst.JRSTReader.java
License:Open Source License
/** * <pre>//from w w w.j a v a 2s . c o m * .. admonition:: And, by the way... * * You can make up your own admonition too. * </pre> * * @param item * @return Element * @throws Exception * */ private Element composeAdmonition(Element item) throws Exception { Element result = null; if (item.attributeValue(TYPE).equalsIgnoreCase(ADMONITION)) { result = DocumentHelper.createElement(ADMONITION); String title = item.attributeValue(TITLE); String admonitionClass = "admonition_" + title; admonitionClass = admonitionClass.toLowerCase().replaceAll("\\p{Punct}", ""); admonitionClass = admonitionClass.replace(' ', '-'); admonitionClass = admonitionClass.replace('\n', '-'); result.addAttribute(CLASS, admonitionClass); result.addElement(TITLE).addAttribute(ATTR_INLINE, TRUE).setText(title.trim()); } else { result = DocumentHelper.createElement(item.attributeValue(TYPE).toLowerCase()); } String text = item.getText(); Document doc = newJRSTReader(new StringReader(text)); result.appendContent(doc.getRootElement()); return result; }
From source file:tokyo.northside.jrst.JRSTReader.java
License:Open Source License
/** * <p>/* www .ja va 2 s. c o m*/ * Complexe Table * </p> * * <pre> * +------------------------+------------+---------------------+ * | body row 3 | Cells may | - Table cells | * +------------------------+ span rows. | - contain | * | body row 4 | | - body elements. | * +------------------------+------------+---------------------+ * </pre> * * <p> * And simple table * </p> * * <pre> * ===== ===== ====== * Inputs Output * ============ ====== * A B A or B * ------------ ------ * A B A or B * ===== ===== ====== * </pre> * * @param item * @return Element * */ private Element composeTable(Element item) throws Exception { Element result = DocumentHelper.createElement(TABLE); int tableWidth = Integer.parseInt(item.attributeValue(JRSTLexer.TABLE_WIDTH)); TreeSet<Integer> beginCellList = new TreeSet<Integer>(); for (Element cell : (List<Element>) item.selectNodes(JRSTLexer.ROW + "/" + JRSTLexer.CELL)) { Integer begin = Integer.valueOf(cell.attributeValue(JRSTLexer.CELL_INDEX_START)); beginCellList.add(begin); } int[] beginCell = new int[beginCellList.size() + 1]; // + 1 to put // table width // to simulate // new cell int[] lengthCell = new int[beginCellList.size()]; int cellNumber = 0; for (int b : beginCellList) { beginCell[cellNumber] = b; if (cellNumber > 0) { lengthCell[cellNumber - 1] = beginCell[cellNumber] - beginCell[cellNumber - 1]; } cellNumber++; } beginCell[cellNumber] = tableWidth; lengthCell[cellNumber - 1] = beginCell[cellNumber] - beginCell[cellNumber - 1]; Element tgroup = result.addElement(TGROUP).addAttribute("cols", String.valueOf(cellNumber)); for (int width : lengthCell) { tgroup.addElement(COLSPEC).addAttribute("colwidth", String.valueOf(width)); } Element rowList = null; if (TRUE.equals(item.attributeValue(JRSTLexer.TABLE_HEADER))) { rowList = tgroup.addElement(THEAD); } else { rowList = tgroup.addElement(TBODY); } List<Element> rows = (List<Element>) item.selectNodes(JRSTLexer.ROW); for (int r = 0; r < rows.size(); r++) { Element row = rowList.addElement(ROW); List<Element> cells = (List<Element>) rows.get(r).selectNodes(JRSTLexer.CELL); for (int c = 0; c < cells.size(); c++) { Element cell = cells.get(c); // si la cellule a ete utilise pour un regroupement vertical on // la passe if (!TRUE.equals(cell.attributeValue("used"))) { Element entry = row.addElement(ENTRY); String text = ""; // on regroupe les cellules verticalement int morerows = -1; Element tmpCell = null; String cellStart = cell.attributeValue(JRSTLexer.CELL_INDEX_START); do { morerows++; tmpCell = (Element) rows.get(r + morerows).selectSingleNode( JRSTLexer.CELL + "[@" + JRSTLexer.CELL_INDEX_START + "=" + cellStart + "]"); text += tmpCell.getText(); // on marque la cellule comme utilise tmpCell.addAttribute("used", TRUE); } while (!TRUE.equals(tmpCell.attributeValue(JRSTLexer.CELL_END))); if (morerows > 0) { entry.addAttribute("morerows", String.valueOf(morerows)); } // on compte le nombre de cellules regroupees // horizontalement int morecols = 0; tmpCell = cells.get(c + morecols); int cellEnd = Integer.parseInt(tmpCell.attributeValue(JRSTLexer.CELL_INDEX_END)); while (cellEnd + 1 != beginCell[c + morecols + 1]) { morecols++; // tmpCell = cells.get(c + morecols); // cellEnd = // Integer.parseInt(tmpCell.attributeValue(JRSTLexer. // CELL_INDEX_END)); } if (morecols > 0) { entry.addAttribute("morecols", String.valueOf(morecols)); } // parse entry text in table Document doc = newJRSTReader(new StringReader(text)); entry.appendContent(doc.getRootElement()); } } if (TRUE.equals(rows.get(r).attributeValue(JRSTLexer.ROW_END_HEADER))) { rowList = tgroup.addElement(TBODY); } } return result; }
From source file:tokyo.northside.jrst.JRSTReader.java
License:Open Source License
/** * Parse text in element and replace text with parse result * //from w w w. java2 s .com * @param e element * @throws DocumentException * @throws UnsupportedEncodingException */ private void inline(Element e) throws DocumentException, UnsupportedEncodingException { String text = e.getText(); text = StringEscapeUtils.escapeXml(text); // search all LITERAL and replace it with special mark // this prevent substitution in literal, example **something** must not // change in literal Map<String, String> temporaries = new HashMap<String, String>(); Matcher matcher = REGEX_LITERAL.matcher(text); int index = 0; while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); String literal = "<" + LITERAL + ">" + matcher.group(1) + "</" + LITERAL + ">"; String key = LITERAL + index++; temporaries.put(key, literal); text = text.substring(0, start) + "<tmp>" + key + "</tmp>" + text.substring(end); matcher = REGEX_LITERAL.matcher(text); } // search all REGEX_INLINE_REFERENCE and replace it with special mark // this prevent substitution of URL with REGEX_REFERENCE. Use same // mechanisme as literal for that matcher = REGEX_INLINE_REFERENCE.matcher(text); index = 0; while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); Element ref = DocumentHelper.createElement(REFERENCE); ref.addAttribute(REFURI, StringEscapeUtils.unescapeXml(matcher.group(2))); ref.setText(StringEscapeUtils.unescapeXml(matcher.group(1))); String key = "inlineReference" + index++; temporaries.put(key, ref.asXML()); text = text.substring(0, start) + "<tmp>" + key + "</tmp>" + text.substring(end); matcher = REGEX_INLINE_REFERENCE.matcher(text); } // do all substitution inline text = REGEX_EMAIL.matcher(text) .replaceAll("$1<" + REFERENCE + " refuri='mailto:$2'>$2</" + REFERENCE + ">$3"); text = REGEX_STRONG.matcher(text).replaceAll("<" + STRONG + ">$1</" + STRONG + ">"); text = REGEX_EMPHASIS.matcher(text).replaceAll("<" + EMPHASIS + ">$1</" + EMPHASIS + ">"); text = REGEX_REFERENCE.matcher(text).replaceAll("<" + REFERENCE + " refuri='$1'>$1</" + REFERENCE + ">$2"); // _[#]truc matcher = REGEX_FOOTNOTE_REFERENCE.matcher(text); while (matcher.find()) { String txtDebut = text.substring(0, matcher.start()); String txtFin = text.substring(matcher.end() - 1, text.length() - 1); Element footnote = DocumentHelper.createElement(FOOTNOTE_REFERENCE); String sFootnote = matcher.group(); boolean done = false; for (int i = 0; i < sFootnote.length() && !done; i++) { if (sFootnote.charAt(i) == ']') { String id = sFootnote.substring(1, i); if (id.equals("*")) { int nb = Math.abs(symbolMaxRef / 10) + 1; char symbol = FOOTNOTE_SYMBOL.charAt(symbolMaxRef % 10); String label = ""; for (int j = 0; j < nb; j++) { label += symbol; } symbolMaxRef++; footnote.addAttribute(AUTO, "*"); for (int j = 0; j < eFootnotes.size(); j++) { Element eFootnote = eFootnotes.get(j); if (eFootnote.attributeValue(LABEL).equals(label)) { footnote.addAttribute(ATTR_IDS, eFootnote.attributeValue(BACKREFS)); footnote.addAttribute(ATTR_REFID, eFootnote.attributeValue(ATTR_IDS)); } } footnote.setText(label); } else if (id.matches("[1-9]+")) { for (int j = 0; j < eFootnotes.size(); j++) { Element eFootnote = eFootnotes.get(j); if (eFootnote.attributeValue(LABEL).equals(id)) { footnote.addAttribute(ATTR_IDS, eFootnote.attributeValue(BACKREFS)); footnote.addAttribute(ATTR_REFID, eFootnote.attributeValue(ATTR_IDS)); } } footnote.setText(id); lblFootnotesRef.add(Integer.parseInt(id)); } else if (id.equals("#")) { int lblMax = 0; for (int j = 0; j < lblFootnotesRef.size(); j++) { lblMax = Math.max(lblMax, lblFootnotesRef.get(j)); } boolean[] lbls = new boolean[lblMax]; for (int j = 0; j < lbls.length; j++) { lbls[j] = false; } for (int j = 0; j < lblFootnotesRef.size(); j++) { lbls[lblFootnotesRef.get(j) - 1] = true; } boolean valide = false; do { boolean trouve = false; String label = null; for (int j = 0; j < lbls.length && !trouve; j++) { if (!lbls[j]) { trouve = true; label = "" + (j + 1); } } if (!trouve) { label = "" + (lbls.length + 1); } footnote.addAttribute(AUTO, "1"); for (int j = 0; j < eFootnotes.size(); j++) { Element eFootnote = eFootnotes.get(j); if (eFootnote.attributeValue(LABEL).equals(label)) { if (!(eFootnote.attributeValue(TYPE).equals(AUTONUMLABEL))) { footnote.addAttribute(ATTR_IDS, eFootnote.attributeValue(BACKREFS)); footnote.addAttribute(ATTR_REFID, eFootnote.attributeValue(ATTR_IDS)); footnote.setText(label); lblFootnotesRef.add(Integer.parseInt(label)); valide = true; } else { valide = false; lbls[Integer.parseInt(label) - 1] = true; } } } } while (!valide); } else { footnote.addAttribute(AUTO, "1"); String name = id.substring(1); boolean trouve = false; for (int j = 0; j < eFootnotes.size() && !trouve; j++) { Element eFootnote = eFootnotes.get(j); if (eFootnote.attributeValue(NAMES).equals(name)) { footnote.addAttribute(ATTR_IDS, eFootnote.attributeValue(BACKREFS)); footnote.addAttribute(ATTR_REFID, eFootnote.attributeValue(ATTR_IDS)); String label = eFootnote.attributeValue(LABEL); footnote.setText(label); lblFootnotesRef.add(Integer.parseInt(label)); trouve = true; } } footnote.addAttribute(NAMES, name); } done = true; } } text = txtDebut + footnote.asXML() + txtFin; matcher = REGEX_FOOTNOTE_REFERENCE.matcher(text); } // .. __http://truc.html matcher = REGEX_ANONYMOUS_HYPERLINK_REFERENCE.matcher(text); while (matcher.find()) { String txtDebut = text.substring(0, matcher.start()); String txtFin = text.substring(matcher.end(), text.length()); String ref = text.substring(matcher.start(), matcher.end() - 2); ref = ref.replaceAll("`", ""); Element anonym = DocumentHelper.createElement(REFERENCE); anonym.addAttribute(ANONYMOUS, "1"); anonym.addAttribute(NAME, ref.trim()); if (!eTargetAnonymous.isEmpty()) { Element target = eTargetAnonymous.getFirst(); eTargetAnonymous.removeFirst(); anonym.addAttribute(REFURI, target.attributeValue(REFURI)); } anonym.setText(ref); text = txtDebut + anonym.asXML() + txtFin; matcher = REGEX_ANONYMOUS_HYPERLINK_REFERENCE.matcher(text); } // .. _truc: http://truc.html matcher = REGEX_HYPERLINK_REFERENCE.matcher(text); while (matcher.find()) { String txtDebut = text.substring(0, matcher.start()); String txtFin = text.substring(matcher.end(), text.length()); String ref = text.substring(matcher.start(), matcher.end() - 1); ref = StringEscapeUtils.unescapeXml(ref); ref = ref.replaceAll("('|_)", ""); ref = ref.replaceAll("`", ""); Element hyper = DocumentHelper.createElement(REFERENCE); hyper.addAttribute(NAME, ref); boolean trouve = false; for (int i = 0; i < eTarget.size() && !trouve; i++) { Element el = eTarget.get(i); String refTmp = URLEncoder.encode(ref.replaceAll("\\s", "-").toLowerCase(), "UTF-8"); if (el.attributeValue(ID).equalsIgnoreCase((refTmp))) { hyper.addAttribute(REFURI, el.attributeValue(REFURI)); trouve = true; } } if (!trouve) { hyper.addAttribute(ATTR_REFID, ref); } hyper.setText(ref); text = txtDebut + hyper.asXML() + " " + txtFin; matcher = REGEX_HYPERLINK_REFERENCE.matcher(text); } // substitution reference matcher = REGEX_SUBSTITUTION_REFERENCE.matcher(text); int begin = 0; while (matcher.find(begin)) { String start = text.substring(0, matcher.start()); String end = text.substring(matcher.end()); String ref = matcher.group(1); Node subst = e.selectSingleNode("//" + SUBSTITUTION_DEFINITION + "[@name='" + ref + "']/child::node()"); if (subst == null) { text = start + "|" + ref + "|"; } else { text = start + subst.asXML(); } begin = text.length(); text += end; matcher = REGEX_SUBSTITUTION_REFERENCE.matcher(text); } // undo substitution in LITERAL Pattern p = Pattern.compile("<tmp>([^<>]+)</tmp>"); matcher = p.matcher(text); while (matcher.find()) { String start = text.substring(0, matcher.start()); String end = text.substring(matcher.end()); String tempKey = matcher.group(1); text = start + temporaries.get(tempKey) + end; matcher = p.matcher(text); } String resultElementText = text.trim(); Element result = DocumentHelper.parseText("<TMP>" + resultElementText + "</TMP>").getRootElement(); e.setText(""); e.appendContent(result); }