Author: kcardineaud Date: 2011-05-18 15:35:06 +0200 (Wed, 18 May 2011) New Revision: 583 Url: http://nuiton.org/repositories/revision/jrst/583 Log: #1393 Replace String constant usage by real constant variable Modified: trunk/jrst/src/main/java/org/nuiton/jrst/JRSTLexer.java trunk/jrst/src/main/java/org/nuiton/jrst/JRSTReader.java Modified: trunk/jrst/src/main/java/org/nuiton/jrst/JRSTLexer.java =================================================================== --- trunk/jrst/src/main/java/org/nuiton/jrst/JRSTLexer.java 2011-05-17 14:34:48 UTC (rev 582) +++ trunk/jrst/src/main/java/org/nuiton/jrst/JRSTLexer.java 2011-05-18 13:35:06 UTC (rev 583) @@ -120,6 +120,8 @@ static final public String PARAGRAPH = "paragraph"; static final public String BLANK_LINE = "blankLine"; + + static final public String COMMENT = "comment"; static final public String SUBSTITUTION_DEFINITION = "substitution_definition"; @@ -146,6 +148,13 @@ public static final String TARGET = "target"; public static final String FOOTNOTE = "footnote"; + + public static final String FOOTNOTES = "footnotes"; + + public static final String LEVEL = "level"; + + public static final String TARGETANONYMOUS = "targetAnonymous"; + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Table Elements @@ -170,7 +179,11 @@ static final public String CELL_BEGIN = "begin"; static final public String CELL_END = "end"; + + static final public String REMOVE = "remove"; + static final public String INCLUDE = "include"; + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Directive Elements // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -180,7 +193,72 @@ static final public String DIRECTIVE_TYPE = "type"; static final public String DIRECTIVE_VALUE = "value"; + + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Attributs + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + static final public String AUTONUM = "autoNum"; + + static final public String AUTONUMLABEL = "autoNumLabel"; + + static final public String AUTOSYMBOL = "autoSymbol"; + + static final public String BULLET = "bullet"; + + static final public String CHAR = "char"; + + static final public String ID = "id"; + + static final public String CLASSIFIERS = "classifiers"; + + static final public String DELIMITER = "delimiter"; + + static final public String DELIMITEREXISTE ="delimiterExiste"; + + static final public String ENUMTYPE = "enumtype"; + + static final public String REFURI = "refuri"; + + static final public String OPTION = "option"; + + static final public String LITERAL = "literal"; + + static final public String NAME = "name"; + + static final public String NUM ="num"; + + static final public String OPTIONARGUMENT = "option_argument"; + + static final public String OPTIONSTRING = "option_string"; + + static final public String PREFIX = "prefix"; + + static final public String START = "start"; + + static final public String SUBEXISTE = "subExiste"; + + static final public String SUFFIX = "suffix"; + + static final public String SUBTITLE = "subtitle"; + + static final public String TERM = "term"; + + static final public String TITLEATTR = "title"; + + static final public String XMLSPACE = "xml:space"; + + static final public String TYPE = "type"; + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + protected static final String TRUE = "true"; + + protected static final String FALSE = "false"; + + /** * retient le niveau du titre, pour un titre de type double, on met deux * fois le caratere dans la chaine, sinon on le met une seul fois. @@ -334,8 +412,8 @@ int level = level(l); l = l.replaceAll("^\\s*.. " + HEADER + ":: ", ""); result = DocumentHelper.createElement(HEADER).addAttribute( - "level", String.valueOf(level)); - result.addAttribute("line", "" + i); + LEVEL, String.valueOf(level)); + result.addAttribute(LINE, "" + i); result.setText(l); } @@ -369,8 +447,8 @@ int level = level(l); l = l.replaceAll("^\\s*.. " + FOOTER + ":: ", ""); result = DocumentHelper.createElement(FOOTER).addAttribute( - "level", String.valueOf(level)); - result.addAttribute("line", "" + i); + LEVEL, String.valueOf(level)); + result.addAttribute(LINE, "" + i); result.setText(l); } } @@ -398,14 +476,17 @@ i++; if (l.matches("^\\s*__ .+$|^\\s*\\.\\. __\\:.+$")) { + log.debug(l); Element resultTmp = DocumentHelper - .createElement("targetAnonymous"); - resultTmp.addAttribute("level", "" + level(l)); + .createElement(TARGETANONYMOUS); + resultTmp.addAttribute(LEVEL, "" + level(l)); Matcher matcher = Pattern.compile("__ ").matcher(l); + if (matcher.find()) { - resultTmp.addAttribute("refuri", l.substring(matcher + resultTmp.addAttribute(REFURI, l.substring(matcher .end(), l.length())); } + result.add(resultTmp); } } @@ -556,13 +637,13 @@ if (line != null) { // Le header est parse des le debut if (line.matches("^\\s*.. " + HEADER + ":: .*")) { - result = DocumentHelper.createElement("remove").addAttribute( - "level", "" + level(line)); + result = DocumentHelper.createElement(REMOVE).addAttribute( + LEVEL, "" + level(line)); } // Le footer if (line.matches("^\\s*.. " + FOOTER + ":: .*")) { - result = DocumentHelper.createElement("remove").addAttribute( - "level", "" + level(line)); + result = DocumentHelper.createElement(REMOVE).addAttribute( + LEVEL, "" + level(line)); } } @@ -590,12 +671,12 @@ String line = in.readLine(); if (line != null) { if (line.matches("^\\s*\\.\\.\\sinclude\\:\\:.+$")) { - result = DocumentHelper.createElement("include"); - result.addAttribute("level", "" + level(line)); + result = DocumentHelper.createElement(INCLUDE); + result.addAttribute(LEVEL, "" + level(line)); String option = line.substring(line.indexOf("::") + 2).trim(); - result.addAttribute("option", ""); - if (option.trim().equalsIgnoreCase("literal")) { - result.addAttribute("option", "literal"); + result.addAttribute(OPTION, ""); + if (option.trim().equalsIgnoreCase(LITERAL)) { + result.addAttribute(OPTION, LITERAL); line = in.readLine(); result.setText(line.trim()); } else { @@ -640,17 +721,17 @@ if (line != null) { if (line.matches("^(\\s*((--?)|(//?))\\w+([ =][<a-zA-Z][\\w-><]*)?)\\s*.*$")) { result = DocumentHelper.createElement(OPTION_LIST) - .addAttribute("level", "" + level(line)); + .addAttribute(LEVEL, "" + level(line)); char delimiter; do { Matcher matcher = Pattern.compile("[-/][-/]?.+").matcher(line); matcher.find(); - Element option = result.addElement("option"); + Element option = result.addElement(OPTION); String option_stringTmp = matcher.group(); matcher = Pattern.compile("^[-/][-/]?\\w+").matcher(option_stringTmp); matcher.find(); String option_string = matcher.group(); - option.addAttribute("option_string", option_string); + option.addAttribute(OPTIONSTRING, option_string); boolean done = false; // Delimiteur bidon @@ -662,7 +743,7 @@ } else { done = true; } - option.addAttribute("delimiterExiste", "false"); + option.addAttribute(DELIMITEREXISTE, FALSE); if (delimiter == ' ') { // S'il y a 2 espaces a suivre, // l'option est finie @@ -672,14 +753,14 @@ } String option_argument = null; if ((delimiter == '=' || delimiter == ' ') && !done) { - option.addAttribute("delimiterExiste", "true"); - option.addAttribute("delimiter", "" + delimiter); + option.addAttribute(DELIMITEREXISTE, TRUE); + option.addAttribute(DELIMITER, "" + delimiter); matcher = Pattern.compile(delimiter + "(([a-zA-Z][\\w-]+)|(<[a-zA-Z][^>]*>))").matcher( option_stringTmp); if (matcher.find()) { option_argument = matcher.group().substring(1, matcher.group().length()); - option.addAttribute("option_argument", option_argument); + option.addAttribute(OPTIONARGUMENT, option_argument); if (option_stringTmp.charAt(option_argument.length() + 1) == ',') { delimiter = ','; } else { @@ -688,7 +769,7 @@ } else { // Si la description n'est pas sur la meme // ligne option_argument = option_stringTmp; - option.addAttribute("option_argument", + option.addAttribute(OPTIONARGUMENT, option_argument); line = in.readLine(); if (line != null) { @@ -731,7 +812,7 @@ Matcher matcher = Pattern.compile(TOPIC + "::").matcher(line); matcher.find(); result = DocumentHelper.createElement(TOPIC).addAttribute( - "level", "" + level(line)); + LEVEL, "" + level(line)); String title = line.substring(matcher.end(), line.length()); result.addAttribute(TITLE, title); line = in.readLine(); @@ -777,7 +858,7 @@ Matcher matcher = Pattern.compile(SIDEBAR + "::").matcher(line); matcher.find(); result = DocumentHelper.createElement(SIDEBAR).addAttribute( - "level", "" + level(line)); + LEVEL, "" + level(line)); String title = line.substring(matcher.end(), line.length()); result.addAttribute(TITLE, title); line = in.readLine(); @@ -786,11 +867,11 @@ matcher.find(); String subTitle = line.substring(matcher.end(), line .length()); - result.addAttribute("subExiste", "true"); - result.addAttribute("subtitle", subTitle); + result.addAttribute(SUBEXISTE, TRUE); + result.addAttribute(SUBTITLE, subTitle); line = in.readLine(); } else { - result.addAttribute("subExiste", "false"); + result.addAttribute(SUBEXISTE, FALSE); } String txt = joinBlock(readBlock(level(line))); result.setText(txt); @@ -832,7 +913,7 @@ int[] levelsTmp = new int[lines.length]; int levelmin = 999; result = DocumentHelper.createElement(LINE_BLOCK).addAttribute( - "level", 0 + ""); + LEVEL, 0 + ""); for (int i = 0; i < levelsTmp.length; i++) { // on enleve | lines[i] = lines[i].replaceAll("\\|\\s?", ""); @@ -893,7 +974,7 @@ } for (int i = 0; i < levels.length; i++) { Element eLine = result.addElement(LINE); - eLine.addAttribute("level", "" + levels[i]); + eLine.addAttribute(LEVEL, "" + levels[i]); eLine.setText(lines[i].trim()); } } @@ -921,8 +1002,8 @@ if (line.matches("^\\s*>>>\\s.*")) { int level = level(line); result = DocumentHelper.createElement(DOCTEST_BLOCK) - .addAttribute("level", String.valueOf(level)); - result.addAttribute("xml:space", "preserve"); + .addAttribute(LEVEL, String.valueOf(level)); + result.addAttribute(XMLSPACE, "preserve"); line += "\n" + joinBlock(readBlock(level)); result.setText(line); } @@ -972,7 +1053,7 @@ } } result = DocumentHelper.createElement(BLOCK_QUOTE) - .addAttribute("level", String.valueOf(level)); + .addAttribute(LEVEL, String.valueOf(level)); if (blockQuote != null) { result.addAttribute(ATTRIBUTION, blockQuote); } @@ -1016,20 +1097,20 @@ matcher.find(); int level = level(line); result = DocumentHelper.createElement(ADMONITION).addAttribute( - "level", "" + level); + LEVEL, "" + level); if (matcher.group().equals(ADMONITION)) { // Il y a un titre // pour un // admonition // general admonition = true; - result.addAttribute("type", ADMONITION); + result.addAttribute(TYPE, ADMONITION); String title = line.substring(matcher.end() + 2, line .length()); - result.addAttribute("title", title); + result.addAttribute(TITLEATTR, title); } else { - result.addAttribute("type", matcher.group()); + result.addAttribute(TYPE, matcher.group()); } String firstLine = ""; @@ -1074,7 +1155,7 @@ if (line != null && line.matches("\\s*")) { int level = level(line); result = DocumentHelper.createElement(BLANK_LINE).addAttribute( - "level", String.valueOf(level)); + LEVEL, String.valueOf(level)); } endPeek(); @@ -1103,13 +1184,13 @@ if (ref != null && !"".equals(ref)) { result = DocumentHelper .createElement(SUBSTITUTION_DEFINITION); - result.addAttribute("name", ref); + result.addAttribute(NAME, ref); directive = result.addElement(DIRECTIVE); } else { result = DocumentHelper.createElement(DIRECTIVE); directive = result; } - result.addAttribute("level", "0"); + result.addAttribute(LEVEL, "0"); directive.addAttribute(DIRECTIVE_TYPE, directiveType); directive.addAttribute(DIRECTIVE_VALUE, directiveValue); @@ -1146,7 +1227,7 @@ // must have one blank line after if (line != null && line.matches("\\s*")) { result = DocumentHelper.createElement(TRANSITION) - .addAttribute("level", String.valueOf(0)); + .addAttribute(LEVEL, String.valueOf(0)); } } } @@ -1195,7 +1276,7 @@ // if para is empty, there are error and possible // infiny loop on para, force read next line result = DocumentHelper.createElement(PARAGRAPH) - .addAttribute("level", String.valueOf(level)) + .addAttribute(LEVEL, String.valueOf(level)) .addText(para); } } @@ -1247,7 +1328,7 @@ } result = DocumentHelper.createElement(LITERAL_BLOCK) - .addAttribute("level", String.valueOf(level)).addText( + .addAttribute(LEVEL, String.valueOf(level)).addText( para); } } @@ -1279,7 +1360,7 @@ if (line != null && line.matches("^:((?i)" + DOCINFO_ITEM + "):.*$")) { result = DocumentHelper.createElement(DOCINFO); - result.addAttribute("level", "0"); + result.addAttribute(LEVEL, "0"); String infotype = line.substring(1, line.indexOf(":", 1)); /* @@ -1299,7 +1380,7 @@ // CVS, RCS support text = text.replaceAll("\\$\\w+: (.+?)\\$", "$1"); - result.addAttribute("type", infotype).addText(text); + result.addAttribute(TYPE, infotype).addText(text); } endPeek(); return result; @@ -1337,9 +1418,9 @@ matcher = pTableBegin.matcher(line); if (matcher.matches()) { // complexe table result = DocumentHelper.createElement(TABLE); - result.addAttribute(TABLE_HEADER, "false"); + result.addAttribute(TABLE_HEADER, FALSE); int level = level(line); - result.addAttribute("level", String.valueOf(level)); + result.addAttribute(LEVEL, String.valueOf(level)); line = line.trim(); int tableWidth = line.length(); result.addAttribute(TABLE_WIDTH, String.valueOf(tableWidth)); @@ -1378,9 +1459,9 @@ // fin normale de ligne, on peut directement l'assigner lastSeparationLine = l; for (Element cell : (List<Element>) row.elements()) { - cell.addAttribute(CELL_END, "true"); + cell.addAttribute(CELL_END, TRUE); } - row.addAttribute(ROW_END_HEADER, "false"); + row.addAttribute(ROW_END_HEADER, FALSE); result.add(row); row = DocumentHelper.createElement(ROW); done = true; @@ -1390,11 +1471,11 @@ // fin de du header, on peut directement l'assigner lastSeparationLine = l; for (Element cell : (List<Element>) row.elements()) { - cell.addAttribute(CELL_END, "true"); + cell.addAttribute(CELL_END, TRUE); } - row.addAttribute(ROW_END_HEADER, "true"); + row.addAttribute(ROW_END_HEADER, TRUE); result.add(row); - result.addAttribute(TABLE_HEADER, "true"); + result.addAttribute(TABLE_HEADER, TRUE); row = DocumentHelper.createElement(ROW); done = true; } @@ -1429,7 +1510,7 @@ Element cell = null; if (row.nodeCount() <= cellNumber) { cell = row.addElement(CELL); - cell.addAttribute(CELL_END, "false"); + cell.addAttribute(CELL_END, FALSE); } else { cell = (Element) row.node(cellNumber); } @@ -1464,7 +1545,7 @@ // mais on commence tout de meme par fermer tout les // cells for (Element cell : (List<Element>) row.elements()) { - cell.addAttribute(CELL_END, "true"); + cell.addAttribute(CELL_END, TRUE); } StringBuffer tmp = new StringBuffer(l); @@ -1505,7 +1586,7 @@ // on a ajouter des choses dans la cell, // donc // ce n'est pas la fin - cell.addAttribute(CELL_END, "false"); + cell.addAttribute(CELL_END, FALSE); cell.addAttribute(CELL_INDEX_START, String .valueOf(start)); cell.addAttribute(CELL_INDEX_END, String @@ -1520,7 +1601,7 @@ } } lastSeparationLine = tmp.toString(); - row.addAttribute(ROW_END_HEADER, "false"); + row.addAttribute(ROW_END_HEADER, FALSE); result.add(row); row = DocumentHelper.createElement(ROW); done = true; @@ -1576,7 +1657,7 @@ // Header if the table contains 3 equals separations result.addAttribute(TABLE_HEADER, "" + (nbSeparations == 2)); int level = level(line); - result.addAttribute("level", String.valueOf(level)); + result.addAttribute(LEVEL, String.valueOf(level)); result .addAttribute(TABLE_WIDTH, String .valueOf(tableWidth + 1)); @@ -1672,10 +1753,10 @@ if (lastLines.size() == 0) { row.addAttribute("debug", "pCell"); - cell.addAttribute(CELL_END, "true"); + cell.addAttribute(CELL_END, TRUE); } else { row.addAttribute("debug", "pCellEnd"); - cell.addAttribute(CELL_END, "false"); + cell.addAttribute(CELL_END, FALSE); } cell.addAttribute(CELL_INDEX_START, String .valueOf(matcher.start() + 1)); @@ -1692,7 +1773,7 @@ row.addAttribute(ROW_END_HEADER, "" + (separation == 2)); } else { - row.addAttribute(ROW_END_HEADER, "false"); + row.addAttribute(ROW_END_HEADER, FALSE); } result.add(row); @@ -1745,7 +1826,7 @@ String bullet = line.substring(level, level + 1); result = DocumentHelper.createElement(BULLET_LIST).addAttribute( - "level", String.valueOf(level)).addAttribute("bullet", + LEVEL, String.valueOf(level)).addAttribute(BULLET, bullet); if (!in.eof()) { @@ -1791,7 +1872,7 @@ int begin = matcher.end(1) + 1; result = DocumentHelper.createElement(FIELD_LIST).addAttribute( - "level", String.valueOf(level)).addAttribute("name", + LEVEL, String.valueOf(level)).addAttribute(NAME, name); if (!in.eof()) { @@ -1842,9 +1923,9 @@ String classifiers = matcher.group(2); result = DocumentHelper.createElement(DEFINITION_LIST) - .addAttribute("level", String.valueOf(level)) - .addAttribute("term", term).addAttribute( - "classifiers", classifiers); + .addAttribute(LEVEL, String.valueOf(level)) + .addAttribute(TERM, term).addAttribute( + CLASSIFIERS, classifiers); // poussin 20070207 don't read block here because can't // interpret it correctly in JRSTReader @@ -1917,10 +1998,10 @@ } result = DocumentHelper.createElement(ENUMERATED_LIST) - .addAttribute("level", String.valueOf(level)) - .addAttribute("start", start).addAttribute("prefix", - prefix).addAttribute("suffix", suffix) - .addAttribute("enumtype", enumtype); + .addAttribute(LEVEL, String.valueOf(level)) + .addAttribute(START, start).addAttribute(PREFIX, + prefix).addAttribute(SUFFIX, suffix) + .addAttribute(ENUMTYPE, enumtype); if (!in.eof()) { String[] content = readBlock(level + 1); @@ -1974,7 +2055,7 @@ && line.length() == titles[1].length() && line.equals(titles[1])) { result = DocumentHelper.createElement(TITLE).addAttribute( - "type", "double").addAttribute("char", + TYPE, "double").addAttribute(CHAR, titles[1].substring(0, 1)).addText(titles[0]); } } else { @@ -1985,7 +2066,7 @@ .length()) { result = DocumentHelper.createElement(TITLE).addAttribute( - "type", "simple").addAttribute("char", + TYPE, "simple").addAttribute(CHAR, title.substring(0, 1)).addText( line.replaceFirst("\\s*$", "")); } @@ -1994,9 +2075,9 @@ if (result != null) { // add level information - String titleLevel = result.attributeValue("char"); + String titleLevel = result.attributeValue(CHAR); - if ("double".equals(result.attributeValue("type"))) { + if ("double".equals(result.attributeValue(TYPE))) { titleLevel += titleLevel; } int level = titleLevels.indexOf(titleLevel); @@ -2004,7 +2085,7 @@ level = titleLevels.size(); titleLevels.add(titleLevel); } - result.addAttribute("level", String + result.addAttribute(LEVEL, String .valueOf(JRSTReader.MAX_SECTION_DEPTH + level)); } @@ -2023,9 +2104,9 @@ Matcher matcher = Pattern.compile("\\.\\.\\s_").matcher(line); if (matcher.find()) { int i = line.indexOf(':'); - result.addAttribute("id", URLEncoder.encode(line.substring(matcher.end(), i) + result.addAttribute(ID, URLEncoder.encode(line.substring(matcher.end(), i) .toLowerCase().replaceAll(" ", "-"), "UTF-8")); - result.addAttribute("level", "" + level(line)); + result.addAttribute(LEVEL, "" + level(line)); } } } @@ -2052,14 +2133,14 @@ boolean done = false; for (int i = matcher.end(); i < line.length() && !done; i++) { if (line.charAt(i) == ':') { - result.getLast().addAttribute("level", + result.getLast().addAttribute(LEVEL, "" + level(line)); result.getLast().addAttribute( - "id", + ID, URLEncoder.encode(line.substring(matcher.end(), i) .replaceAll(" ", "-").toLowerCase(), "UTF-8")); result.getLast().addAttribute( - "name", + NAME, line.substring(matcher.end(), i) .toLowerCase()); if (i + 2 > line.length()) { @@ -2071,10 +2152,10 @@ if (line == null) { line = ""; } - result.getLast().addAttribute("refuri", + result.getLast().addAttribute(REFURI, line.trim()); } else { - result.getLast().addAttribute("refuri", line.substring(i + 2, line.length())); + result.getLast().addAttribute(REFURI, line.substring(i + 2, line.length())); } done = true; @@ -2099,8 +2180,8 @@ String line = in.readLine(); if (line != null) { if (line.matches("^\\s*__ .+$|^\\s*\\.\\. __\\:.+$")) { - result = DocumentHelper.createElement("targetAnonymous"); - result.addAttribute("level", "" + level(line)); + result = DocumentHelper.createElement(TARGETANONYMOUS); + result.addAttribute(LEVEL, "" + level(line)); } } @@ -2121,9 +2202,9 @@ String line = in.readLine(); if (line != null) { if (line.matches("^\\.\\.\\s+.*$")) { - result = DocumentHelper.createElement("comment"); - result.addAttribute("level", "0"); - result.addAttribute("xml:space", "preserve"); + result = DocumentHelper.createElement(COMMENT); + result.addAttribute(LEVEL, "0"); + result.addAttribute(XMLSPACE, "preserve"); // first line is part of comment result.setText(line.substring(2).trim()); @@ -2159,9 +2240,9 @@ if (lines != null) { // int levelRef = level(line); for (String line : lines) { - Element comment = DocumentHelper.createElement("comment"); - comment.addAttribute("level", "0"); - comment.addAttribute("xml:space", "preserve"); + Element comment = DocumentHelper.createElement(COMMENT); + comment.addAttribute(LEVEL, "0"); + comment.addAttribute(XMLSPACE, "preserve"); // first line is part of comment comment.setText(line.substring(2).trim()); @@ -2196,7 +2277,7 @@ String line = in.readLine(); if (line != null) { if (line.matches("^\\s*\\.\\.\\s\\[(#|[0-9]|\\*).*\\]\\s.+$")) { - result = DocumentHelper.createElement("footnotes"); + result = DocumentHelper.createElement(FOOTNOTES); boolean bLine = false; do { @@ -2211,19 +2292,19 @@ for (int i = matcher.end(); i < line.length() && !done; i++) { if (line.charAt(i) == ']') { - result.addAttribute("level", "" + level(line)); + result.addAttribute(LEVEL, "" + level(line)); String id = line.substring(matcher.end(), i); if (id.matches("\\*")) { - footnote.addAttribute("type", "autoSymbol"); + footnote.addAttribute(TYPE, AUTOSYMBOL); } else if (id.matches("[0-9]")) { - footnote.addAttribute("type", "num"); - footnote.addAttribute("name", id); + footnote.addAttribute(TYPE, NUM); + footnote.addAttribute(NAME, id); } else if (id.equals("#")) { - footnote.addAttribute("type", "autoNum"); + footnote.addAttribute(TYPE, AUTONUM); } else { - footnote.addAttribute("type", - "autoNumLabel"); - footnote.addAttribute("name", id + footnote.addAttribute(TYPE, + AUTONUMLABEL); + footnote.addAttribute(NAME, id .substring(1)); } String text = line.substring(i + 2, line Modified: trunk/jrst/src/main/java/org/nuiton/jrst/JRSTReader.java =================================================================== --- trunk/jrst/src/main/java/org/nuiton/jrst/JRSTReader.java 2011-05-17 14:34:48 UTC (rev 582) +++ trunk/jrst/src/main/java/org/nuiton/jrst/JRSTReader.java 2011-05-18 13:35:06 UTC (rev 583) @@ -69,8 +69,10 @@ import static org.nuiton.jrst.ReStructuredText.FOOTNOTE; import static org.nuiton.jrst.ReStructuredText.FOOTNOTE_REFERENCE; import static org.nuiton.jrst.ReStructuredText.FOOTNOTE_SYMBOL; +import static org.nuiton.jrst.ReStructuredText.GENERATED; import static org.nuiton.jrst.ReStructuredText.HEADER; import static org.nuiton.jrst.ReStructuredText.IMAGE; +import static org.nuiton.jrst.ReStructuredText.LABEL; import static org.nuiton.jrst.ReStructuredText.LINE; import static org.nuiton.jrst.ReStructuredText.LINE_BLOCK; import static org.nuiton.jrst.ReStructuredText.LIST_ITEM; @@ -114,6 +116,7 @@ import static org.nuiton.jrst.ReStructuredText.TRANSITION; import static org.nuiton.jrst.ReStructuredText.VERSION; + import java.io.BufferedReader; import java.io.File; import java.io.FileReader; @@ -363,6 +366,72 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ private static Log log = LogFactory.getLog(JRSTReader.class); + protected static final String ANONYMOUS = "anonymous"; + + protected static final String AUTO = "auto"; + + protected static final String AUTONUM = "autoNum"; + + protected static final String AUTONUMLABEL = "autoNumLabel"; + + protected static final String AUTOSYMBOL = "autoSymbol"; + + protected static final String ATTR_REFID = "refid"; + + protected static final String ATTR_INLINE = "inline"; + + protected static final String ATTR_IDS = "id"; + + protected static final String BACKREFS = "backrefs"; + + protected static final String BULLET = "bullet"; + + protected static final String CLASS = "class"; + + protected static final String CONTENTS = "contents"; + + protected static final String DELIMITER = "delimiter"; + + protected static final String DELIMITEREXISTE ="delimiterExiste"; + + protected static final String ENUMTYPE = "enumtype"; + + protected static final String FOOTNOTES = "footnotes"; + + protected static final String ID = "id"; + + protected static final String INCLUDE = "include"; + + protected static final String LEVEL = "level"; + + protected static final String NAME = "name"; + + protected static final String NAMES = "name"; + + protected static final String NUM = "num"; + + protected static final String REFURI = "refuri"; + + protected static final String PREFIX = "prefix"; + + protected static final String REMOVE = "remove"; + + protected static final String START = "start"; + + protected static final String SECTNUM = "sectnum"; + + protected static final String SUBEXISTE = "subExiste"; + + protected static final String SUFFIX = "suffix"; + + protected static final String TRUE = "true"; + + protected static final String TYPE = "type"; + + protected static final String TARGETANONYMOUS = "targetAnonymous"; + + protected static final String VALUE = "value"; + protected boolean ERROR_MISSING_ITEM; protected static int MAX_SECTION_DEPTH = -1000; @@ -400,9 +469,9 @@ defaultDirectives.put(IMAGE, new ImageDirective()); defaultDirectives.put(DATE, new DateDirective()); defaultDirectives.put("time", new DateDirective()); - defaultDirectives.put("contents", new ContentDirective()); + defaultDirectives.put(CONTENTS, new ContentDirective()); // defaultDirectives.put("calc", new CalcDirective()); - defaultDirectives.put("sectnum", new SectnumDirective()); + defaultDirectives.put(SECTNUM, new SectnumDirective()); // TODO put here all other directive } @@ -464,18 +533,18 @@ @Override public void visit(Element e) { // remove all level attribute - e.addAttribute("level", null); + e.addAttribute(LEVEL, null); // Constrution du sommaire - String type = e.attributeValue("type"); + String type = e.attributeValue(TYPE); if (type != null) { - if (type.equals("contents")) { + if (type.equals(CONTENTS)) { composeContents(e); - e.addAttribute("type", null); + e.addAttribute(TYPE, null); } } - if ("true".equalsIgnoreCase(e.attributeValue("inline"))) { - e.addAttribute("inline", null); + if (TRUE.equalsIgnoreCase(e.attributeValue(ATTR_INLINE))) { + e.addAttribute(ATTR_INLINE, null); try { inline(e); } catch (DocumentException eee) { @@ -536,7 +605,7 @@ try { levelInit = Integer.parseInt(eTitle.getFirst().attributeValue( - "level")); + LEVEL)); } catch (NumberFormatException eee) { log.error("Can't parse level in: " + eTitle.getFirst().asXML(), eee); @@ -549,13 +618,13 @@ // on rajoute les refid for (int i = 0; i < eTitle.size(); i++) { idMax++; - eTitle.get(i).addAttribute("refid", "id" + idMax); + eTitle.get(i).addAttribute(ATTR_REFID, ID + idMax); } // on enleve les titres limites par depth for (Element el : eTitle) { - int level = Integer.parseInt(el.attributeValue("level")); + int level = Integer.parseInt(el.attributeValue(LEVEL)); level = level - levelInit; - el.addAttribute("level", "" + level); + el.addAttribute(LEVEL, "" + level); if (depth == -1) { title.add(el); } @@ -565,18 +634,18 @@ } } } - e.addAttribute("class", "contents"); - String titleValue = e.attributeValue("value"); - e.addAttribute("value", null); + e.addAttribute(CLASS, CONTENTS); + String titleValue = e.attributeValue(VALUE); + e.addAttribute(VALUE, null); String value = titleValue.trim().toLowerCase(); // sans titre c "contents" par default if (value.matches("\\s*")) { - value = "contents"; + value = CONTENTS; titleValue = "Contents"; } - e.addAttribute("id", value); - e.addAttribute("name", value); - result.addElement("title").setText(titleValue); + e.addAttribute(ATTR_IDS, value); + e.addAttribute(NAMES, value); + result.addElement(TITLE).setText(titleValue); // on compose les lignes if (!noTitle) { //Si il y a des titres à lier à la table des matières result.add(composeLineContent(title, "")); @@ -593,14 +662,14 @@ private Element composeLineContent(LinkedList<Element> title, String num) { Element result = DocumentHelper.createElement(BULLET_LIST); if (sectnum) { - result.addAttribute("class", "auto-toc"); + result.addAttribute(CLASS, "auto-toc"); } Element item = null; int cnt = 0; while (!title.isEmpty()) { Element e = title.getFirst(); - int level = Integer.parseInt(e.attributeValue("level")); + int level = Integer.parseInt(e.attributeValue(LEVEL)); LinkedList<Element> child = new LinkedList<Element>(); if (level <= 0) { @@ -610,20 +679,20 @@ Element para = item.addElement(PARAGRAPH); Element reference = para.addElement(REFERENCE); String text = e.getText(); - String id = e.attributeValue("refid"); - reference.addAttribute("id", id); - reference.addAttribute("refid", text.replaceAll("\\W+", " ") + String id = e.attributeValue(ATTR_REFID); + reference.addAttribute(ATTR_IDS, id); + reference.addAttribute(ATTR_REFID, text.replaceAll("\\W+", " ") .trim().toLowerCase().replaceAll("\\W+", "-")); // si l'on doit les numeroter if (sectnum) { - Element generated = reference.addElement("generated") - .addAttribute("class", "sectnum"); + Element generated = reference.addElement(GENERATED) + .addAttribute(CLASS, SECTNUM); generated.setText(num + cnt + " "); for (int i = 0; i < eTitle.size(); i++) { - if (eTitle.get(i).attributeValue("refid").equals(id)) { + if (eTitle.get(i).attributeValue(ATTR_REFID).equals(id)) { Element generatedTitle = eTitle.get(i).addElement( - "generated"); - generatedTitle.addAttribute("class", "sectnum"); + GENERATED); + generatedTitle.addAttribute(CLASS, SECTNUM); generatedTitle.setText(num + cnt + " "); } @@ -633,12 +702,12 @@ } else { do { - e.addAttribute("level", "" + (level - 1)); + e.addAttribute(LEVEL, "" + (level - 1)); child.add(e); title.removeFirst(); if (!title.isEmpty()) { e = title.getFirst(); - level = Integer.parseInt(e.attributeValue("level")); + level = Integer.parseInt(e.attributeValue(LEVEL)); } } while (!title.isEmpty() && level > 0); String numTmp = ""; @@ -665,7 +734,7 @@ */ private Element composeDocument(JRSTLexer lexer) throws Exception { Element result = DocumentHelper.createElement(DOCUMENT); - result.addAttribute("level", String.valueOf(MAX_SECTION_DEPTH - 1)); + result.addAttribute(LEVEL, String.valueOf(MAX_SECTION_DEPTH - 1)); Element item = null; @@ -684,7 +753,7 @@ if (itemEquals(HEADER, item)) { Element decoration = result.addElement(DECORATION); Element header = decoration.addElement(HEADER); - header.addAttribute("inline", "true").setText(item.getText()); + header.addAttribute(ATTR_INLINE, TRUE).setText(item.getText()); } // le footer @@ -692,7 +761,7 @@ if (itemEquals(FOOTER, item)) { footer = DocumentHelper.createElement(DECORATION); Element header = footer.addElement(FOOTER); - header.addAttribute("inline", "true").setText(item.getText()); + header.addAttribute(ATTR_INLINE, TRUE).setText(item.getText()); } // les hyperlinks @@ -700,11 +769,12 @@ if (listItem != null) { for (Element e : listItem) { Element anonym = DocumentHelper.createElement(TARGET); - anonym.addAttribute("anonymous", "1"); + anonym.addAttribute(ANONYMOUS, "1"); idMax++; - anonym.addAttribute("id", "id" + idMax); - anonym.addAttribute("refuri", e.attributeValue("refuri") - .trim()); + anonym.addAttribute(ATTR_IDS, ID + idMax); + + anonym.addAttribute(REFURI, e.attributeValue(REFURI).trim()); + eTargetAnonymous.add(anonym); eTargetAnonymousCopy.add(anonym); } @@ -712,7 +782,7 @@ // les eléments a enlever (deja parser : header, footer...) item = lexer.peekRemove(); - if (itemEquals("remove", item)) { + if (itemEquals(REMOVE, item)) { lexer.remove(); } @@ -728,12 +798,12 @@ lexer.remove(); Element title = result.addElement(TITLE); String txt = item.getText(); - result.addAttribute("id", txt.replaceAll("[(\\W+)_]", " ") + result.addAttribute(ATTR_IDS, txt.replaceAll("[(\\W+)_]", " ") .toLowerCase().trim().replaceAll("\\s+", "-")); - result.addAttribute("name", txt.toLowerCase().replaceAll( + result.addAttribute(NAMES, txt.toLowerCase().replaceAll( "[(\\W+)_&&[^\\:]]+", " ").trim()); copyLevel(item, title); - title.addAttribute("inline", "true").setText(txt.trim()); + title.addAttribute(ATTR_INLINE, TRUE).setText(txt.trim()); } // skip blank line @@ -745,13 +815,13 @@ lexer.remove(); Element subtitle = result.addElement(SUBTITLE); String txt = item.getText(); - subtitle.addAttribute("id", txt.replaceAll("[(\\W+)_]", " ") + subtitle.addAttribute(ATTR_IDS, txt.replaceAll("[(\\W+)_]", " ") .toLowerCase().trim().replaceAll("\\s+", "-")); - subtitle.addAttribute("name", txt.toLowerCase().replaceAll( + subtitle.addAttribute(NAMES, txt.toLowerCase().replaceAll( "[(\\W+)_]", " ").trim()); copyLevel(item, subtitle); - DocumentHelper.createElement("footnotes"); - subtitle.addAttribute("inline", "true").setText(txt.trim()); + DocumentHelper.createElement(FOOTNOTES); + subtitle.addAttribute(ATTR_INLINE, TRUE).setText(txt.trim()); } // skip blank line @@ -770,56 +840,56 @@ Element field = composeFieldItemList(lexer); documentinfo.add(field); } else { - if ("author".equalsIgnoreCase(item.attributeValue("type"))) { - documentinfo.addElement(AUTHOR).addAttribute("inline", - "true").setText(item.getText()); - } else if ("date".equalsIgnoreCase(item.attributeValue("type"))) { + if ("author".equalsIgnoreCase(item.attributeValue(TYPE))) { + documentinfo.addElement(AUTHOR).addAttribute(ATTR_INLINE, + TRUE).setText(item.getText()); + } else if ("date".equalsIgnoreCase(item.attributeValue(TYPE))) { documentinfo.addElement(DATE) - .addAttribute("inline", "true").setText( + .addAttribute(ATTR_INLINE, TRUE).setText( item.getText().trim()); } else if ("organization".equalsIgnoreCase(item - .attributeValue("type"))) { + .attributeValue(TYPE))) { documentinfo.addElement(ORGANIZATION).addAttribute( - "inline", "true").setText(item.getText().trim()); + ATTR_INLINE, TRUE).setText(item.getText().trim()); } else if ("contact".equalsIgnoreCase(item - .attributeValue("type"))) { - documentinfo.addElement(CONTACT).addAttribute("inline", - "true").setText(item.getText().trim()); + .attributeValue(TYPE))) { + documentinfo.addElement(CONTACT).addAttribute(ATTR_INLINE, + TRUE).setText(item.getText().trim()); } else if ("address".equalsIgnoreCase(item - .attributeValue("type"))) { - documentinfo.addElement(ADDRESS).addAttribute("inline", - "true").setText(item.getText().trim()); + .attributeValue(TYPE))) { + documentinfo.addElement(ADDRESS).addAttribute(ATTR_INLINE, + TRUE).setText(item.getText().trim()); } else if ("version".equalsIgnoreCase(item - .attributeValue("type"))) { - documentinfo.addElement(VERSION).addAttribute("inline", - "true").setText(item.getText().trim()); + .attributeValue(TYPE))) { + documentinfo.addElement(VERSION).addAttribute(ATTR_INLINE, + TRUE).setText(item.getText().trim()); } else if ("revision".equalsIgnoreCase(item - .attributeValue("type"))) { - documentinfo.addElement(REVISION).addAttribute("inline", - "true").setText(item.getText().trim()); + .attributeValue(TYPE))) { + documentinfo.addElement(REVISION).addAttribute(ATTR_INLINE, + TRUE).setText(item.getText().trim()); } else if ("status".equalsIgnoreCase(item - .attributeValue("type"))) { - documentinfo.addElement(STATUS).addAttribute("inline", - "true").setText(item.getText().trim()); + .attributeValue(TYPE))) { + documentinfo.addElement(STATUS).addAttribute(ATTR_INLINE, + TRUE).setText(item.getText().trim()); } else if ("copyright".equalsIgnoreCase(item - .attributeValue("type"))) { - documentinfo.addElement(COPYRIGHT).addAttribute("inline", - "true").setText(item.getText().trim()); + .attributeValue(TYPE))) { + documentinfo.addElement(COPYRIGHT).addAttribute(ATTR_INLINE, + TRUE).setText(item.getText().trim()); } else if ("authors".equalsIgnoreCase(item - .attributeValue("type"))) { + .attributeValue(TYPE))) { Element authors = documentinfo.addElement(AUTHORS); int t = 0; String line = item.getText(); for (int i = 0; i < line.length(); i++) { if (line.charAt(i) == ';' || line.charAt(i) == ',') { - authors.addElement(AUTHOR).addAttribute("inline", - "true") + authors.addElement(AUTHOR).addAttribute(ATTR_INLINE, + TRUE) .setText(line.substring(t, i).trim()); t = i + 1; } } - authors.addElement(AUTHOR).addAttribute("inline", "true") + authors.addElement(AUTHOR).addAttribute(ATTR_INLINE, TRUE) .setText(line.substring(t, line.length()).trim()); } lexer.remove(); @@ -903,9 +973,9 @@ if (itemEquals(JRSTLexer.BLANK_LINE, item)) { // go to the next element lexer.remove(); - } else if (itemEquals("remove", item)) { + } else if (itemEquals(REMOVE, item)) { lexer.remove(); - } else if (itemEquals("include", item)) { + } else if (itemEquals(INCLUDE, item)) { lexer.remove(); Element list = composeInclude(item); parent.add(list); @@ -933,7 +1003,7 @@ lexer.remove(); Element para = parent.addElement(PARAGRAPH); copyLevel(item, para); - para.addAttribute("inline", "true").setText(item.getText()); + para.addAttribute(ATTR_INLINE, TRUE).setText(item.getText()); } else if (itemEquals(JRSTLexer.DIRECTIVE, item)) { lexer.remove(); Node directive = composeDirective(item); @@ -985,12 +1055,12 @@ parent.add(list); } catch (IllegalAddException e) {} } else - System.err.println("Unknown target name : \"" + item.attributeValue("id") + "\""); - } else if (itemEquals("targetAnonymous", item)) { + System.err.println("Unknown target name : \"" + item.attributeValue(ATTR_IDS) + "\""); + } else if (itemEquals(TARGETANONYMOUS, item)) { lexer.remove(); Element list = composeTargetAnonymous(item); parent.add(list); - } else if (itemEquals("footnotes", item)) { + } else if (itemEquals(FOOTNOTES, item)) { lexer.remove(); Element[] list = composeFootnote(item); for (Element l : list) { @@ -1043,10 +1113,10 @@ * @throws Exception */ private Element composeInclude(Element item) throws Exception { - String option = item.attributeValue("option"); + String option = item.attributeValue(OPTION); String path = item.getText(); Element result = null; - if (option.equals("literal")) { + if (option.equals(LITERAL)) { result = DocumentHelper.createElement(LITERAL_BLOCK); FileReader reader = new FileReader(path); BufferedReader bf = new BufferedReader(reader); @@ -1108,7 +1178,7 @@ private Element composeTarget(Element item) { Element result = null; for (Element e : eTarget) { - if (e.attributeValue("id").equals(item.attributeValue("id"))) { + if (e.attributeValue(ID).equals(item.attributeValue(ID))) { result = e; } } @@ -1126,7 +1196,7 @@ */ private Element[] composeFootnote(Element item) throws Exception { Element[] result = null; - if (itemEquals("footnotes", item)) { + if (itemEquals(FOOTNOTES, item)) { List<Element> footnotes = (List<Element>) item .selectNodes(FOOTNOTE); result = new Element[footnotes.size()]; @@ -1152,25 +1222,25 @@ String name = null; String id = ""; String label = null; - String type = footnote.attributeValue("type"); - if (type.equals("autoNum") || type.equals("autoNumLabel")) { - result[cnt].addAttribute("auto", "1"); + String type = footnote.attributeValue(TYPE); + if (type.equals(AUTONUM) || type.equals(AUTONUMLABEL)) { + result[cnt].addAttribute(AUTO, "1"); } - if (type.equals("autoSymbol")) { - result[cnt].addAttribute("auto", "*"); + if (type.equals(AUTOSYMBOL)) { + result[cnt].addAttribute(AUTO, "*"); } - result[cnt].addAttribute("backrefs", "id" + idMax); - efootnote.addAttribute("backrefs", "id" + idMax); - if (type.equals("num") || type.equals("autoNumLabel")) { - name = footnote.attributeValue("name"); - if (type.equals("autoNumLabel")) { + result[cnt].addAttribute(BACKREFS, ID + idMax); + efootnote.addAttribute(BACKREFS, ID + idMax); + if (type.equals(NUM) || type.equals(AUTONUMLABEL)) { + name = footnote.attributeValue(NAME); + if (type.equals(AUTONUMLABEL)) { id = name; } else { label = name; } } - if (type.equals("autoNum") || type.equals("autoNumLabel")) { + if (type.equals(AUTONUM) || type.equals(AUTONUMLABEL)) { boolean done = false; for (int i = 0; i < labels.length && !done; i++) { @@ -1182,11 +1252,11 @@ if (!done) { label = "" + (labels.length + 1); } - if (type.equals("autoNum")) { + if (type.equals(AUTONUM)) { name = label; } } - if (type.equals("autoSymbol")) { + if (type.equals(AUTOSYMBOL)) { int nb = Math.abs(symbolMax / 10) + 1; char symbol = FOOTNOTE_SYMBOL.charAt(symbolMax % 10); @@ -1197,18 +1267,18 @@ symbolMax++; } - result[cnt].addAttribute("id", "" + id); - efootnote.addAttribute("id", "" + id); - if (!type.equals("autoSymbol")) { - result[cnt].addAttribute("name", "" + name); - efootnote.addAttribute("name", "" + name); + result[cnt].addAttribute(ATTR_IDS, "" + id); + efootnote.addAttribute(ATTR_IDS, "" + id); + if (!type.equals(AUTOSYMBOL)) { + result[cnt].addAttribute(NAME, "" + name); + efootnote.addAttribute(NAME, "" + name); } - result[cnt].addElement("label").setText("" + label); - efootnote.addAttribute("label", "" + label); - if (!type.equals("autoSymbol")) { + result[cnt].addElement(LABEL).setText("" + label); + efootnote.addAttribute(LABEL, "" + label); + if (!type.equals(AUTOSYMBOL)) { lblFootnotes.add(Integer.parseInt(label)); } - efootnote.addAttribute("type", type); + efootnote.addAttribute(TYPE, type); eFootnotes.add(efootnote); String text = footnote.getText(); Document doc = newJRSTReader(new StringReader(text)); @@ -1218,10 +1288,10 @@ } } for (int i = 0; i < result.length; i++) { - if (result[i].attributeValue("id").equals("")) { + if (result[i].attributeValue(ATTR_IDS).equals("")) { idMax++; - result[i].addAttribute("id", "id" + idMax); - (eFootnotes.get(i)).addAttribute("id", "id" + idMax); + result[i].addAttribute(ATTR_IDS, ID + idMax); + (eFootnotes.get(i)).addAttribute(ATTR_IDS, ID + idMax); } } @@ -1248,14 +1318,14 @@ lexer.remove(); Element optionListItem = result.addElement(OPTION_LIST_ITEM); Element optionGroup = optionListItem.addElement(OPTION_GROUP); - List<Element> option = (List<Element>) item.selectNodes("option"); + List<Element> option = (List<Element>) item.selectNodes(OPTION); for (Element e : option) { Element eOption = optionGroup.addElement(OPTION); eOption.addElement(OPTION_STRING).setText( e.attributeValue(OPTION_STRING)); - if (e.attributeValue("delimiterExiste").equals("true")) { + if (e.attributeValue(DELIMITEREXISTE).equals(TRUE)) { eOption.addElement(OPTION_ARGUMENT).addAttribute( - "delimiter", e.attributeValue("delimiter")) + DELIMITER, e.attributeValue(DELIMITER)) .setText(e.attributeValue(OPTION_ARGUMENT)); } } @@ -1286,7 +1356,7 @@ private Element composeTopic(Element item) throws Exception { Element result = null; result = DocumentHelper.createElement(TOPIC); - result.addElement(TITLE).addAttribute("inline", "true").setText( + result.addElement(TITLE).addAttribute(ATTR_INLINE, TRUE).setText( item.attributeValue(TITLE)); String text = item.getText(); Document doc = newJRSTReader(new StringReader(text)); @@ -1311,10 +1381,10 @@ private Element composeSidebar(Element item) throws Exception { Element result = null; result = DocumentHelper.createElement(SIDEBAR); - result.addElement(TITLE).addAttribute("inline", "true").setText( + result.addElement(TITLE).addAttribute(ATTR_INLINE, TRUE).setText( item.attributeValue(TITLE)); - if (item.attributeValue("subExiste").equals("true")) { - result.addElement(SUBTITLE).addAttribute("inline", "true").setText( + if (item.attributeValue(SUBEXISTE).equals(TRUE)) { + result.addElement(SUBTITLE).addAttribute(ATTR_INLINE, TRUE).setText( item.attributeValue(SUBTITLE)); } @@ -1345,7 +1415,7 @@ int[] levels = new int[lines.size()]; int cnt = 0; for (Element l : lines) { - levels[cnt] = Integer.parseInt(l.attributeValue("level")); + levels[cnt] = Integer.parseInt(l.attributeValue(LEVEL)); cnt++; } cnt = 0; @@ -1355,7 +1425,7 @@ } for (Element l : lines) { if (levels[cnt] == 0) { - result.addElement(LINE).addAttribute("inline", "true").setText( + result.addElement(LINE).addAttribute(ATTR_INLINE, TRUE).setText( l.getText()); } else { @@ -1364,8 +1434,8 @@ 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)); + Element eLine = newItem.addElement(LINE); + eLine.addAttribute(LEVEL, "" + (levels[i] - 1)); eLine.setText(lines.get(i).getText()); lineDone[i] = true; } else { @@ -1422,7 +1492,7 @@ if (sAttribution != null) { Element attribution = result.addElement(ATTRIBUTION); attribution.setText(sAttribution); - attribution.addAttribute("inline", "true"); + attribution.addAttribute(ATTR_INLINE, TRUE); } return result; } @@ -1441,7 +1511,7 @@ */ private Element composeAdmonition(Element item) throws Exception { Element result = null; - if (item.attributeValue("type").equalsIgnoreCase(ADMONITION)) { + if (item.attributeValue(TYPE).equalsIgnoreCase(ADMONITION)) { result = DocumentHelper.createElement(ADMONITION); String title = item.attributeValue(TITLE); String admonitionClass = "admonition_" + title; @@ -1449,11 +1519,11 @@ "\\p{Punct}", ""); admonitionClass = admonitionClass.replace(' ', '-'); admonitionClass = admonitionClass.replace('\n', '-'); - result.addAttribute("class", admonitionClass); - result.addElement(TITLE).addAttribute("inline", "true").setText( + result.addAttribute(CLASS, admonitionClass); + result.addElement(TITLE).addAttribute(ATTR_INLINE, TRUE).setText( title.trim()); } else { - result = DocumentHelper.createElement(item.attributeValue("type") + result = DocumentHelper.createElement(item.attributeValue(TYPE) .toLowerCase()); } @@ -1472,7 +1542,7 @@ private Node composeDirective(Element item) { Node result = item; String type = item.attributeValue(JRSTLexer.DIRECTIVE_TYPE); - if (type.equals("sectnum")) { + if (type.equals(SECTNUM)) { sectnum = true; } JRSTDirective directive = getDirective(type); @@ -1579,7 +1649,7 @@ } Element rowList = null; - if ("true".equals(item.attributeValue(JRSTLexer.TABLE_HEADER))) { + if (TRUE.equals(item.attributeValue(JRSTLexer.TABLE_HEADER))) { rowList = tgroup.addElement(THEAD); } else { rowList = tgroup.addElement(TBODY); @@ -1593,7 +1663,7 @@ Element cell = cells.get(c); // si la cellule a ete utilise pour un regroupement vertical on // la passe - if (!"true".equals(cell.attributeValue("used"))) { + if (!TRUE.equals(cell.attributeValue("used"))) { Element entry = row.addElement(ENTRY); String text = ""; @@ -1611,8 +1681,8 @@ + "=" + cellStart + "]"); text += tmpCell.getText(); // on marque la cellule comme utilise - tmpCell.addAttribute("used", "true"); - } while (!"true".equals(tmpCell + tmpCell.addAttribute("used", TRUE); + } while (!TRUE.equals(tmpCell .attributeValue(JRSTLexer.CELL_END))); if (morerows > 0) { @@ -1644,7 +1714,7 @@ entry.appendContent(doc.getRootElement()); } } - if ("true".equals(rows.get(r).attributeValue( + if (TRUE.equals(rows.get(r).attributeValue( JRSTLexer.ROW_END_HEADER))) { rowList = tgroup.addElement(TBODY); } @@ -1674,13 +1744,13 @@ Element item = lexer.peekBulletList(); Element result = DocumentHelper.createElement(BULLET_LIST); copyLevel(item, result); - result.addAttribute("bullet", item.attributeValue("bullet")); + result.addAttribute(BULLET, item.attributeValue(BULLET)); while (itemEquals(BULLET_LIST, item) && isSameLevel(item, result) - && hasSameAttribute(item, result, "bullet")) { + && hasSameAttribute(item, result, BULLET)) { lexer.remove(); Element bullet = result.addElement(LIST_ITEM); copyLevel(item, bullet); - bullet.addElement(PARAGRAPH).addAttribute("inline", "true") + bullet.addElement(PARAGRAPH).addAttribute(ATTR_INLINE, TRUE) .setText(item.getText()); composeBody(lexer, bullet); @@ -1706,22 +1776,22 @@ Element item = lexer.peekEnumeratedList(); Element result = DocumentHelper.createElement(ENUMERATED_LIST); copyLevel(item, result); - String enumType = item.attributeValue("enumtype"); + String enumType = item.attributeValue(ENUMTYPE); if (!enumType.equals("arabic")) { - result.addAttribute("start", item.attributeValue("start")); + result.addAttribute(START, item.attributeValue(START)); } - result.addAttribute("prefix", item.attributeValue("prefix")); - result.addAttribute("suffix", item.attributeValue("suffix")); - result.addAttribute("enumtype", enumType); + result.addAttribute(PREFIX, item.attributeValue(PREFIX)); + result.addAttribute(SUFFIX, item.attributeValue(SUFFIX)); + result.addAttribute(ENUMTYPE, enumType); while (itemEquals(ENUMERATED_LIST, item) && isSameLevel(item, result) - && hasSameAttribute(item, result, "prefix", "suffix") - && ("auto".equals(item.attributeValue("enumtype")) || hasSameAttribute( - item, result, "enumtype"))) { + && hasSameAttribute(item, result, PREFIX, SUFFIX) + && (AUTO.equals(item.attributeValue(ENUMTYPE)) || hasSameAttribute( + item, result, ENUMTYPE))) { lexer.remove(); Element e = result.addElement(LIST_ITEM); copyLevel(item, e); - e.addElement(PARAGRAPH).addAttribute("inline", "true").setText( + e.addElement(PARAGRAPH).addAttribute(ATTR_INLINE, TRUE).setText( item.getText()); composeBody(lexer, e); @@ -1751,7 +1821,7 @@ Element term = def.addElement(TERM); copyLevel(item, term); - term.addAttribute("inline", "true").setText( + term.addAttribute(ATTR_INLINE, TRUE).setText( item.attributeValue("term")); String[] classifiers = StringUtil.split(item @@ -1759,12 +1829,12 @@ for (String classifierText : classifiers) { Element classifier = def.addElement("classifier"); copyLevel(item, classifier); - classifier.addAttribute("inline", "true").setText( + classifier.addAttribute(ATTR_INLINE, TRUE).setText( classifierText); } Element definition = def.addElement(DEFINITION); - definition.addElement(PARAGRAPH).addAttribute("inline", "true") + definition.addElement(PARAGRAPH).addAttribute(ATTR_INLINE, TRUE) .setText(item.getText()); copyLevel(item, definition); @@ -1819,10 +1889,10 @@ copyLevel(item, field); Element fieldName = field.addElement(FIELD_NAME); copyLevel(item, fieldName); - fieldName.addAttribute("inline", "true").setText( - item.attributeValue("name")); + fieldName.addAttribute(ATTR_INLINE, TRUE).setText( + item.attributeValue(NAME)); Element fieldBody = field.addElement(FIELD_BODY); - fieldBody.addElement(PARAGRAPH).addAttribute("inline", "true") + fieldBody.addElement(PARAGRAPH).addAttribute(ATTR_INLINE, TRUE) .setText(item.getText()); copyLevel(item, fieldBody); composeBody(lexer, fieldBody); @@ -1858,10 +1928,10 @@ Element title = result.addElement(TITLE); copyLevel(item, result); copyLevel(item, title); - title.addAttribute("inline", "true").setText(item.getText().trim()); - result.addAttribute("id", item.getText().replaceAll("\\W+", " ") + title.addAttribute(ATTR_INLINE, TRUE).setText(item.getText().trim()); + result.addAttribute(ID, item.getText().replaceAll("\\W+", " ") .trim().toLowerCase().replaceAll("\\W+", "-")); - result.addAttribute("name", item.getText().toLowerCase().trim()); + result.addAttribute(NAME, item.getText().toLowerCase().trim()); eTitle.add(title); } @@ -1901,8 +1971,8 @@ // return true; // } int subSectionLevel = Integer.parseInt(subSection - .attributeValue("level")); - int sectionLevel = Integer.parseInt(section.attributeValue("level")); + .attributeValue(LEVEL)); + int sectionLevel = Integer.parseInt(section.attributeValue(LEVEL)); boolean result = subSectionLevel > sectionLevel; return result; } @@ -1922,8 +1992,8 @@ // return false; // } int subSectionLevel = Integer.parseInt(subSection - .attributeValue("level")); - int sectionLevel = Integer.parseInt(section.attributeValue("level")); + .attributeValue(LEVEL)); + int sectionLevel = Integer.parseInt(section.attributeValue(LEVEL)); boolean result = subSectionLevel == sectionLevel; return result; } @@ -1958,11 +2028,11 @@ * @throws DocumentException */ private void copyLevel(Element from, Element to) throws DocumentException { - String level = from.attributeValue("level"); + String level = from.attributeValue(LEVEL); if (level == null) { throw new DocumentException("Element without level: " + from); } - to.addAttribute("level", level); + to.addAttribute(LEVEL, level); } /** @@ -2077,7 +2147,7 @@ int end = matcher.end(); String literal = "<" + LITERAL + ">" + matcher.group(1) + "</" + LITERAL + ">"; - String key = "literal" + index++; + String key = LITERAL + index++; temporaries.put(key, literal); text = text.substring(0, start) + "<tmp>" + key + "</tmp>" + text.substring(end); @@ -2092,7 +2162,7 @@ int start = matcher.start(); int end = matcher.end(); Element ref = DocumentHelper.createElement(REFERENCE); - ref.addAttribute("refuri", StringEscapeUtils.unescapeXml(matcher.group(2))); + ref.addAttribute(REFURI, StringEscapeUtils.unescapeXml(matcher.group(2))); ref.setText(StringEscapeUtils.unescapeXml(matcher.group(1))); String key = "inlineReference" + index++; temporaries.put(key, ref.asXML()); @@ -2130,15 +2200,15 @@ label += symbol; } symbolMaxRef++; - footnote.addAttribute("auto", "*"); + footnote.addAttribute(AUTO, "*"); for (int j = 0; j < eFootnotes.size(); j++) { Element eFootnote = eFootnotes.get(j); - if (eFootnote.attributeValue("label").equals(label)) { + if (eFootnote.attributeValue(LABEL).equals(label)) { - footnote.addAttribute("id", eFootnote - .attributeValue("backrefs")); - footnote.addAttribute("refid", eFootnote - .attributeValue("id")); + footnote.addAttribute(ATTR_IDS, eFootnote + .attributeValue(BACKREFS)); + footnote.addAttribute(ATTR_REFID, eFootnote + .attributeValue(ATTR_IDS)); } } @@ -2148,11 +2218,11 @@ for (int j = 0; j < eFootnotes.size(); j++) { Element eFootnote = eFootnotes.get(j); - if (eFootnote.attributeValue("label").equals(id)) { - footnote.addAttribute("id", eFootnote - .attributeValue("backrefs")); - footnote.addAttribute("refid", eFootnote - .attributeValue("id")); + if (eFootnote.attributeValue(LABEL).equals(id)) { + footnote.addAttribute(ATTR_IDS, eFootnote + .attributeValue(BACKREFS)); + footnote.addAttribute(ATTR_REFID, eFootnote + .attributeValue(ATTR_IDS)); } } footnote.setText(id); @@ -2185,17 +2255,17 @@ if (!trouve) { label = "" + (lbls.length + 1); } - footnote.addAttribute("auto", "1"); + footnote.addAttribute(AUTO, "1"); for (int j = 0; j < eFootnotes.size(); j++) { Element eFootnote = eFootnotes.get(j); - if (eFootnote.attributeValue("label").equals( + if (eFootnote.attributeValue(LABEL).equals( label)) { - if (!(eFootnote.attributeValue("type") - .equals("autoNumLabel"))) { - footnote.addAttribute("id", eFootnote - .attributeValue("backrefs")); - footnote.addAttribute("refid", - eFootnote.attributeValue("id")); + 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)); @@ -2211,26 +2281,26 @@ } else { - footnote.addAttribute("auto", "1"); + 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("name").equals(name)) { - footnote.addAttribute("id", eFootnote - .attributeValue("backrefs")); - footnote.addAttribute("refid", eFootnote - .attributeValue("id")); + 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"); + .attributeValue(LABEL); footnote.setText(label); lblFootnotesRef.add(Integer.parseInt(label)); trouve = true; } } - footnote.addAttribute("name", name); + footnote.addAttribute(NAMES, name); } done = true; } @@ -2245,13 +2315,13 @@ 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()); + 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.addAttribute(REFURI, target.attributeValue(REFURI)); } anonym.setText(ref); text = txtDebut + anonym.asXML() + txtFin; @@ -2266,19 +2336,19 @@ ref = StringEscapeUtils.unescapeXml(ref); ref = ref.replaceAll("('|_)", ""); ref = ref.replaceAll("`", ""); - Element hyper = DocumentHelper.createElement("reference"); - hyper.addAttribute("name", ref); + 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")); + if (el.attributeValue(ID).equalsIgnoreCase((refTmp))) { + hyper.addAttribute(REFURI, el.attributeValue(REFURI)); trouve = true; } } if (!trouve) { - hyper.addAttribute("refid", ref); + hyper.addAttribute(ATTR_REFID, ref); } hyper.setText(ref); text = txtDebut + hyper.asXML() + " " + txtFin;