-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Cleanup DOMUtils #15053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sandeshkr419
wants to merge
2
commits into
apache:main
Choose a base branch
from
sandeshkr419:dom
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Cleanup DOMUtils #15053
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,8 @@ | |
*/ | ||
package org.apache.lucene.queryparser.xml; | ||
|
||
import java.io.Reader; | ||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
import org.xml.sax.InputSource; | ||
|
||
/** Helper methods for parsing XML */ | ||
public class DOMUtils { | ||
|
@@ -44,11 +39,7 @@ public static Element getFirstChildOrFail(Element e) throws ParserException { | |
} | ||
|
||
public static String getAttributeOrFail(Element e, String name) throws ParserException { | ||
String v = e.getAttribute(name); | ||
if (null == v) { | ||
throw new ParserException(e.getTagName() + " missing \"" + name + "\" attribute"); | ||
} | ||
return v; | ||
return e.getAttribute(name); | ||
} | ||
|
||
public static String getAttributeWithInheritanceOrFail(Element e, String name) | ||
|
@@ -61,9 +52,8 @@ public static String getAttributeWithInheritanceOrFail(Element e, String name) | |
} | ||
|
||
public static String getNonBlankTextOrFail(Element e) throws ParserException { | ||
String v = getText(e); | ||
if (null != v) v = v.trim(); | ||
if (null == v || 0 == v.length()) { | ||
String v = getText(e).trim(); | ||
if (v.isEmpty()) { | ||
throw new ParserException(e.getTagName() + " has no text"); | ||
} | ||
return v; | ||
|
@@ -86,7 +76,7 @@ public static Element getChildByTagName(Element e, String name) { | |
*/ | ||
public static String getAttributeWithInheritance(Element element, String attributeName) { | ||
String result = element.getAttribute(attributeName); | ||
if ((result == null) || (result.isEmpty())) { | ||
if (result.isEmpty()) { | ||
Node n = element.getParentNode(); | ||
if ((n == element) || (n == null)) { | ||
return null; | ||
|
@@ -99,45 +89,27 @@ public static String getAttributeWithInheritance(Element element, String attribu | |
return result; | ||
} | ||
|
||
/* Convenience method where there is only one child Element of a given name */ | ||
public static String getChildTextByTagName(Element e, String tagName) { | ||
Element child = getChildByTagName(e, tagName); | ||
return child != null ? getText(child) : null; | ||
} | ||
|
||
/* Convenience method to append a new child with text*/ | ||
public static Element insertChild(Element parent, String tagName, String text) { | ||
Element child = parent.getOwnerDocument().createElement(tagName); | ||
parent.appendChild(child); | ||
if (text != null) { | ||
child.appendChild(child.getOwnerDocument().createTextNode(text)); | ||
} | ||
return child; | ||
} | ||
|
||
public static String getAttribute(Element element, String attributeName, String deflt) { | ||
public static String getAttribute(Element element, String attributeName, String defaultValue) { | ||
String result = element.getAttribute(attributeName); | ||
return (result == null) || (result.isEmpty()) ? deflt : result; | ||
return result.isEmpty() ? defaultValue : result; | ||
} | ||
|
||
public static float getAttribute(Element element, String attributeName, float deflt) { | ||
public static float getAttribute(Element element, String attributeName, float defaultValue) { | ||
String result = element.getAttribute(attributeName); | ||
return (result == null) || (result.isEmpty()) ? deflt : Float.parseFloat(result); | ||
return result.isEmpty() ? defaultValue : Float.parseFloat(result); | ||
} | ||
|
||
public static int getAttribute(Element element, String attributeName, int deflt) { | ||
public static int getAttribute(Element element, String attributeName, int defaultValue) { | ||
String result = element.getAttribute(attributeName); | ||
return (result == null) || (result.isEmpty()) ? deflt : Integer.parseInt(result); | ||
return result.isEmpty() ? defaultValue : Integer.parseInt(result); | ||
} | ||
|
||
public static boolean getAttribute(Element element, String attributeName, boolean deflt) { | ||
public static boolean getAttribute(Element element, String attributeName, boolean defaultValue) { | ||
String result = element.getAttribute(attributeName); | ||
return (result == null) || (result.isEmpty()) ? deflt : Boolean.valueOf(result); | ||
return result.isEmpty() ? defaultValue : Boolean.parseBoolean(result); | ||
} | ||
|
||
/* Returns text of node and all child nodes - without markup */ | ||
// MH changed to Node from Element 25/11/2005 | ||
|
||
public static String getText(Node e) { | ||
StringBuilder sb = new StringBuilder(); | ||
getTextBuffer(e, sb); | ||
|
@@ -161,45 +133,12 @@ private static void getTextBuffer(Node e, StringBuilder sb) { | |
sb.append(kid.getNodeValue()); | ||
break; | ||
} | ||
case Node.ELEMENT_NODE: | ||
{ | ||
getTextBuffer(kid, sb); | ||
break; | ||
} | ||
case Node.ENTITY_REFERENCE_NODE: | ||
case Node.ELEMENT_NODE, Node.ENTITY_REFERENCE_NODE: | ||
{ | ||
getTextBuffer(kid, sb); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Helper method to parse an XML file into a DOM tree, given a reader. | ||
* | ||
* @param is reader of the XML file to be parsed | ||
* @return an org.w3c.dom.Document object | ||
*/ | ||
public static Document loadXML(Reader is) { | ||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder db = null; | ||
|
||
try { | ||
db = dbf.newDocumentBuilder(); | ||
} catch (Exception se) { | ||
throw new RuntimeException("Parser configuration error", se); | ||
} | ||
|
||
// Step 3: parse the input file | ||
org.w3c.dom.Document doc = null; | ||
try { | ||
Comment on lines
-184
to
-196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am assuming this method is unused, right? Even then, we cannot remove public method from public class without deprecating, as it might break clients |
||
doc = db.parse(new InputSource(is)); | ||
// doc = db.parse(is); | ||
} catch (Exception se) { | ||
throw new RuntimeException("Error parsing file:" + se, se); | ||
} | ||
|
||
return doc; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same goes for these public methods