Skip to content
This repository
Browse code

Remove duplicate TextHelper functionality. Move tests of Format to Te…

…stElement
  • Loading branch information...
commit c1c63c500a3d0737c081b3eb5616b17767d965e6 1 parent be5e939
Rolf rolfl authored
224 core/src/java/org/jdom2/util/TextHelper.java
... ... @@ -1,224 +0,0 @@
1   -/*--
2   -
3   - Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
4   - All rights reserved.
5   -
6   - Redistribution and use in source and binary forms, with or without
7   - modification, are permitted provided that the following conditions
8   - are met:
9   -
10   - 1. Redistributions of source code must retain the above copyright
11   - notice, this list of conditions, and the following disclaimer.
12   -
13   - 2. Redistributions in binary form must reproduce the above copyright
14   - notice, this list of conditions, and the disclaimer that follows
15   - these conditions in the documentation and/or other materials
16   - provided with the distribution.
17   -
18   - 3. The name "JDOM" must not be used to endorse or promote products
19   - derived from this software without prior written permission. For
20   - written permission, please contact <request_AT_jdom_DOT_org>.
21   -
22   - 4. Products derived from this software may not be called "JDOM", nor
23   - may "JDOM" appear in their name, without prior written permission
24   - from the JDOM Project Management <request_AT_jdom_DOT_org>.
25   -
26   - In addition, we request (but do not require) that you include in the
27   - end-user documentation provided with the redistribution and/or in the
28   - software itself an acknowledgement equivalent to the following:
29   - "This product includes software developed by the
30   - JDOM Project (http://www.jdom.org/)."
31   - Alternatively, the acknowledgment may be graphical using the logos
32   - available at http://www.jdom.org/images/logos.
33   -
34   - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35   - WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37   - DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
38   - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39   - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40   - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41   - USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42   - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43   - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44   - OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45   - SUCH DAMAGE.
46   -
47   - This software consists of voluntary contributions made by many
48   - individuals on behalf of the JDOM Project and was originally
49   - created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
50   - Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
51   - on the JDOM Project, please see <http://www.jdom.org/>.
52   -
53   - */
54   -
55   -package org.jdom2.util;
56   -
57   -import org.jdom2.Element;
58   -import org.jdom2.Namespace;
59   -import org.jdom2.output.Format;
60   -
61   -/**
62   - * <p>
63   - * This class contains static helper methods for accessing Text content of JDOM
64   - * Elements.
65   - * </p>
66   - *
67   - * @author Alex Rosen
68   - * @author Rolf Lear
69   - */
70   -public class TextHelper {
71   -
72   - private TextHelper() {
73   - // make constructor inaccessible.
74   - }
75   -
76   - /**
77   - * <p>
78   - * This convenience method returns the textual content of the first child
79   - * element with the given name and in no Namespace, or returns an empty
80   - * <code>String</code> ("") if the child has no textual content. However, if
81   - * the child does not exist, <code>null</code> is returned.
82   - * </p>
83   - *
84   - * @param parent
85   - * the parent element
86   - * @param name
87   - * the name of the child
88   - * @return text content for the named child, or null if no such child exists
89   - * @see Element#getChild(String)
90   - */
91   - public static String getChildText(final Element parent, final String name) {
92   - final Element child = parent.getChild(name);
93   - if (child == null) {
94   - return null;
95   - }
96   - return child.getText();
97   - }
98   -
99   - /**
100   - * <p>
101   - * This convenience method returns the textual content of the first named
102   - * child element, or returns an empty <code>String</code> ("") if the child
103   - * has no textual content. However, if the child does not exist,
104   - * <code>null</code> is returned.
105   - * </p>
106   - *
107   - * @param parent
108   - * the parent element
109   - * @param name
110   - * the name of the child
111   - * @param ns
112   - * the namespace of the child
113   - * @return text content for the named child, or null if no such child exists
114   - * @see Element#getChild(String, Namespace)
115   - */
116   - public static String getChildText(final Element parent, final String name,
117   - final Namespace ns) {
118   - final Element child = parent.getChild(name, ns);
119   - if (child == null) {
120   - return null;
121   - }
122   - return child.getText();
123   - }
124   -
125   - /**
126   - * <p>
127   - * This convenience method returns the trimmed textual content of the first
128   - * named child element, or returns null if there's no such child. See
129   - * {@link Format#trimBoth(String)} for details of text trimming.
130   - * </p>
131   - *
132   - * @param parent
133   - * the parent element
134   - * @param name
135   - * the name of the child
136   - * @return trimmed text content for the named child, or null if no such
137   - * child exists
138   - * @see Element#getChild(String)
139   - */
140   - public static String getChildTextTrim(final Element parent,
141   - final String name) {
142   - final Element child = parent.getChild(name);
143   - if (child == null) {
144   - return null;
145   - }
146   - return Format.trimBoth(child.getText());
147   - }
148   -
149   - /**
150   - * <p>
151   - * This convenience method returns the trimmed textual content of the named
152   - * child element, or returns null if there's no such child. See
153   - * <code>String.trim()</code> for details of text trimming.
154   - * </p>
155   - *
156   - * @param parent
157   - * the parent element
158   - * @param name
159   - * the name of the child
160   - * @param ns
161   - * the namespace of the child
162   - * @return trimmed text content for the named child, or null if no such
163   - * child exists
164   - * @see Element#getChild(String, Namespace)
165   - */
166   - public static String getChildTextTrim(final Element parent,
167   - final String name, final Namespace ns) {
168   - final Element child = parent.getChild(name, ns);
169   - if (child == null) {
170   - return null;
171   - }
172   - return child.getText().trim();
173   - }
174   -
175   - /**
176   - * <p>
177   - * This convenience method returns the normalized textual content of the
178   - * named child element, or returns null if there's no such child. See
179   - * {@link Format#compact(String)} for details of text normalization.
180   - * </p>
181   - *
182   - * @param parent
183   - * the parent element
184   - * @param name
185   - * the name of the child
186   - * @return normalized text content for the named child, or null if no such
187   - * child exists
188   - * @see Element#getChild(String)
189   - */
190   - public static String getChildTextNormalize(final Element parent,
191   - final String name) {
192   - final Element child = parent.getChild(name);
193   - if (child == null) {
194   - return null;
195   - }
196   - return Format.compact(child.getText());
197   - }
198   -
199   - /**
200   - * <p>
201   - * This convenience method returns the normalized textual content of the
202   - * named child element, or returns null if there's no such child. See
203   - * {@link Format#compact(String)} for details of text normalization.
204   - * </p>
205   - *
206   - * @param parent
207   - * the parent element
208   - * @param name
209   - * the name of the child
210   - * @param ns
211   - * the namespace of the child
212   - * @return normalized text content for the named child, or null if no such
213   - * child exists
214   - * @see Element#getChild(String, Namespace)
215   - */
216   - public static String getChildTextNormalize(final Element parent,
217   - final String name, final Namespace ns) {
218   - final Element child = parent.getChild(name, ns);
219   - if (child == null) {
220   - return null;
221   - }
222   - return Format.compact(child.getText());
223   - }
224   -}
99 test/src/java/org/jdom2/test/cases/TestElement.java
@@ -101,6 +101,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
101 101 import org.jdom2.filter.ContentFilter;
102 102 import org.jdom2.filter.ElementFilter;
103 103 import org.jdom2.filter.Filters;
  104 +import org.jdom2.output.Format;
