001: package net.sf.jmoney.reconciliation.parser;
002:
003: /*
004: * @(#)SimpleElement.java
005: */
006:
007: import java.util.HashMap;
008: import java.util.LinkedList;
009: import java.util.regex.Matcher;
010: import java.util.regex.Pattern;
011:
012: /**
013: * <code>SimpleElement</code> is the only node type for simplified DOM model.
014: */
015: public class SimpleElement {
016: private String tagName;
017: private String text;
018:
019: private HashMap<String, String> attributes;
020: private LinkedList<SimpleElement> childElements;
021: private SimpleElement nextSibling;
022:
023: public SimpleElement(String tagName) {
024: this .tagName = tagName;
025: attributes = new HashMap<String, String>();
026: childElements = new LinkedList<SimpleElement>();
027: }
028:
029: public String getTagName() {
030: return tagName;
031: }
032:
033: public void setTagName(String tagName) {
034: this .tagName = tagName;
035: }
036:
037: public String getText() {
038: return text;
039: }
040:
041: public String getTrimmedText() {
042: if (isEmptyText())
043: return "";
044: else {
045: return text.trim();
046: // Matcher m = onlyWhiteSpacePattern.matcher(text);
047: // if (m.matches())
048: // return m.group(2);
049: // else
050: // return text;
051: }
052: }
053:
054: public void setText(String text) {
055: this .text = text;
056: }
057:
058: public boolean isEmptyText() {
059: if (text == null || text.length() == 0)
060: return true;
061: // if string only contains newlines and whitespace -> consider it as
062: // empty string
063: Matcher m = onlyWhiteSpacePattern.matcher(text);
064: return !m.matches();
065: }
066:
067: public String getAttribute(String name) {
068: return attributes.get(name);
069: }
070:
071: public void setAttribute(String name, String value) {
072: attributes.put(name, value);
073: }
074:
075: public SimpleElement getNextSibling() {
076: return nextSibling;
077: }
078:
079: public void setNextSibling(SimpleElement nextSibling) {
080: this .nextSibling = nextSibling;
081: }
082:
083: public void addChildElement(SimpleElement element) {
084: if (!childElements.isEmpty())
085: childElements.getLast().setNextSibling(element);
086: childElements.add(element);
087: }
088:
089: public LinkedList<SimpleElement> getChildElements() {
090: return childElements;
091: }
092:
093: @Override
094: public String toString() {
095: return "<" + tagName + ">" + getTrimmedText() + "</" + tagName
096: + ">";
097: }
098:
099: public static String newline = System.getProperty("line.separator");
100:
101: private static final Pattern onlyWhiteSpacePattern = Pattern
102: .compile("(\\s*)([^\\s]+)(.*)", Pattern.DOTALL);
103:
104: public String toXMLString(int spaces) {
105: String trimString = getTrimmedText();
106: StringBuffer sb = new StringBuffer(trimString.length()
107: + tagName.length() + 2 + 3 * spaces);
108: appendWhiteSpace(spaces, sb);
109: sb.append("<" + tagName + ">" + newline);
110: spaces += 2;
111: if (!isEmptyText()) {
112: appendWhiteSpace(spaces, sb);
113: sb.append(trimString);
114: appendWhiteSpace(spaces, sb);
115: sb.append(newline);
116: }
117: for (SimpleElement element : getChildElements()) {
118: sb.append(element.toXMLString(spaces));
119: }
120: spaces -= 2;
121: appendWhiteSpace(spaces, sb);
122: sb.append("</" + tagName + ">" + newline);
123: return sb.toString();
124: }
125:
126: private void appendWhiteSpace(int spaces, StringBuffer sb) {
127: for (int i = 0; i < spaces; i++) {
128: sb.append(' ');
129: }
130: }
131:
132: /**
133: * Returns the child-element of the current element with the given tagName
134: *
135: * @param tagToFind
136: * @return a child SimpleElement with tagName = tagToFind
137: */
138: public SimpleElement findElement(String tagToFind) {
139: SimpleElement found = null;
140: if (getTagName().equalsIgnoreCase(tagToFind))
141: return this ;
142: for (SimpleElement element : getChildElements()) {
143: if (found != null)
144: break;
145: found = element.findElement(tagToFind);
146: }
147: return found;
148: }
149: }
|