/** * Create a NodeModel from a SAX input source. Adjacent text nodes will be merged (and CDATA * sections are considered as text nodes). * * @param removeComments whether to remove all comment nodes (recursively) from the tree before * processing * @param removePIs whether to remove all processing instruction nodes (recursively from the tree * before processing */ public static NodeModel parse(InputSource is, boolean removeComments, boolean removePIs) throws SAXException, IOException, ParserConfigurationException { DocumentBuilder builder = getDocumentBuilderFactory().newDocumentBuilder(); if (errorHandler != null) builder.setErrorHandler(errorHandler); Document doc = builder.parse(is); if (removeComments && removePIs) { simplify(doc); } else { if (removeComments) { removeComments(doc); } if (removePIs) { removePIs(doc); } mergeAdjacentText(doc); } return wrap(doc); }
/** * método que permite verificar si el ODE tiene metadatos.. ya sea integrados en el manifest o * externos al mismo. * * @return String con tres posibles valores * <ul> * <li>null . - si el ODE no tiene metadatos * <li>vacio . - si el ODE tiene metadatos integrados en el manifest * <li>path al metadato. - si el ODE tiene metadatos externos que sean lomes validos </ul */ private String chequearLomes() throws Exception { String resultado = null; List listaLomes = new ArrayList(); Node nodo = modelo.getNode().getChildNodes().item(0); NodeModel.simplify(nodo); boolean terminado = false; NodeList hijos = nodo.getChildNodes(); for (int i = 0; i < hijos.getLength() && !terminado; i++) { Node child = hijos.item(i); if (child.getLocalName() != null && child.getLocalName().equals("metadata")) { NodeList hijos2 = child.getChildNodes(); for (int j = 0; j < hijos2.getLength() && !terminado; j++) { Node child2 = hijos2.item(j); if (child2.getLocalName() != null && child2.getLocalName().equals("lom")) { resultado = ""; terminado = true; } if (child2.getLocalName() != null && child2.getLocalName().equals("location")) { listaLomes.add(child2.getFirstChild().getNodeValue()); } } } // metadata } if (resultado == null && listaLomes.size() > 0) { // busco en la lista de nodos externos al primero que sea lomes boolean encontrado = false; for (Iterator iter = listaLomes.iterator(); iter.hasNext() && !encontrado; ) { String path = (String) iter.next(); if (chequearTipoLomesExterno(path)) { resultado = path; encontrado = true; } } } return resultado; }
/** * Removes comments and processing instruction, and then unites adjacent text nodes. Note that * CDATA sections count as text nodes. */ public static void simplify(Node node) { NodeList children = node.getChildNodes(); int i = 0; int len = children.getLength(); Node prevTextChild = null; while (i < len) { Node child = children.item(i); if (child.hasChildNodes()) { simplify(child); prevTextChild = null; i++; } else { int type = child.getNodeType(); if (type == Node.PROCESSING_INSTRUCTION_NODE) { node.removeChild(child); len--; } else if (type == Node.COMMENT_NODE) { node.removeChild(child); len--; } else if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) { if (prevTextChild != null) { CharacterData ptc = (CharacterData) prevTextChild; ptc.setData(ptc.getNodeValue() + child.getNodeValue()); node.removeChild(child); len--; } else { prevTextChild = child; i++; } } else { prevTextChild = null; i++; } } } }
/** * Método que devuelve el título de ode en caso que existiera.. busca el título en el lomes * integrado en el manifest o en algun lomes externo si lo tuviera. * * @return String con el título del ode */ private String obtenerTitulo() throws Exception { String resultado = ""; if (pathLomesExterno == null) return resultado; boolean terminado = false; if (pathLomesExterno.equals("")) { Node nodo = modelo.getNode().getChildNodes().item(0); NodeModel.simplify(nodo); NodeList hijos = nodo.getChildNodes(); for (int i = 0; i < hijos.getLength() && !terminado; i++) { Node child = hijos.item(i); if (child.getLocalName() != null && child.getLocalName().equals("metadata")) { NodeList hijos2 = child.getChildNodes(); for (int j = 0; j < hijos2.getLength() && !terminado; j++) { Node child2 = hijos2.item(j); if (child2.getLocalName() != null && child2.getLocalName().equals("lom")) { NodeList hijos3 = child2.getChildNodes(); for (int k = 0; k < hijos3.getLength() && !terminado; k++) { Node child3 = hijos3.item(k); if (child3.getLocalName() != null && child3.getLocalName().equals("general")) { NodeList hijos4 = child3.getChildNodes(); for (int l = 0; l < hijos4.getLength() && !terminado; l++) { Node child4 = hijos4.item(l); if (child4.getLocalName() != null && child4.getLocalName().equals("title")) { NodeList hijos5 = child4.getChildNodes(); for (int ll = 0; ll < hijos5.getLength() && !terminado; ll++) { Node child5 = hijos5.item(ll); if (child5.getLocalName() != null && child5.getLocalName().equals("string")) { resultado = child5.getFirstChild().getNodeValue(); terminado = true; } } } } } } } } } // metadata } } else { NodeModel modeloExterno = null; try { modeloExterno = NodeModel.parse(new File(pathOde + "/" + pathLomesExterno)); Node nodo = modeloExterno.getNode().getChildNodes().item(0); NodeModel.simplify(nodo); NodeList hijos = nodo.getChildNodes(); for (int i = 0; i < hijos.getLength() && !terminado; i++) { Node child = hijos.item(i); if (child.getLocalName() != null && child.getLocalName().equals("general")) { NodeList hijos2 = child.getChildNodes(); for (int j = 0; j < hijos2.getLength() && !terminado; j++) { Node child2 = hijos2.item(j); if (child2.getLocalName() != null && child2.getLocalName().equals("title")) { NodeList hijos3 = child2.getChildNodes(); for (int k = 0; k < hijos3.getLength() && !terminado; k++) { Node child3 = hijos3.item(k); if (child3.getLocalName() != null && child3.getLocalName().equals("string")) { resultado = child3.getFirstChild().getNodeValue(); terminado = true; } } } } } // metadata } } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("Error en GeneradorHTML:obtenerTitulo .. " + e.getMessage()); } throw e; } } return resultado; }