protected void transformXML(File[] files) { int number = 0; try { SAXBuilder builder = new SAXBuilder(); for (int f = 0; f < files.length; f++) { int fn = f + 1; // split by treatment Document doc = builder.build(files[f]); Element root = doc.getRootElement(); List<Element> treatments = XPath.selectNodes(root, "/tax:taxonx/tax:taxonxBody/tax:treatment"); // detach all but one treatments from doc ArrayList<Element> saved = new ArrayList<Element>(); for (int t = 1; t < treatments.size(); t++) { Element e = treatments.get(t); doc.removeContent(e); e.detach(); saved.add(e); } // now doc is a template to create other treatment files // root.detach(); formatDescription( (Element) XPath.selectSingleNode(root, "/tax:taxonx/tax:taxonxBody/tax:treatment"), ".//tax:div[@type='description']", ".//tax:p", fn, 0); root.detach(); writeTreatment2Transformed(root, fn, 0); listener.info((number++) + "", fn + "_0.xml"); // list the file on GUI here getDescriptionFrom(root, fn, 0); // replace treatement in doc with a new treatment in saved Iterator<Element> it = saved.iterator(); int count = 1; while (it.hasNext()) { Element e = it.next(); Element body = root.getChild("taxonxBody", root.getNamespace()); Element treatment = (Element) XPath.selectSingleNode(root, "/tax:taxonx/tax:taxonxBody/tax:treatment"); // in treatment/div[@type="description"], replace <tax:p> tag with <description // pid="1.txtp436_1.txt"> int index = body.indexOf(treatment); e = formatDescription(e, ".//tax:div[@type='description']", ".//tax:p", fn, count); body.setContent(index, e); // write each treatment as a file in the target/transfromed folder // write description text in the target/description folder root.detach(); writeTreatment2Transformed(root, fn, count); listener.info((number++) + "", fn + "_" + count + ".xml"); // list the file on GUI here getDescriptionFrom(root, fn, count); count++; } } } catch (Exception e) { e.printStackTrace(); LOGGER.error("Type4Transformer : error.", e); } }
@Override protected void setUp() throws Exception { final Document document = new SAXBuilder().build(new File(getTestDataRoot(), "/RETest.xml")); final List<Element> list = XPath.selectNodes(document.getRootElement(), "//test"); new File(getTestDataPath()).mkdirs(); int i = 0; for (Element element : list) { final String name; final Element parent = (Element) element.getParent(); final String s = parent.getName(); final String t = parent.getAttribute("id") == null ? "" : parent.getAttribute("id").getValue() + "-"; if (!"tests".equals(s)) { name = s + "/test-" + t + ++i + ".regexp"; } else { name = "test-" + t + ++i + ".regexp"; } final Result result = Result.valueOf((String) XPath.selectSingleNode(element, "string(expected)")); final boolean warn = !"false".equals(element.getAttributeValue("warning")); final boolean info = "true".equals(element.getAttributeValue("info")); myMap.put(name, new Test(result, warn, info)); final File file = new File(getTestDataPath(), name); file.getParentFile().mkdirs(); final FileWriter stream = new FileWriter(file); final String pattern = (String) XPath.selectSingleNode(element, "string(pattern)"); if (!"false".equals(element.getAttributeValue("verify"))) try { Pattern.compile(pattern); if (result == Result.ERR) { System.out.println("Incorrect FAIL value for " + pattern); } } catch (PatternSyntaxException e) { if (result == Result.OK) { System.out.println("Incorrect OK value for " + pattern); } } stream.write(pattern); stream.close(); } super.setUp(); myOut = new ByteArrayOutputStream(); System.setErr(new PrintStream(myOut)); }
@SuppressWarnings("unchecked") private List<JarConfig> loadConfig() { List<JarConfig> list_pair = new ArrayList<JarConfig>(); SAXBuilder builder = new SAXBuilder(); try { Document document = builder.build(this.config); Element root = document.getRootElement(); Element ele = (Element) XPath.selectSingleNode(root, path.JAR.toString()); List<Element> list = ele.getChildren(); for (Element element : list) { JarConfig config = new JarConfig( classPath + element.getAttributeValue("jar").trim(), element.getAttributeValue("name").trim(), element.getAttributeValue("class").trim()); list_pair.add(config); } } catch (JDOMException e) { WriteToLog.error("XML FORMAT ERROR!" + "\n" + e.getMessage()); } catch (IOException e) { WriteToLog.error("XML FORMAT ERROR!" + "\n" + e.getMessage()); } catch (Exception e) { e.printStackTrace(); WriteToLog.error("XML ERROR!" + "\n" + e.getMessage()); } return list_pair; }
/** * Retrieves a single XML element given a simple xpath (like "a/b/c"). * * @param xml * @param xpath * @param theNSs * @return * @throws JDOMException */ public static Object selectSingle(Element xml, String xpath, List<Namespace> theNSs) throws JDOMException { XPath xp = prepareXPath(xml, xpath, theNSs); return xp.selectSingleNode(xml); }
/** * Update page information. * * @param node Element for the page. * @param page Page. * @throws JDOMException */ public void updatePageInformation(Element node, Page page) throws JDOMException { // Retrieve basic page information Attribute attrPageId = node.getAttribute("pageid"); if (attrPageId != null) { page.setPageId(attrPageId.getValue()); } Attribute attrTitle = node.getAttribute("title"); if (attrTitle != null) { page.setTitle(attrTitle.getValue()); } page.setStartTimestamp(node.getAttributeValue("starttimestamp")); Attribute attrRedirect = node.getAttribute("redirect"); if (attrRedirect != null) { page.isRedirect(true); } Attribute attrMissing = node.getAttribute("missing"); if (attrMissing != null) { page.setExisting(Boolean.FALSE); } // Retrieve protection information XPath xpaProtection = XPath.newInstance("protection/pr[@type=\"edit\"]"); Element protectionNode = (Element) xpaProtection.selectSingleNode(node); if (protectionNode != null) { XPath xpaLevel = XPath.newInstance("./@level"); page.setEditProtectionLevel(xpaLevel.valueOf(protectionNode)); } }
@Override public String parse(Element t) throws ParseException { try { XPath p = constructPath(this.getPath(), t); Element elem = (Element) p.selectSingleNode(t); return elem.getAttribute(attribute).getValue(); } catch (JDOMException e) { throw new ParseException(e.getMessage(), 0); } }
/** * Revisa cualquier nodo pasandole como parametros: atributo, XPath y el nodo a evaluar * * @param atributo Es el nombre del atributo que se desea obtener para sacar su valor * @param rutaXpath Es la ruta del nodo al que se desea accesar * @param nodoEvaluar Es el nodo donde se entcuentra el atributo pasado anteriormente para poder * saber su ubicacion * @return El valor del atributo deseado */ public String revisaNodos( String atributo, String rutaXpath, String nodoEvaluar) { // File ArchivoXML, String valorAtributo = ""; try { try { Element nodo = (Element) XPath.selectSingleNode(document, rutaXpath); List listTfd = nodo.getChildren(); for (int i = 0; i < listTfd.size(); i++) { Element tfd = (Element) listTfd.get(i); if (tfd.getName().equals(nodoEvaluar)) { valorAtributo = tfd.getAttributeValue(atributo); logger.debug("El atributo " + atributo + " contiene: " + valorAtributo); } } } catch (NullPointerException e) { logger.error("El atributo " + atributo + " no se encuentra ERROR: " + e.getMessage()); } } catch (JDOMException ex) { logger.error("Error en JDOM: " + ex.getMessage()); // valorAtributo="Error en JDOM:"+ex.getMessage(); } return valorAtributo; }
public void testXPath() { String variableString = "<variable name=\"inputVariable\" messageType=\"client:RentalProcessRequestMessage\">" + "<message name=\"RentalProcessRequestMessage\">" + "<part name=\"payload\" element=\"{http:////xmlns.oracle.com//RentalProcess}RentalProcessProcessRequest\">" + "<element xmlns=\"http:////www.w3.org//2001//XMLSchema\" name=\"RentalProcessProcessRequest\" >" + "<complexType name=\"rentalType\">" + "<sequence>" + "<element xmlns=\"http:////www.hotzenplotz.org//2001//isht\"" + " xmlns:sawsdl=\"http:////www.w3.org//ns//sawsdl\" " + " xmlns:bia=\"http:////www.ipvs.uni-stuttgart.de\" " + " name=\"rentalID\" " + " type=\"string\" " + " sawsdl:modelReference=\"http:////localhost:8080//OWLBuergerInformation.owl#Gender\" " + " bia:modelType=\"OWL\" /> " + " <element xmlns=\"http:////www.hotzenplotz.org//2001//isht\" " + " name=\"customer\">" + " <complextype name=\"customerType\">" + " <sequence>" + " <element name=\"CID\" " + " type=\"string\" />" + " </sequence>" + " </complextype>" + " </element>" + " <element " + " xmlns=\"http:////www.w3.org//2001//XMLSchema\" " + " xmlns:sawsdl=\"http://www.w3.org/ns/sawsdl\" " + " xmlns:bia=\"http://www.ipvs.uni-stuttgart.de\" " + " name=\"bybie\" " + " type=\"string\" " + " sawsdl:modelReference=\"http://BIA-Project.org/2007/Order#buyer\"" + " bia:modelType=\"WSMO\" />" + " </sequence>" + " </complexType>" + " </element>" + " </part>" + " </message>" + "</variable>"; SAXBuilder sb = new SAXBuilder(); Document doc = null; try { doc = sb.build(new StringReader(variableString)); } catch (JDOMException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } XPath path; XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); try { // path = // XPath.newInstance("/descendant::variable/descendant::message"); // path = // XPath.newInstance("/descendant::variable/descendant::message/descendant::part"); // path = // XPath.newInstance("/descendant::variable/descendant::message/descendant::part/descendant::default:element"); // path = XPath.newInstance("/descendant::variable" + // "/descendant::message" + // "/descendant::part" + // "/descendant::default:element" + // "/descendant::default:complexType" + // "/descendant::default:sequence"); // path = XPath.newInstance( // "/descendant::variable[1]" + // "/descendant::message[1]" + // "/descendant::part[1]" + // "/descendant::default:element[1]" + // "/descendant::default:complexType[1]" + // "/descendant::default:sequence[1]" + // "/descendant::hotze:element[2]"); String pathString = "/descendant::variable[1]/descendant::message[1]/descendant::part[1]/descendant::ns0:element[1]/descendant::ns0:complexType[1]/descendant::ns0:sequence[1]/descendant::ns1:element[1]"; path = XPath.newInstance(pathString); Namespace ns = Namespace.getNamespace("ns1", "http:////www.hotzenplotz.org//2001//shit"); path.addNamespace(ns); ns = Namespace.getNamespace("ns0", "http:////www.w3.org//2001//XMLSchema"); path.addNamespace(ns); // Namespace ns = Namespace.getNamespace("default", // "http:////www.w3.org//2001//XMLSchema"); // path.addNamespace(ns); // // ns = Namespace.getNamespace("hotze", // "http:////www.hotzenplotz.org//2001//shit"); // path.addNamespace(ns); Element element = (Element) path.selectSingleNode(doc); assertNotNull(element); // boolean selection = false; // Object o = path.selectSingleNode(doc); // List list; // if (null != o) { // selection = true; // // Element e = (Element) o; // outputter.output(e, System.out); // System.out.println("\n"); // // Iterator iter = e.getDescendants(new ElementFilter()); // while (iter.hasNext()) { // System.out.println(iter.next()); // } // } else { // System.out.println("No Element found"); // } // // if (!selection) { // list = path.selectNodes(doc); // if (null != list) { // selection = true; // System.out.println(list.size()); // // for (Object oo : list) { // Element e = (Element) oo; // outputter.output(e, System.out); // } // } // } // if (null == element) { // System.out.println("Element is null"); // return; // } // assertTrue(selection); // XMLOutputter outputter = new // XMLOutputter(Format.getPrettyFormat()); outputter.output(element, System.out); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } }
@Override public Record addGlobalLOIdentifier(Record record, String reposIdentifier, String ctlg) throws IllegalStateException, JDOMException { // TODO Auto-generated method stub String ident = ctlg + "_" + reposIdentifier + "_"; String loIdent = ""; // try { Element general = JDomUtils.getXpathNode( "//lom:lom/lom:general", Namespace.NO_NAMESPACE, record.getMetadata()); // /lom:lom/lom:general if (general != null) { Element generalIdentifier = general.getChild("identifier", Namespace.NO_NAMESPACE); if (generalIdentifier != null) { if (!(Boolean) gIdOaiCatalog.selectSingleNode(record.getMetadata())) { loIdent = generalIdentifier.getChildText("entry", generalIdentifier.getNamespace()); if (loIdent != null) { ident = ident.concat(loIdent); Element newIdentifier = new Element("identifier", Namespace.NO_NAMESPACE); general.addContent(0, newIdentifier); Element catalog = new Element("catalog", Namespace.NO_NAMESPACE); catalog.setText(ctlg); newIdentifier.addContent(catalog); Element entry = new Element("entry", Namespace.NO_NAMESPACE); ident = ident.replace("/", "."); ident = ident.replace(":", "."); entry.setText(ident); newIdentifier.addContent(entry); gLOID = ident; } else { System.err.println("Missing LOM Identifier"); } } } else { Element technical = JDomUtils.getXpathNode( "//lom:lom/lom:technical", Namespace.NO_NAMESPACE, record.getMetadata()); if (technical != null) { Element location = technical.getChild("location", Namespace.NO_NAMESPACE); if (location != null) { loIdent = location.getText(); ident = ident.concat(loIdent); Element newIdentifier = new Element("identifier", Namespace.NO_NAMESPACE); general.addContent(0, newIdentifier); Element catalog = new Element("catalog", Namespace.NO_NAMESPACE); catalog.setText(ctlg); newIdentifier.addContent(catalog); Element entry = new Element("entry", Namespace.NO_NAMESPACE); ident = ident.replace("/", "."); ident = ident.replace(":", "."); entry.setText(ident); newIdentifier.addContent(entry); gLOID = ident; } } } } else { } return record; }
@Override public CancelRequestItemResponseData performService( CancelRequestItemInitiationData initData, ServiceContext serviceContext, RemoteServiceManager serviceManager) // throws ServiceException { { log.debug("Starting CancelRequestItem Service."); CancelRequestItemResponseData cancelRequestItemResponseData = new CancelRequestItemResponseData(); voyagerSvcMgr = (VoyagerRemoteServiceManager) serviceManager; String itemId; String patronId; String patronAgencyId; String patronUbId; String host; String pickupCode = null; String itemAgencyId = null; // String requestType = initData.getRequestType().getValue(); // UserId is present if (initData.getUserId() != null) { log.info("User id is " + initData.getUserId().getUserIdentifierValue()); patronId = initData.getUserId().getUserIdentifierValue(); if (initData.getUserId().getAgencyId() != null) { patronAgencyId = initData.getUserId().getAgencyId().getValue(); } else { log.debug("No To Agency ID found in the initiation header"); patronAgencyId = (String) voyagerConfig.getProperty(VoyagerConstants.CONFIG_ILS_DEFAULT_AGENCY); } // Retrieve the patron's database Id patronUbId = (String) voyagerConfig.getProperty(patronAgencyId); boolean consortialUse = Boolean.parseBoolean( (String) voyagerConfig.getProperty(VoyagerConstants.CONFIG_CONSORTIUM)); if (consortialUse) { host = voyagerSvcMgr.getUrlFromAgencyId(patronAgencyId); } else { host = (String) voyagerConfig.getProperty(VoyagerConstants.CONFIG_VOYAGER_WEB_SERVICE_URL); } String url = host + "/vxws/patron/" + patronId + "?patron_homedb=" + patronUbId; Document patronInfoDoc = voyagerSvcMgr.getWebServicesDoc(url); if (!patronInfoDoc.getRootElement().getChildText("reply-text").equalsIgnoreCase("ok")) { problems.addAll( ServiceHelper.generateProblems( Version1LookupUserProcessingError.UNKNOWN_USER, "UserId/UserIdentifierValue", initData.getUserId().getUserIdentifierValue(), "Unknown User")); cancelRequestItemResponseData.setProblems(problems); return cancelRequestItemResponseData; } } else { // User ID not present, assuming Authentication Input if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getFromAgencyId() != null) { patronAgencyId = initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue(); patronUbId = (String) voyagerConfig.getProperty(patronAgencyId); } else { log.error("No From Agency ID found in the initiation header"); patronAgencyId = (String) voyagerConfig.getProperty(VoyagerConstants.CONFIG_ILS_DEFAULT_AGENCY); patronUbId = (String) voyagerConfig.getProperty(patronAgencyId); } boolean consortialUse = Boolean.parseBoolean( (String) voyagerConfig.getProperty(VoyagerConstants.CONFIG_CONSORTIUM)); if (consortialUse) { host = voyagerSvcMgr.getUrlFromAgencyId(patronAgencyId); } else { host = (String) voyagerConfig.getProperty(VoyagerConstants.CONFIG_VOYAGER_WEB_SERVICE_URL); } String authenticatedUserId = voyagerSvcMgr.authenticateUser(initData.getAuthenticationInputs(), host); String username = ""; for (AuthenticationInput a : initData.getAuthenticationInputs()) { if (a.getAuthenticationInputType().getValue().equalsIgnoreCase("Username")) { username = a.getAuthenticationInputData(); } else if (a.getAuthenticationInputType().getValue().equalsIgnoreCase("LDAPUsername")) { username = a.getAuthenticationInputData(); } } patronId = authenticatedUserId; if (patronId == null) { problems.addAll( ServiceHelper.generateProblems( Version1LookupUserProcessingError.UNKNOWN_USER, "UserId/UserIdentifierValue", username, "Unknown User")); cancelRequestItemResponseData.setProblems(problems); return cancelRequestItemResponseData; } } UserId userId = new UserId(); userId.setUserIdentifierValue(patronId); userId.setUserIdentifierType(new UserIdentifierType("Username")); userId.setAgencyId(new AgencyId(patronAgencyId)); String requestId = initData.getRequestId().getRequestIdentifierValue(); String requestType = initData.getRequestType().getValue(); boolean consortialUse = Boolean.parseBoolean( (String) voyagerConfig.getProperty(VoyagerConstants.CONFIG_CONSORTIUM)); if (consortialUse) { host = voyagerSvcMgr.getUrlFromAgencyId(patronAgencyId); } else { host = (String) voyagerConfig.getProperty(VoyagerConstants.CONFIG_VOYAGER_WEB_SERVICE_URL); } String truncatedUbId = patronUbId.substring(2); if (requestType.equalsIgnoreCase("Hold")) { String url = host + "/vxws/patron/" + patronId + "/circulationActions/requests/holds/" + truncatedUbId + "%7C" + requestId + "?patron_homedb=" + patronUbId; Document cancelRequestResponse = voyagerSvcMgr.deleteWebServicesDoc(url); try { XPath xpath = XPath.newInstance("response/reply-text"); Element message = (Element) xpath.selectSingleNode(cancelRequestResponse); log.debug("The cancel hold request response is: " + message.getText()); if (message.getText().equalsIgnoreCase("ok")) { cancelRequestItemResponseData.setUserId(userId); } else { List<Problem> problems = new ArrayList<Problem>(); problems.addAll( ServiceHelper.generateProblems( Version1RequestItemProcessingError.USER_INELIGIBLE_TO_REQUEST_THIS_ITEM, null, null, "Unable to process cancel request")); cancelRequestItemResponseData.setProblems(problems); return cancelRequestItemResponseData; } } catch (JDOMException e) { log.error("XPath processing error in cancel hold request"); } } else if (requestType.equalsIgnoreCase("Stack Retrieval")) { String url = host + "/vxws/patron/" + patronId + "/circulationActions/requests/callslips/" + truncatedUbId + "%7C" + requestId + "?patron_homedb=" + patronUbId; Document cancelRequestResponse = voyagerSvcMgr.deleteWebServicesDoc(url); try { XPath xpath = XPath.newInstance("response/reply-text"); Element message = (Element) xpath.selectSingleNode(cancelRequestResponse); log.debug("The cancel callslip request response is: " + message.getText()); if (message.getText().equalsIgnoreCase("ok")) { cancelRequestItemResponseData.setUserId(userId); } else { List<Problem> problems = new ArrayList<Problem>(); problems.addAll( ServiceHelper.generateProblems( Version1RequestItemProcessingError.USER_INELIGIBLE_TO_REQUEST_THIS_ITEM, null, null, "Unable to process cancel request")); cancelRequestItemResponseData.setProblems(problems); return cancelRequestItemResponseData; } } catch (JDOMException e) { log.error("XPath processing error in cancel callslip request"); } } return cancelRequestItemResponseData; }
/** Register an agent on login */ public void registerAgent(org.opensims.agent.Agent agent) { boolean internalScanAvail = false; try { // avoiding the proverbial chicken-and-the-egg problem, we // obtain some scan parameters from the config file... if (agent.getNetworkNode("internal") != null) { StringBuffer path = new StringBuffer("//"); path.append(org.opensims.xml.Node.NETWORK_NODE); path.append("[@topology = 'internal']/"); path.append(org.opensims.xml.Node.SCAN_NODE); path.append("[@enabled = 'true']"); org.jdom.Element internal_net_node = (org.jdom.Element) agent.getNetworkNode("internal"); org.jdom.Element internal_scan_node = (org.jdom.Element) org.jdom.xpath.XPath.selectSingleNode(internal_net_node, path.toString()); if (internal_scan_node != null) { // create a Scan object, to maintain the state of autodiscovery String class_name = internal_scan_node.getAttributeValue("class"); Class c = Class.forName(class_name); org.opensims.model.Scan scan = (org.opensims.model.Scan) c.newInstance(); scan.init( this, agent, internal_scan_node, agent.getNetworkNode("internal"), agent.getNetworkNumber("internal"), agent.getNetworkDevice("internal")); // Restructure Hash Table to use network_number as a key, as opposed to the agent key // put(scan.getKey(), scan); put(agent.getNetworkNumber("internal"), scan); // Use this scan as the default for all agents that aren't directly assigned one (using // the createThreads() method on the agent object --ME agent.setScan(scan); internalScanAvail = true; } } if (agent.getNetworkNode("dmz") != null) { StringBuffer path = new StringBuffer("//"); path.append(org.opensims.xml.Node.NETWORK_NODE); path.append("[@topology = 'dmz']/"); path.append(org.opensims.xml.Node.SCAN_NODE); path.append("[@enabled = 'true']"); org.jdom.Element external_net_node = (org.jdom.Element) agent.getNetworkNode("dmz"); org.jdom.Element external_scan_node = (org.jdom.Element) org.jdom.xpath.XPath.selectSingleNode(external_net_node, path.toString()); if (external_scan_node != null) { // create a Scan object, to maintain the state of autodiscovery String class_name = external_scan_node.getAttributeValue("class"); Class c = Class.forName(class_name); org.opensims.model.Scan scan = (org.opensims.model.Scan) c.newInstance(); scan.init( this, agent, external_scan_node, agent.getNetworkNode("dmz"), agent.getNetworkNumber("dmz"), agent.getNetworkDevice("dmz")); // Restructure Hash Table to use network_number as a key, as opposed to the agent key // put(scan.getKey(), scan); put(agent.getNetworkNumber("dmz"), scan); // First check if we have an "internal" scan, if not, then set this scan as default if (!internalScanAvail) { agent.setScan(scan); } } } } catch (Exception e) { log_.error("register agent", e); } }
/** * Update redirect and missing information of a list of pages. * * @param root Root element. * @param pages List of pages. * @throws JDOMException */ public void updateRedirect(Element root, Collection<Page> pages) throws JDOMException { // Retrieving redirects XPath xpaRedirects = XPath.newInstance("/api/query/redirects/r"); List listRedirects = xpaRedirects.selectNodes(root); XPath xpaFrom = XPath.newInstance("./@from"); XPath xpaTo = XPath.newInstance("./@to"); // Retrieving pages XPath xpaPages = XPath.newInstance("/api/query/pages"); Element listPages = (Element) xpaPages.selectSingleNode(root); XPath xpaPageId = XPath.newInstance("./@pageid"); XPath xpaNamespace = XPath.newInstance("./@ns"); XPath xpaTitle = XPath.newInstance("./@title"); // Retrieving normalization information Map<String, String> normalization = new HashMap<String, String>(); retrieveNormalization(root, normalization); // Analyzing redirects Iterator itRedirect = listRedirects.iterator(); while (itRedirect.hasNext()) { Element currentRedirect = (Element) itRedirect.next(); String fromPage = xpaFrom.valueOf(currentRedirect); String toPage = xpaTo.valueOf(currentRedirect); for (Page p : pages) { // Find if the redirect is already taken into account boolean exists = false; Iterator<Page> itPage = p.getRedirectIteratorWithPage(); while (itPage.hasNext()) { Page tmp = itPage.next(); String title = getNormalizedTitle(tmp.getTitle(), normalization); if (Page.areSameTitle(title, toPage)) { exists = true; } } // Add the redirect if needed itPage = p.getRedirectIteratorWithPage(); while (itPage.hasNext()) { Page tmp = itPage.next(); String title = getNormalizedTitle(tmp.getTitle(), normalization); if (!exists && Page.areSameTitle(title, fromPage)) { XPath xpaPage = createXPath("page", "title", toPage); List listTo = xpaPage.selectNodes(listPages); if (!listTo.isEmpty()) { Element to = (Element) listTo.get(0); Page pageTo = DataManager.getPage(p.getWikipedia(), xpaTitle.valueOf(to), null, null, null); pageTo.setNamespace(xpaNamespace.valueOf(to)); pageTo.setPageId(xpaPageId.valueOf(to)); p.addRedirect(pageTo); } } } } } // Analyzing missing pages for (Page p : pages) { Iterator<Page> itPage = p.getRedirectIteratorWithPage(); while (itPage.hasNext()) { Page tmp = itPage.next(); String title = getNormalizedTitle(tmp.getTitle(), normalization); XPath xpaPage = createXPath("page", "title", title); Element page = (Element) xpaPage.selectSingleNode(listPages); if (page != null) { List pageId = xpaPageId.selectNodes(page); if ((pageId != null) && (!pageId.isEmpty())) { tmp.setExisting(Boolean.TRUE); } else { Attribute attrMissing = page.getAttribute("missing"); if (attrMissing != null) { tmp.setExisting(Boolean.FALSE); } } } } } }