first commit
This commit is contained in:
@@ -0,0 +1,302 @@
|
||||
package org.palomafp.aadd.dta;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Scanner;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
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.palomafp.aadd.dta.gestores.GestorConfiguracion;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
public class AppPrincipal {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try (Scanner sc = new Scanner(System.in)) {
|
||||
|
||||
// Obtener la ruta del fichero desde application.properties a un objeto de la interfaz Path
|
||||
Path ruta = GestorConfiguracion.getRutaFicheroIes();
|
||||
|
||||
// Cargar el fichero XML con DOM y recogerlo en un Document
|
||||
Document doc = cargarXml(ruta);
|
||||
|
||||
int opcion;
|
||||
|
||||
do {
|
||||
|
||||
mostrarMenu();
|
||||
|
||||
try {
|
||||
opcion = Integer.parseInt(sc.nextLine().trim());
|
||||
} catch (NumberFormatException e) {
|
||||
opcion = -1;
|
||||
}
|
||||
|
||||
try {
|
||||
switch (opcion) {
|
||||
case 1 -> crearFicheroInformacionInstituto(doc);
|
||||
case 2 -> crearFicheroResumenCiclos(doc);
|
||||
case 3 -> crearFicheroModulosDam(doc);
|
||||
case 4 -> crearFicheroInformeCiclos(doc);
|
||||
case 0 -> System.out.println("Saliendo del programa...");
|
||||
default -> System.out.println("Opción inválida: (0-4)");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error en la opción " + opcion + ": " + e);
|
||||
}
|
||||
|
||||
} while (opcion != 0);
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error al iniciar el programa: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void crearFicheroInformacionInstituto(Document doc) throws Exception {
|
||||
|
||||
// 1. Buscar el <nombre> que es hijo directo de <instituto>
|
||||
String nombreIes = obtenerNombreIes(doc);
|
||||
|
||||
// 2. Crear un documento nuevo
|
||||
Document nuevo = DocumentBuilderFactory.newInstance()
|
||||
.newDocumentBuilder().newDocument();
|
||||
|
||||
// 3. Construir el árbol
|
||||
Element instituto = nuevo.createElement("instituto");
|
||||
nuevo.appendChild(instituto);
|
||||
|
||||
Element nombre = nuevo.createElement("nombre");
|
||||
nombre.setTextContent(nombreIes);
|
||||
instituto.appendChild(nombre);
|
||||
|
||||
// 4. Guardar
|
||||
guardarXml(nuevo, Path.of("info_IES.xml"));
|
||||
System.out.println("Fichero info_IES.xml creado correctamente.");
|
||||
}
|
||||
|
||||
private static void crearFicheroResumenCiclos(Document doc) throws Exception {
|
||||
|
||||
// 1. Datos del instituto: atributo tipo y nombre (hijo directo de la raíz)
|
||||
String nombreIes = obtenerNombreIes(doc);
|
||||
String tipo = doc.getDocumentElement().getAttribute("tipo");
|
||||
|
||||
// 2. Crear documento nuevo
|
||||
Document nuevo = DocumentBuilderFactory.newInstance()
|
||||
.newDocumentBuilder().newDocument();
|
||||
|
||||
Element resumen = nuevo.createElement("resumen");
|
||||
nuevo.appendChild(resumen);
|
||||
|
||||
Element instituto = nuevo.createElement("instituto");
|
||||
instituto.setAttribute("tipo", tipo);
|
||||
instituto.setTextContent(nombreIes);
|
||||
resumen.appendChild(instituto);
|
||||
|
||||
Element ciclos = nuevo.createElement("ciclos");
|
||||
resumen.appendChild(ciclos);
|
||||
|
||||
// 3. Recorrer los ciclos del original
|
||||
NodeList ciclosOrigen = doc.getElementsByTagName("ciclo");
|
||||
for (int i = 0; i < ciclosOrigen.getLength(); i++) {
|
||||
Element cicloOrigen = (Element) ciclosOrigen.item(i);
|
||||
|
||||
String codigo = cicloOrigen.getAttribute("codigo");
|
||||
String nombreCorto = cicloOrigen.getElementsByTagName("nombre_corto")
|
||||
.item(0).getTextContent().trim();
|
||||
|
||||
Element ciclo = nuevo.createElement("ciclo");
|
||||
ciclo.setAttribute("codigo", codigo);
|
||||
ciclo.setTextContent(nombreCorto);
|
||||
ciclos.appendChild(ciclo);
|
||||
}
|
||||
|
||||
// 4. Guardar
|
||||
guardarXml(nuevo, Path.of("resumen_ciclos_IES.xml"));
|
||||
System.out.println("Fichero resumen_ciclos_IES.xml creado correctamente.");
|
||||
}
|
||||
|
||||
private static void crearFicheroModulosDam(Document doc) throws Exception {
|
||||
|
||||
// 1. Nombre del IES (hijo directo de la raíz)
|
||||
String nombreIes = obtenerNombreIes(doc);
|
||||
|
||||
// 2. Localizar el ciclo DAM
|
||||
Element cicloDam = null;
|
||||
NodeList ciclosOrigen = doc.getElementsByTagName("ciclo");
|
||||
for (int i = 0; i < ciclosOrigen.getLength(); i++) {
|
||||
Element c = (Element) ciclosOrigen.item(i);
|
||||
String corto = c.getElementsByTagName("nombre_corto").item(0).getTextContent().trim();
|
||||
if (corto.equals("DAM")) {
|
||||
cicloDam = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (cicloDam == null) {
|
||||
System.out.println("No se ha encontrado el ciclo DAM.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Documento nuevo
|
||||
Document nuevo = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
|
||||
Element ies = nuevo.createElement("ies");
|
||||
ies.setAttribute("nombre", nombreIes);
|
||||
ies.setAttribute("tipo", doc.getDocumentElement().getAttribute("tipo"));
|
||||
nuevo.appendChild(ies);
|
||||
|
||||
Element ciclo = nuevo.createElement("ciclo");
|
||||
ciclo.setAttribute("codigo", cicloDam.getAttribute("codigo"));
|
||||
ciclo.setAttribute("nivel", cicloDam.getAttribute("nivel"));
|
||||
ies.appendChild(ciclo);
|
||||
|
||||
Element nombreCorto = nuevo.createElement("nombre_corto");
|
||||
nombreCorto.setTextContent(cicloDam.getElementsByTagName("nombre_corto").item(0).getTextContent().trim());
|
||||
ciclo.appendChild(nombreCorto);
|
||||
|
||||
Element nombreLargo = nuevo.createElement("nombre_largo");
|
||||
nombreLargo.setTextContent(cicloDam.getElementsByTagName("nombre_largo").item(0).getTextContent().trim());
|
||||
ciclo.appendChild(nombreLargo);
|
||||
|
||||
// 4. Curso 1 y sus módulos
|
||||
NodeList cursosOrigen = cicloDam.getElementsByTagName("curso");
|
||||
for (int i = 0; i < cursosOrigen.getLength(); i++) {
|
||||
Element cursoOrigen = (Element) cursosOrigen.item(i);
|
||||
if (!cursoOrigen.getAttribute("numero").equals("1")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Element curso = nuevo.createElement("curso");
|
||||
curso.setAttribute("num", "1");
|
||||
ciclo.appendChild(curso);
|
||||
|
||||
NodeList modulosOrigen = cursoOrigen.getElementsByTagName("modulo");
|
||||
for (int j = 0; j < modulosOrigen.getLength(); j++) {
|
||||
Element moduloOrigen = (Element) modulosOrigen.item(j);
|
||||
|
||||
Element modulo = nuevo.createElement("modulo");
|
||||
modulo.setAttribute("id", moduloOrigen.getAttribute("id"));
|
||||
modulo.setAttribute("ref", moduloOrigen.getAttribute("ref"));
|
||||
modulo.setAttribute("numero_horas",
|
||||
moduloOrigen.getElementsByTagName("numerohoras").item(0).getTextContent().trim());
|
||||
|
||||
Element titulo = nuevo.createElement("titulo");
|
||||
titulo.setTextContent(
|
||||
moduloOrigen.getElementsByTagName("titulo").item(0).getTextContent().trim());
|
||||
modulo.appendChild(titulo);
|
||||
|
||||
curso.appendChild(modulo);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// 5. Guardar
|
||||
guardarXml(nuevo, Path.of("modulos1_ciclo_DAM.xml"));
|
||||
System.out.println("Fichero modulos1_ciclo_DAM.xml creado correctamente.");
|
||||
}
|
||||
|
||||
private static void crearFicheroInformeCiclos(Document doc) throws Exception {
|
||||
|
||||
// 1. Nombre del IES (hijo directo de la raíz)
|
||||
String nombreIes = obtenerNombreIes(doc);
|
||||
|
||||
String igual = "=".repeat(40);
|
||||
String guiones = "-".repeat(40);
|
||||
String salto = System.lineSeparator();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// 2. Cabecera
|
||||
sb.append("INFORME DE CICLOS FORMATIVOS").append(salto);
|
||||
sb.append(nombreIes).append(salto);
|
||||
sb.append(igual).append(salto);
|
||||
|
||||
// 3. Ciclos
|
||||
NodeList ciclos = doc.getElementsByTagName("ciclo");
|
||||
for (int i = 0; i < ciclos.getLength(); i++) {
|
||||
Element ciclo = (Element) ciclos.item(i);
|
||||
|
||||
if (i > 0) {
|
||||
sb.append(guiones).append(salto);
|
||||
} else {
|
||||
sb.append(salto);
|
||||
}
|
||||
|
||||
sb.append("Código: ").append(ciclo.getAttribute("codigo")).append(salto);
|
||||
sb.append("Nombre corto: ")
|
||||
.append(ciclo.getElementsByTagName("nombre_corto").item(0).getTextContent().trim())
|
||||
.append(salto);
|
||||
sb.append("Observaciones:").append(salto);
|
||||
|
||||
// Solo comentarios hijos directos del ciclo
|
||||
NodeList hijos = ciclo.getChildNodes();
|
||||
for (int j = 0; j < hijos.getLength(); j++) {
|
||||
Node h = hijos.item(j);
|
||||
if (h.getNodeType() == Node.COMMENT_NODE) {
|
||||
sb.append("- ").append(h.getNodeValue().trim()).append(salto);
|
||||
}
|
||||
}
|
||||
sb.append(salto);
|
||||
}
|
||||
|
||||
// 4. Pie
|
||||
sb.append(igual).append(salto);
|
||||
sb.append("Fin del informe").append(salto);
|
||||
|
||||
// 5. Guardar (UTF-8 por defecto en writeString)
|
||||
Files.writeString(Path.of("informe_ciclos_IES.txt"), sb.toString());
|
||||
|
||||
System.out.println("Fichero informe_ciclos_IES.txt creado correctamente.");
|
||||
}
|
||||
|
||||
private static void mostrarMenu() {
|
||||
System.out.println("---- MENÚ ----");
|
||||
System.out.println("1. Crear fichero 'info_IES.xml'");
|
||||
System.out.println("2. Crear fichero 'resumen_ciclos_IES.xml'");
|
||||
System.out.println("3. Crear fichero 'modulos1_ciclo_DAM.xml'");
|
||||
System.out.println("4. Crear fichero 'informe_ciclos_IES.txt'");
|
||||
System.out.println("0. Salir");
|
||||
System.out.print("Opción: ");
|
||||
}
|
||||
|
||||
private static Document cargarXml(Path ruta) throws Exception {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document doc = builder.parse(ruta.toFile());
|
||||
doc.getDocumentElement().normalize();
|
||||
return doc;
|
||||
}
|
||||
|
||||
private static void guardarXml(Document doc, Path destino) throws Exception {
|
||||
doc.setXmlStandalone(true); // quita standalone="no"
|
||||
|
||||
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.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
|
||||
t.transform(new DOMSource(doc), new StreamResult(destino.toFile()));
|
||||
}
|
||||
|
||||
private static String obtenerNombreIes(Document doc) {
|
||||
NodeList hijosRaiz = doc.getDocumentElement().getChildNodes();
|
||||
for (int i = 0; i < hijosRaiz.getLength(); i++) {
|
||||
Node n = hijosRaiz.item(i);
|
||||
if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals("nombre")) {
|
||||
return n.getTextContent().trim();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.palomafp.aadd.dta.gestores;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Properties;
|
||||
|
||||
public class GestorConfiguracion {
|
||||
|
||||
private static final String RUTA_FICHERO_CONFIGURACION = "application.properties";
|
||||
private static final Properties PROPIEDADES_CONFIGURACION = new Properties();
|
||||
|
||||
private GestorConfiguracion() {}
|
||||
|
||||
static {
|
||||
try (var in = GestorConfiguracion.class.getClassLoader()
|
||||
.getResourceAsStream(RUTA_FICHERO_CONFIGURACION)) {
|
||||
|
||||
if (in == null) {
|
||||
throw new IOException("No se ha encontrado el fichero " + RUTA_FICHERO_CONFIGURACION);
|
||||
}
|
||||
|
||||
PROPIEDADES_CONFIGURACION.load(in);
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("No se ha podido cargar el fichero de configuración", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Path getRutaFicheroIes() {
|
||||
|
||||
String rutaStr = PROPIEDADES_CONFIGURACION.getProperty("ies.ruta.fichero");
|
||||
|
||||
if (rutaStr == null) {
|
||||
throw new IllegalStateException("Falta la propiedad 'ies.ruta.fichero' en " + RUTA_FICHERO_CONFIGURACION);
|
||||
}
|
||||
|
||||
return Path.of(rutaStr);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user