/**
 * Ausgabe der mitgespeicherten Resultate. Vorgehen: Der Resultat-Baum wird
 * top-down abgearbeitet- zuerst die Content-Kanten (vertikal). Ab dem Element,
 * ohne Content werden dann (horizontal) die Next-Kanten abgearbeitet. Element
 * mit content und next wird wie oben bearbeitet (zuerst Contet-Kante etc. ...),
 * und auf dem Stack ge-pusht. Wenn ein next-Strang zu Ende geht, fängt man mit
 * dem next-Strang des Elements auf dem Stack.
 */
public class PrintAll {
	/** Sammelt die Ausgabe für MySAXParser */
	static StringBuffer outputBuffer = null;

	// Kann der Element-Name (und potenziell die Elememnt-Attribute) zur Ausgabe vorbereiten.
	static void printElement(Element el) {
		outputBuffer.append("<code>&lt;" + el.name + "&gt;</code> ");
		//		System.out.println("<" + el.name + ">");
		//		-------------- Ausgabe Attribute ----------
		//		if (el.attribut != null) {
		//			for (int i = 0; i < el.attribut.length; i++) {
		//				System.out.println("\t" + (el.attribut[i]).name + ": " + "\t"
		//						+ (el.attribut[i]).value);
		//			}
		//		}
	}

	Stack stack = new Stack();

	PrintAll(Content start) {
		// "el" kommt als erster auf dem Stack
		Element el = new Element();
		stack.push(el);
		outputBuffer = new StringBuffer("<li> ");
		try {
			printAll(start);
		} catch (Exception e) {
			outputBuffer.append("<p> " + "Problem in der Klasse PrintAll.java"
					+ "\n" + " </p>");
		}
		outputBuffer.append(" </li>" + "\n");
	}

	public void printAll(Content start) {
		// Run-time Type Identification (RTTI)
		//=============== Content ======================================
		if (start.getClass() != Element.class) { // folglich Content
			printContent(start); // Ausgabe des Element-Inhalts
			if (start.next != null) {
				printAll(start.next);
			} else {
				if ((stack.top()).name != null) {
					Content temp = (stack.top()).next;
					stack.pop();
					printAll(temp);
				}
			}
			// ===================== Element ================================
		} else {
			Element start_el = (Element) start;
			printElement(start_el); // Ausgabe des Element-Inhalts
			if (start_el.next != null && start_el.content != null) {
				stack.push(start_el);
				// zum späteren Verarbeiten des Next-Strangs
				printAll(start_el.content);
			} else if (start_el.next != null) {
				printAll(start_el.next);
			} else if (start_el.content != null) {
				printAll(start_el.content);
			} else {
				// kann vorkommen bei leerem Element
				Content temp = (stack.top()).next;
				stack.pop();
				printAll(temp);
			}
		}
	}

	protected void printContent(Content cont) {
		// ev. andere Content-Arten einbauen ...
		ContentData cont_d = (ContentData) cont;
		outputBuffer.append(" " + cont_d.data + " ");
	}
}