/** * Returns a list of childs having the requested label and an non-empty value, and no error. * * <p>Can get directly grandchilds if label contains <tt>/</tt> e.g. * titleFields.getChild("FieldTitle/TexText") returns all grandchilds "TexText" in all * "FieldTitle" childs. */ public List getValuedChilds(String label) { List ret = getChilds(label); Iterator it = ret.iterator(); while (it.hasNext()) { AbcNode node = (AbcNode) it.next(); if (node.hasError() || (node.getValue() == null) || (node.getValue().length() == 0)) it.remove(); } return ret; }
/** * Look for child, grandchild, grand-grand-child having the requested label. When such one is * found, doesn't continue search into his own childs. * * @param label */ public List getChildsInAllGenerations(String label) { if (label == null || label.equals("")) return new ArrayList(0); List ret = new ArrayList(); Iterator it = childs.iterator(); while (it.hasNext()) { AbcNode abcn = (AbcNode) it.next(); if (abcn.getLabel().equals(label)) { ret.add(abcn); } else { ret.addAll(abcn.getChildsInAllGenerations(label)); } } return ret; }
/** * Return the deepest childs of this node. This is useful to browse easily smallest segments of * parsed text, and get closer to errors. * * <p>e.g. node A has childs B and C.<br> * B has 3 childs D, E and F.<br> * A.getDeepestChilds() returns D, E, F, C in this order. * * <p>If it goes too deep, you can check if node is child of B or C using {@link * #isChildOf(String)} or {@link #isChildOf_or_is(String)}. * * @return a List of node */ public List getDeepestChilds() { if (childs.size() == 0) { return new ArrayList(0); } List ret = new ArrayList(childs.size() * 3); Iterator it = getChilds().iterator(); while (it.hasNext()) { AbcNode child = (AbcNode) it.next(); if (!child.hasChilds()) ret.add(child); else { ret.addAll(child.getDeepestChilds()); } } return ret; }
/** * Returns the first child having the request label, <code>null</code> if doesn't exist. * * <p>Can get directly a grandchild if label contains <tt>/</tt> e.g. * abcHeader.getChild("FieldNumber/DIGITS") returns the grandchild "DIGITS" if exist in * FieldNumber child. * * @param label One of {@link AbcTokens} constants */ public AbcNode getChild(String label) { if (label == null || label.equals("")) return null; String[] generation = label.split("/"); String child = generation[0]; String grandchild = ""; if (generation.length > 1) { for (int i = 1; i < generation.length; i++) { grandchild += (grandchild.equals("") ? "" : "/"); grandchild += generation[i]; } } Iterator it = childs.iterator(); while (it.hasNext()) { AbcNode abcn = (AbcNode) it.next(); if (child.equals(abcn.getLabel())) { if (grandchild.equals("")) return abcn; else return abcn.getChild(grandchild); } } return null; }
/** * Returns a list of childs having the requested label * * <p>Can get directly grandchilds if label contains <tt>/</tt> e.g. * titleFields.getChild("FieldTitle/TexText") returns all grandchilds "TexText" in all * "FieldTitle" childs. */ public List getChilds(String label) { if (label == null || label.equals("")) return new ArrayList(0); String[] generation = label.split("/"); String child = generation[0]; String grandchild = ""; if (generation.length > 1) { for (int i = 1; i < generation.length; i++) { grandchild += (grandchild.equals("") ? "" : "/"); grandchild += generation[i]; } } List ret = new ArrayList(childs.size()); Iterator it = childs.iterator(); while (it.hasNext()) { AbcNode abcn = (AbcNode) it.next(); if (abcn.getLabel().equals(child)) { if (grandchild.equals("")) ret.add(abcn); else ret.addAll(abcn.getChilds(grandchild)); } } return ret; }
// @SuppressWarnings("unchecked") protected AbcNode( Node node, InputBuffer parseInputBuffer, List<ParseError> parseErrors, AbcInputBuffer abcInputBuffer) { super(null); if (node != null) { this.label = node.getLabel(); this.value = parseInputBuffer.extract(node.getStartIndex(), node.getEndIndex()); Position pos = parseInputBuffer.getPosition(node.getStartIndex()); int sourceStartIndex = abcInputBuffer.getIndex(pos); int sourceEndIndex = sourceStartIndex + value.length(); setCharStreamPosition( new CharStreamPosition(pos.line, pos.column, sourceStartIndex, sourceEndIndex)); this.childs = new ArrayList(node.getChildren().size()); Iterator it = node.getChildren().iterator(); while (it.hasNext()) { AbcNode abcn = new AbcNode((Node) it.next(), parseInputBuffer, parseErrors, abcInputBuffer); abcn.parent = this; childs.add(abcn); } if (!hasError() /*(childs.size() == 0)*/ && node.hasError()) { this.errors = new ArrayList(); it = parseErrors.iterator(); while (it.hasNext()) { ParseError pe = (ParseError) it.next(); String peValue = pe.getInputBuffer().extract(pe.getStartIndex(), pe.getEndIndex()); String peMsg = pe.getErrorMessage(); Position pePos = pe.getInputBuffer().getPosition(pe.getStartIndex()); int peIndex = abcInputBuffer.getIndex(pePos); CharStreamPosition csp = new CharStreamPosition( pePos.line, pePos.column, peIndex, peIndex + (peValue.length() > 0 ? peValue.length() : 1)); // if ((pe.getStartIndex() >= node.getStartIndex()) // && (pe.getStartIndex() </*=*/ node.getEndIndex())) { if ((peIndex >= sourceStartIndex) && ((peIndex < sourceEndIndex) || (sourceStartIndex == sourceEndIndex))) { errors.add(new AbcParseError(peMsg, peValue, csp)); } } } } else { this.label = "AbcFile-Error"; this.value = ""; int nbL = parseInputBuffer.getLineCount(); for (int i = 1; i <= nbL; i++) this.value += parseInputBuffer.extractLine(i) + "\n"; this.childs = new ArrayList(0); setCharStreamPosition(new CharStreamPosition(1, 1, 0, 1)); if (parseErrors != null) { this.errors = new ArrayList(); Iterator it = parseErrors.iterator(); while (it.hasNext()) { ParseError pe = (ParseError) it.next(); errors.add(new AbcParseError(pe.getErrorMessage(), value, getCharStreamPosition())); } } } }