104 105 import org.jdom2.output.XMLOutputter;
105 106 import org.jdom2.test.util.UnitTestUtil;
106 107
@@ -2886,4 +2887,102 @@ private void checkAttOrder(List<Attribute> attributes, Attribute...atts) {
2886 2887
2887 2888 }
2888 2889
  2890 + private String getTestString() {
  2891 + return " this has space ";
  2892 + }
  2893 +
  2894 + private String getComp() {
  2895 + return Format.compact(getTestString());
  2896 + }
  2897 +
  2898 + private String getTrim() {
  2899 + return Format.trimBoth(getTestString());
  2900 + }
  2901 +
  2902 + private String getPlain() {
  2903 + return getTestString();
  2904 + }
  2905 +
  2906 + private Namespace getNamespace() {
  2907 + return Namespace.getNamespace("jdomtest");
  2908 + }
  2909 +
  2910 + public Element getTextHelperRoot() {
  2911 + final Namespace ns = getNamespace();
  2912 +
  2913 + final Element root = new Element("root");
  2914 + final Element childa = new Element("child");
  2915 + final Element childb = new Element("child", ns);
  2916 + final Element childc = new Element("child");
  2917 + final Element childd = new Element("child", ns);
  2918 + final Element childe = new Element("kid");
  2919 + final Element childf = new Element("kid", ns);
  2920 +
  2921 + childa.setText(getTestString());
  2922 + childb.setText(getTestString());
  2923 + childc.setText(getTestString());
  2924 + childd.setText(getTestString());
  2925 + root.addContent(childa);
  2926 + root.addContent(childb);
  2927 + root.addContent(childc);
  2928 + root.addContent(childd);
  2929 + root.addContent(childe);
  2930 + root.addContent(childf);
  2931 +
  2932 + return root;
  2933 + }
  2934 +
  2935 + @Test
  2936 + public void testGetChildTextElementString() {
  2937 + Element root = getTextHelperRoot();
  2938 + assertEquals(getPlain(), root.getChildText("child"));
  2939 + assertEquals(null, root.getChildText("dummy"));
  2940 + assertEquals("", root.getChildText("kid"));
  2941 + }
  2942 +
  2943 + @Test
  2944 + public void testGetChildTextElementStringNamespace() {
  2945 + Element root = getTextHelperRoot();
  2946 + Namespace ns = getNamespace();
  2947 + assertEquals(getPlain(), root.getChildText("child", ns));
  2948 + assertEquals(null, root.getChildText("dummy", ns));
  2949 + assertEquals("", root.getChildText("kid", ns));
  2950 + }
  2951 +
  2952 + @Test
  2953 + public void testGetChildTextTrimElementString() {
  2954 + Element root = getTextHelperRoot();
  2955 + assertEquals(getTrim(), root.getChildTextTrim("child"));
  2956 + assertEquals(null, root.getChildTextTrim("dummy"));
  2957 + assertEquals("", root.getChildTextTrim("kid"));
  2958 + }
  2959 +
  2960 + @Test
  2961 + public void testGetChildTextTrimElementStringNamespace() {
  2962 + Element root = getTextHelperRoot();
  2963 + Namespace ns = getNamespace();
  2964 + assertEquals(getTrim(), root.getChildTextTrim("child", ns));
  2965 + assertEquals(null, root.getChildTextTrim("dummy", ns));
  2966 + assertEquals("", root.getChildTextTrim("kid", ns));
  2967 + }
  2968 +
  2969 + @Test
  2970 + public void testGetChildTextNormalizeElementString() {
  2971 + Element root = getTextHelperRoot();
  2972 + assertEquals(getComp(), root.getChildTextNormalize("child"));
  2973 + assertEquals(null, root.getChildTextNormalize("dummy"));
  2974 + assertEquals("", root.getChildTextNormalize("kid"));
  2975 + }
  2976 +
  2977 + @Test
  2978 + public void testGetChildTextNormalizeElementStringNamespace() {
  2979 + Element root = getTextHelperRoot();
  2980 + Namespace ns = getNamespace();
  2981 + assertEquals(getComp(), root.getChildTextNormalize("child", ns));
  2982 + assertEquals(null, root.getChildTextNormalize("dummy", ns));
  2983 + assertEquals("", root.getChildTextNormalize("kid", ns));
  2984 + }
  2985 +
  2986 +
  2987 +
