~ubuntu-branches/ubuntu/vivid/herold/vivid

« back to all changes in this revision

Viewing changes to java/org/dbdoclet/trafo/html/docbook/editor/BrEditor.java

  • Committer: Package Import Robot
  • Author(s): Mathieu Malaterre
  • Date: 2012-11-29 13:00:52 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20121129130052-x60zqbks3mwb8nmx
Tags: 6.0.3-1
* New upstream. Closes: #692544
* option --logging-level was removed. Closes: #691170
* preserve lang attribute. Closes: #692543
* Fix herold help output. Closes: #692545
* pdftohtml support (detect-trapped-br=false to revert). Closes: #692741
* Fix invalid XML element. Closes: #693214
* Remove d/p/antencoding.patch, applied upstream
* man page is provided. Closes: #689309
* Fix herold.home location. Closes: #692547
* Support section element. Closes: #692756

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 */
19
19
package org.dbdoclet.trafo.html.docbook.editor;
20
20
 
 
21
import org.dbdoclet.service.StringServices;
 
22
import org.dbdoclet.trafo.html.docbook.DbtConstants;
 
23
import org.dbdoclet.trafo.script.Script;
21
24
import org.dbdoclet.trafo.tag.docbook.DocBookElement;
22
25
import org.dbdoclet.trafo.tag.docbook.DocBookTagFactory;
23
26
import org.dbdoclet.trafo.tag.docbook.Para;
 
27
import org.dbdoclet.trafo.tag.html.Br;
24
28
import org.dbdoclet.xiphias.dom.TextImpl;
 
29
import org.w3c.dom.Node;
 
30
import org.w3c.dom.Text;
25
31
 
26
32
public class BrEditor extends Editor {
27
33
 
28
 
    @Override
29
 
    public EditorInstruction edit(EditorInstruction values) throws EditorException {
30
 
 
31
 
        setValues(super.edit(values));
32
 
        DocBookTagFactory dbfactory = values.getTagFactory();
33
 
 
34
 
        traverse(false);
35
 
 
36
 
        if (getCurrent() instanceof Para) {
37
 
 
38
 
            DocBookElement parent = (DocBookElement) getCurrent().getParentNode();
39
 
 
40
 
            if (parent != null) {
41
 
 
42
 
                Para para = dbfactory.createPara();
43
 
                parent.appendChild(para);
44
 
 
45
 
                setParent(para);
46
 
                setCurrent(para);
47
 
            }
48
 
        } // end of if ()
49
 
        else {
50
 
 
51
 
            getCurrent().appendChild(new TextImpl("\n"));
52
 
        }
53
 
 
54
 
        return finalizeValues();
55
 
    }
 
34
        @Override
 
35
        public EditorInstruction edit(EditorInstruction values)
 
36
                        throws EditorException {
 
37
 
 
38
                setValues(super.edit(values));
 
39
                DocBookTagFactory dbfactory = values.getTagFactory();
 
40
                traverse(false);
 
41
 
 
42
                Br brElement = (Br) values.getHtmlElement();
 
43
 
 
44
                Script script = getScript();
 
45
                boolean detectTrappedBrEnabled = script.isParameterOn(
 
46
                                DbtConstants.SECTION_DOCBOOK,
 
47
                                DbtConstants.PARAM_DOCBOOK_DETECT_TRAPPED_BR, true);
 
48
 
 
49
                if (detectTrappedBrEnabled && isTrapped(brElement, values.getCurrent())) {
 
50
                        return finalizeValues();
 
51
                }
 
52
 
 
53
                if (getCurrent() instanceof Para) {
 
54
 
 
55
                        DocBookElement parent = (DocBookElement) getCurrent()
 
56
                                        .getParentNode();
 
57
 
 
58
                        if (parent != null) {
 
59
 
 
60
                                Para para = dbfactory.createPara();
 
61
                                parent.appendChild(para);
 
62
 
 
63
                                setParent(para);
 
64
                                setCurrent(para);
 
65
                        }
 
66
 
 
67
                } else {
 
68
 
 
69
                        getCurrent().appendChild(new TextImpl("\n"));
 
70
                }
 
71
 
 
72
                return finalizeValues();
 
73
        }
 
74
 
 
75
        private boolean isTrapped(Br brElement, DocBookElement current) {
 
76
 
 
77
                Node prevSibling = brElement.getPreviousSibling();
 
78
                Node nextSibling = brElement.getNextSibling();
 
79
 
 
80
                if (prevSibling == null || nextSibling == null) {
 
81
                        return false;
 
82
                }
 
83
 
 
84
                if (prevSibling instanceof Text == false
 
85
                                || nextSibling instanceof Text == false) {
 
86
                        return false;
 
87
                }
 
88
 
 
89
                String prevText = prevSibling.getTextContent();
 
90
                String nextText = nextSibling.getTextContent();
 
91
 
 
92
                if (prevText == null || nextText == null) {
 
93
                        return false;
 
94
                }
 
95
 
 
96
                if (nextText.matches("(?s)^[\\w\\u00A0].*$") == false) {
 
97
                        return false;
 
98
                }
 
99
 
 
100
                if (prevText.matches("(?s)^.*[\\w\\u00A0-]$") == false) {
 
101
                        return false;
 
102
                }
 
103
 
 
104
                if (prevText.endsWith("-")) {
 
105
 
 
106
                        Node lastChild = current.getLastChild();
 
107
 
 
108
                        if (lastChild instanceof Text) {
 
109
 
 
110
                                Text lastChildText = (Text) lastChild;
 
111
                                String buffer = lastChildText.getTextContent();
 
112
 
 
113
                                if (buffer != null && buffer.endsWith("-")) {
 
114
                                        buffer = StringServices.cutSuffix(buffer, "-");
 
115
                                        lastChildText.setTextContent(buffer);
 
116
                                }
 
117
                        }
 
118
                }
 
119
 
 
120
                return true;
 
121
        }
56
122
}