262 lines
9.4 KiB
Java
262 lines
9.4 KiB
Java
package main;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.io.PrintWriter;
|
|
import java.util.Properties;
|
|
import java.util.Scanner;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import javax.xml.transform.OutputKeys;
|
|
import javax.xml.transform.Transformer;
|
|
import javax.xml.transform.TransformerFactory;
|
|
import javax.xml.transform.dom.DOMSource;
|
|
import javax.xml.transform.stream.StreamResult;
|
|
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.Element;
|
|
import org.w3c.dom.Node;
|
|
import org.w3c.dom.NodeList;
|
|
|
|
public class Main {
|
|
|
|
static String rutaXml = "informacion-IES_v2.xml";
|
|
|
|
public static void main(String[] args) {
|
|
cargarConfiguracion();
|
|
Scanner sc = new Scanner(System.in);
|
|
int opcion = -1;
|
|
|
|
do {
|
|
System.out.println();
|
|
System.out.println("=== TRATAMIENTO XML CON DOM ===");
|
|
System.out.println("1. Crear info_IES.xml");
|
|
System.out.println("2. Crear resumen_ciclos_IES.xml");
|
|
System.out.println("3. Crear modulos1_ciclo_DAM.xml");
|
|
System.out.println("4. Crear informe_ciclos_IES.txt");
|
|
System.out.println("0. Salir");
|
|
System.out.print("Opcion: ");
|
|
|
|
try {
|
|
opcion = Integer.parseInt(sc.nextLine().trim());
|
|
} catch (NumberFormatException e) {
|
|
opcion = -1;
|
|
}
|
|
|
|
try {
|
|
if (opcion == 1) {
|
|
crearInfoIes();
|
|
} else if (opcion == 2) {
|
|
crearResumenCiclos();
|
|
} else if (opcion == 3) {
|
|
crearModulosDam();
|
|
} else if (opcion == 4) {
|
|
crearInformeTxt();
|
|
} else if (opcion == 0) {
|
|
System.out.println("Hasta luego.");
|
|
} else {
|
|
System.out.println("Opcion no valida.");
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println("Error: " + e.getMessage());
|
|
}
|
|
} while (opcion != 0);
|
|
|
|
sc.close();
|
|
}
|
|
|
|
static void crearInfoIes() throws Exception {
|
|
Element raizOrigen = leerXml();
|
|
|
|
Document doc = nuevoDocumento();
|
|
Element instituto = doc.createElement("instituto");
|
|
doc.appendChild(instituto);
|
|
|
|
Element nombre = doc.createElement("nombre");
|
|
nombre.setTextContent(texto(raizOrigen, "nombre"));
|
|
instituto.appendChild(nombre);
|
|
|
|
guardarXml(doc, "info_IES.xml");
|
|
System.out.println("Fichero info_IES.xml creado.");
|
|
}
|
|
|
|
static void crearResumenCiclos() throws Exception {
|
|
Element raizOrigen = leerXml();
|
|
|
|
Document doc = nuevoDocumento();
|
|
Element resumen = doc.createElement("resumen");
|
|
doc.appendChild(resumen);
|
|
|
|
Element instituto = doc.createElement("instituto");
|
|
instituto.setTextContent(texto(raizOrigen, "nombre"));
|
|
instituto.setAttribute("tipo", raizOrigen.getAttribute("tipo"));
|
|
resumen.appendChild(instituto);
|
|
|
|
Element ciclos = doc.createElement("ciclos");
|
|
resumen.appendChild(ciclos);
|
|
|
|
NodeList listaCiclos = raizOrigen.getElementsByTagName("ciclo");
|
|
for (int i = 0; i < listaCiclos.getLength(); i++) {
|
|
Element cicloOrigen = (Element) listaCiclos.item(i);
|
|
|
|
Element ciclo = doc.createElement("ciclo");
|
|
ciclo.setTextContent(texto(cicloOrigen, "nombre_corto"));
|
|
ciclo.setAttribute("codigo", cicloOrigen.getAttribute("codigo"));
|
|
ciclos.appendChild(ciclo);
|
|
}
|
|
|
|
guardarXml(doc, "resumen_ciclos_IES.xml");
|
|
System.out.println("Fichero resumen_ciclos_IES.xml creado.");
|
|
}
|
|
|
|
static void crearModulosDam() throws Exception {
|
|
Element raizOrigen = leerXml();
|
|
|
|
Document doc = nuevoDocumento();
|
|
Element ies = doc.createElement("ies");
|
|
ies.setAttribute("nombre", texto(raizOrigen, "nombre"));
|
|
ies.setAttribute("tipo", raizOrigen.getAttribute("tipo"));
|
|
doc.appendChild(ies);
|
|
|
|
boolean encontrado = false;
|
|
NodeList listaCiclos = raizOrigen.getElementsByTagName("ciclo");
|
|
|
|
for (int i = 0; i < listaCiclos.getLength(); i++) {
|
|
Element cicloOrigen = (Element) listaCiclos.item(i);
|
|
|
|
if (!texto(cicloOrigen, "nombre_corto").equals("DAM")) {
|
|
continue;
|
|
}
|
|
encontrado = true;
|
|
|
|
Element ciclo = doc.createElement("ciclo");
|
|
ciclo.setAttribute("codigo", cicloOrigen.getAttribute("codigo"));
|
|
ciclo.setAttribute("nivel", cicloOrigen.getAttribute("nivel"));
|
|
ies.appendChild(ciclo);
|
|
|
|
Element nombreCorto = doc.createElement("nombre_corto");
|
|
nombreCorto.setTextContent(texto(cicloOrigen, "nombre_corto"));
|
|
ciclo.appendChild(nombreCorto);
|
|
|
|
Element nombreLargo = doc.createElement("nombre_largo");
|
|
nombreLargo.setTextContent(texto(cicloOrigen, "nombre_largo"));
|
|
ciclo.appendChild(nombreLargo);
|
|
|
|
NodeList cursos = cicloOrigen.getElementsByTagName("curso");
|
|
for (int j = 0; j < cursos.getLength(); j++) {
|
|
Element cursoOrigen = (Element) cursos.item(j);
|
|
|
|
if (!cursoOrigen.getAttribute("numero").equals("1")) {
|
|
continue;
|
|
}
|
|
|
|
Element curso = doc.createElement("curso");
|
|
curso.setAttribute("num", "1");
|
|
ciclo.appendChild(curso);
|
|
|
|
NodeList modulos = cursoOrigen.getElementsByTagName("modulo");
|
|
for (int k = 0; k < modulos.getLength(); k++) {
|
|
Element moduloOrigen = (Element) modulos.item(k);
|
|
|
|
Element modulo = doc.createElement("modulo");
|
|
modulo.setAttribute("id", moduloOrigen.getAttribute("id"));
|
|
modulo.setAttribute("ref", moduloOrigen.getAttribute("ref"));
|
|
modulo.setAttribute("numero_horas", texto(moduloOrigen, "numerohoras"));
|
|
curso.appendChild(modulo);
|
|
|
|
Element titulo = doc.createElement("titulo");
|
|
titulo.setTextContent(texto(moduloOrigen, "titulo"));
|
|
modulo.appendChild(titulo);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!encontrado) {
|
|
System.out.println("No se ha encontrado el ciclo DAM.");
|
|
return;
|
|
}
|
|
|
|
guardarXml(doc, "modulos1_ciclo_DAM.xml");
|
|
System.out.println("Fichero modulos1_ciclo_DAM.xml creado.");
|
|
}
|
|
|
|
static void crearInformeTxt() throws Exception {
|
|
Element raizOrigen = leerXml();
|
|
|
|
PrintWriter pw = new PrintWriter("informe_ciclos_IES.txt", "UTF-8");
|
|
|
|
pw.println("INFORME DE CICLOS FORMATIVOS");
|
|
pw.println(texto(raizOrigen, "nombre"));
|
|
pw.println("========================================");
|
|
|
|
NodeList listaCiclos = raizOrigen.getElementsByTagName("ciclo");
|
|
for (int i = 0; i < listaCiclos.getLength(); i++) {
|
|
Element ciclo = (Element) listaCiclos.item(i);
|
|
|
|
pw.println();
|
|
pw.println("Código: " + ciclo.getAttribute("codigo"));
|
|
pw.println("Nombre corto: " + texto(ciclo, "nombre_corto"));
|
|
pw.println("Observaciones:");
|
|
|
|
NodeList hijos = ciclo.getChildNodes();
|
|
for (int j = 0; j < hijos.getLength(); j++) {
|
|
Node hijo = hijos.item(j);
|
|
if (hijo.getNodeType() == Node.COMMENT_NODE) {
|
|
pw.println("- " + hijo.getNodeValue().trim());
|
|
}
|
|
}
|
|
|
|
pw.println();
|
|
if (i < listaCiclos.getLength() - 1) {
|
|
pw.println("----------------------------------------");
|
|
} else {
|
|
pw.println("========================================");
|
|
}
|
|
}
|
|
|
|
pw.println("Fin del informe");
|
|
pw.close();
|
|
|
|
System.out.println("Fichero informe_ciclos_IES.txt creado.");
|
|
}
|
|
|
|
static void cargarConfiguracion() {
|
|
Properties p = new Properties();
|
|
try {
|
|
FileInputStream in = new FileInputStream("config/application.properties");
|
|
p.load(in);
|
|
in.close();
|
|
rutaXml = p.getProperty("xml.ruta", rutaXml);
|
|
} catch (IOException e) {
|
|
System.out.println("Sin application.properties, se usa " + rutaXml);
|
|
}
|
|
}
|
|
|
|
static Element leerXml() throws Exception {
|
|
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(rutaXml));
|
|
return doc.getDocumentElement();
|
|
}
|
|
|
|
static Document nuevoDocumento() throws Exception {
|
|
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
|
doc.setXmlStandalone(true);
|
|
return doc;
|
|
}
|
|
|
|
static void guardarXml(Document doc, String ruta) throws Exception {
|
|
Transformer t = TransformerFactory.newInstance().newTransformer();
|
|
t.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
|
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
|
|
t.transform(new DOMSource(doc), new StreamResult(new File(ruta)));
|
|
}
|
|
|
|
static String texto(Element padre, String nombre) {
|
|
NodeList lista = padre.getElementsByTagName(nombre);
|
|
if (lista.getLength() == 0) {
|
|
return "";
|
|
}
|
|
return lista.item(0).getTextContent().trim();
|
|
}
|
|
} |