2889 2988 }
84 test/src/java/org/jdom2/test/cases/util/TestTextHelper.java
... ... @@ -1,84 +0,0 @@
1   -package org.jdom2.test.cases.util;
2   -
3   -import static org.junit.Assert.*;
4   -
5   -import org.junit.Test;
6   -
7   -import org.jdom2.Element;
8   -import org.jdom2.Namespace;
9   -import org.jdom2.output.Format;
10   -import org.jdom2.util.TextHelper;
11   -
12   -@SuppressWarnings("javadoc")
13   -public class TestTextHelper {
14   -
15   - private final Namespace ns = Namespace.getNamespace("jdomtest");
16   -
17   - private final Element root = new Element("root");
18   - private final Element childa = new Element("child");
19   - private final Element childb = new Element("child", ns);
20   - private final Element childc = new Element("child");
21   - private final Element childd = new Element("child", ns);
22   - private final Element childe = new Element("kid");
23   - private final Element childf = new Element("kid", ns);
24   - private final String teststr = " this has space ";
25   - private final String comp = Format.compact(teststr);
26   - private final String trim = Format.trimBoth(teststr);
27   - private final String plain = teststr;
28   -
29   - public TestTextHelper() {
30   - childa.setText(teststr);
31   - childb.setText(teststr);
32   - childc.setText(teststr);
33   - childd.setText(teststr);
34   - root.addContent(childa);
35   - root.addContent(childb);
36   - root.addContent(childc);
37   - root.addContent(childd);
38   - root.addContent(childe);
39   - root.addContent(childf);
40   - }
41   -
42   - @Test
43   - public void testGetChildTextElementString() {
44   - assertEquals(plain, TextHelper.getChildText(root, "child"));
45   - assertEquals(null, TextHelper.getChildText(root, "dummy"));
46   - assertEquals("", TextHelper.getChildText(root, "kid"));
47   - }
48   -
49   - @Test
50   - public void testGetChildTextElementStringNamespace() {
51   - assertEquals(plain, TextHelper.getChildText(root, "child", ns));
52   - assertEquals(null, TextHelper.getChildText(root, "dummy", ns));
53   - assertEquals("", TextHelper.getChildText(root, "kid", ns));
54   - }
55   -
56   - @Test
57   - public void testGetChildTextTrimElementString() {
58   - assertEquals(trim, TextHelper.getChildTextTrim(root, "child"));
59   - assertEquals(null, TextHelper.getChildTextTrim(root, "dummy"));
60   - assertEquals("", TextHelper.getChildTextTrim(root, "kid"));
61   - }
62   -
63   - @Test
64   - public void testGetChildTextTrimElementStringNamespace() {
65   - assertEquals(trim, TextHelper.getChildTextTrim(root, "child", ns));
66   - assertEquals(null, TextHelper.getChildTextTrim(root, "dummy", ns));
67   - assertEquals("", TextHelper.getChildTextTrim(root, "kid", ns));
68   - }
69   -
70   - @Test
71   - public void testGetChildTextNormalizeElementString() {
72   - assertEquals(comp, TextHelper.getChildTextNormalize(root, "child"));
73   - assertEquals(null, TextHelper.getChildTextNormalize(root, "dummy"));
74   - assertEquals("", TextHelper.getChildTextNormalize(root, "kid"));
75   - }
76   -
77   - @Test
78   - public void testGetChildTextNormalizeElementStringNamespace() {
79   - assertEquals(comp, TextHelper.getChildTextNormalize(root, "child", ns));
80   - assertEquals(null, TextHelper.getChildTextNormalize(root, "dummy", ns));
81   - assertEquals("", TextHelper.getChildTextNormalize(root, "kid", ns));
82   - }
83   -
84   -}

0 comments on commit c1c63c5

Please sign in to comment.
Something went wrong with that request. Please try again.