All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nosi.core.xml.XMLWritter Maven / Gradle / Ivy

Go to download

IGRP Framework is a powerful and highly customizable platform developed by the Operational Nucleus for the Information Society (NOSi) to create web applications, it provides out of box, several modules to make easy to create stand-alone, production-grade web applications: authentication and access-control, business processes automation, reporting, page builder with automatic code generation and incorporation of the Once-Only-Principle, written in Java. IGRP Framework WAR - Contains some keys resources that give UI to IGRP Framework and others supports files.

There is a newer version: 2.0.0.241121-RCM
Show newest version
package nosi.core.xml;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.text.StringEscapeUtils;

/**
 * @author: Emanuel Pereira
 * 

* Apr 11, 2017 *

* Description: class to build xml */ public class XMLWritter { protected ArrayList listXml; private HashMap countAttr; private final StringBuilder xmlConstruct; private static final String LINE_SEPARATOR = ""; public XMLWritter() { this.listXml = new ArrayList<>(); this.countAttr = new HashMap<>(); this.xmlConstruct = new StringBuilder(); } public XMLWritter(String rootElement, String xslPath) { this(); final String xslPathResolved = this.resolvePath(xslPath); this.xmlConstruct.append(""); this.xmlConstruct.append(LINE_SEPARATOR); this.xmlConstruct.append(""); this.xmlConstruct.append(LINE_SEPARATOR); this.startElement(rootElement); this.closeLarger(); this.xmlConstruct.append(LINE_SEPARATOR); } private String resolvePath(String xslPath) { if (xslPath == null) return ""; return xslPath.contains("&") ? xslPath : xslPath.replace("&", "&"); } public void startElement(String tag) { this.closeLarger(); this.countAttr.put(tag, true); if (!this.listXml.isEmpty()) { this.xmlConstruct.append(LINE_SEPARATOR); String identityString = ""; this.xmlConstruct.append(identityString); } this.xmlConstruct.append("<").append(tag); this.listXml.add(tag); } public void text(String text) { this.closeLarger(); this.xmlConstruct.append(StringEscapeUtils.escapeXml11(text)); } public void emptyTag(String tag) { this.closeLarger(); this.xmlConstruct.append("<").append(tag).append("/>"); } public void writeAttribute(String key, String value) { this.xmlConstruct.append(" ").append(key).append("=\"").append(StringEscapeUtils.escapeXml11(value)).append("\""); } public void setElement(String tag, String value) { if (value != null && !value.isEmpty()) { this.startElement(tag); this.text(value); this.countAttr.put(tag, false); this.endElement(); return; } this.emptyTag(tag); } public void setElement(String tag, Object value) { if (value != null && !value.equals("")) { this.setElement(tag, value.toString()); return; } this.emptyTag(tag); } private void closeLarger() { try { final String key = this.listXml.get(this.listXml.size() - 1); if (Boolean.TRUE.equals(this.countAttr.get(key))) { this.xmlConstruct.append(">"); this.countAttr.remove(key); } } catch (Exception ignored) { } } public void endElement() { try { final int index = this.listXml.size() - 1; final String tag = this.listXml.get(index); this.closeLarger(); this.xmlConstruct.append(""); this.xmlConstruct.append(LINE_SEPARATOR); this.listXml.remove(index); } catch (Exception ignored) { } } //Add xml string public void addXml(String xml) { this.closeLarger(); this.xmlConstruct.append("\n"); this.xmlConstruct.append(xml); } public String getXml() { if (!this.listXml.isEmpty()) { for (int i = this.listXml.size() - 1; i >= 0; i--) { this.xmlConstruct.append(LINE_SEPARATOR).append(""); } this.listXml = null; this.countAttr = null; } return this.xmlConstruct.toString(); } @Override public String toString() { return this.getXml(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy