List of usage examples for org.w3c.dom Node getFirstChild
public Node getFirstChild();
From source file:eu.europa.cedefop.europass.jtool.util.SoftToolUtil.java
/** * This method is used from the previous methods saveFile(InputStream xmlStream), saveFile(File xmlFile) to parsing the XML data and store them to the database. * @param doc the Document to be parse/*from ww w.j a va 2s . c om*/ * @return the logs of the saving */ public static String saveFile(Document doc) { Transaction tx = null; Session hsession = null; String logs = "OK..."; try { doc.getDocumentElement().normalize(); // SAVE THE MAIN TABLE (ecv_XML) String fname = null; String lname = null; String telephone = null; String fax = null; String mobile = null; String email = null; String addressLine = null; String municipality = null; String postalCode = null; String countryCode = null; String country = null; String gender = null; String birthDate = null; String codeAppl = null; String appl = null; String social = null; String organisational = null; String technical = null; String computer = null; String artistic = null; String other = null; String additional = null; String annexes = null; String photo = null; String photoType = null; String langCode = null; String lang = null; byte[] photoArray; Node n = XPathAPI.selectSingleNode(doc, "//identification/firstname"); if (n != null && n.getFirstChild() != null) fname = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/lastname"); if (n != null && n.getFirstChild() != null) lname = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/contactinfo/telephone"); if (n != null && n.getFirstChild() != null) telephone = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/contactinfo/fax"); if (n != null && n.getFirstChild() != null) fax = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/contactinfo/mobile"); if (n != null && n.getFirstChild() != null) mobile = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/contactinfo/email"); if (n != null && n.getFirstChild() != null) email = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/contactinfo/address/addressLine"); if (n != null && n.getFirstChild() != null) addressLine = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/contactinfo/address/municipality"); if (n != null && n.getFirstChild() != null) municipality = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/contactinfo/address/postalCode"); if (n != null && n.getFirstChild() != null) postalCode = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/contactinfo/address/country/code"); if (n != null && n.getFirstChild() != null) countryCode = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/contactinfo/address/country/label"); if (n != null && n.getFirstChild() != null) country = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/demographics/gender"); if (n != null && n.getFirstChild() != null) gender = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//identification/demographics/birthdate"); if (n != null && n.getFirstChild() != null) birthDate = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//application/code"); if (n != null && n.getFirstChild() != null) codeAppl = n.getFirstChild().getNodeValue(); n = XPathAPI.selectSingleNode(doc, "//application/label"); if (n != null && n.getFirstChild() != null) appl = n.getFirstChild().getNodeValue(); NodeList mLangLst = XPathAPI.selectNodeList(doc, "//language[@type='europass:mother']"); if (mLangLst.getLength() > 0) { for (int j = 0; j < mLangLst.getLength(); j++) { Node mLang = mLangLst.item(j); if (mLang.getNodeType() == Node.ELEMENT_NODE) { Node tmp1 = XPathAPI.selectSingleNode(mLang, "code"); if (tmp1 != null && tmp1.getFirstChild() != null) langCode = tmp1.getFirstChild().getNodeValue(); Node tmp2 = XPathAPI.selectSingleNode(mLang, "label"); if (tmp2 != null && tmp2.getFirstChild() != null) lang = tmp2.getFirstChild().getNodeValue(); } } } Node skillS = XPathAPI.selectSingleNode(doc, "//skill[@type='social']"); if (skillS != null && skillS.getFirstChild() != null) social = skillS.getFirstChild().getNodeValue(); Node skillO = XPathAPI.selectSingleNode(doc, "//skill[@type='organisational']"); if (skillO != null && skillO.getFirstChild() != null) organisational = skillO.getFirstChild().getNodeValue(); Node skillT = XPathAPI.selectSingleNode(doc, "//skill[@type='technical']"); if (skillT != null && skillT.getFirstChild() != null) technical = skillT.getFirstChild().getNodeValue(); Node skillC = XPathAPI.selectSingleNode(doc, "//skill[@type='computer']"); if (skillC != null && skillC.getFirstChild() != null) computer = skillC.getFirstChild().getNodeValue(); Node skillA = XPathAPI.selectSingleNode(doc, "//skill[@type='artistic']"); if (skillA != null && skillA.getFirstChild() != null) artistic = skillA.getFirstChild().getNodeValue(); Node skillOt = XPathAPI.selectSingleNode(doc, "//skill[@type='other']"); if (skillOt != null && skillOt.getFirstChild() != null) other = skillOt.getFirstChild().getNodeValue(); Node misc = XPathAPI.selectSingleNode(doc, "//misc[@type='additional']"); if (misc != null && misc.getFirstChild() != null) additional = misc.getFirstChild().getNodeValue(); misc = XPathAPI.selectSingleNode(doc, "//misc[@type='annexes']"); if (misc != null && misc.getFirstChild() != null) annexes = misc.getFirstChild().getNodeValue(); Node photoN = XPathAPI.selectSingleNode(doc, "//identification/photo"); if (photoN != null && photoN.getFirstChild() != null) { photo = photoN.getFirstChild().getNodeValue(); photoType = photoN.getAttributes().item(0).getFirstChild().getNodeValue(); if (photo != null) { photoArray = photo.getBytes(); } else { photoArray = null; } } else { photoArray = null; } hsession = HibernateUtil.currentSession(); tx = hsession.beginTransaction(); Xml xml = null; // xml = new Xml(); xml.setAdditional(trunc(additional, 1024)); xml.setFname(trunc(fname, 30)); xml.setLname(trunc(lname, 30)); xml.setPhone(trunc(telephone, 30)); xml.setFax(trunc(fax, 30)); xml.setMobile(trunc(mobile, 30)); xml.setEmail(trunc(email, 50)); xml.setAddress(trunc(addressLine, 50)); xml.setMunic(trunc(municipality, 50)); xml.setPostalCode(trunc(postalCode, 10)); xml.setCountryCode(trunc(countryCode, 5)); xml.setCountry(trunc(country, 30)); xml.setGender(trunc(gender, 2)); xml.setBirthDate(trunc(birthDate, 10)); xml.setApplCode(trunc(codeAppl, 10)); xml.setAppl(trunc(appl, 1024)); xml.setSocial(trunc(social, 1024)); xml.setOrg(trunc(organisational, 1024)); xml.setTech(trunc(technical, 1024)); xml.setComputer(trunc(computer, 1024)); xml.setArtistic(trunc(artistic, 1024)); xml.setOther(trunc(other, 1024)); xml.setAnnexes(trunc(annexes, 1024)); if (photoArray != null) xml.setPhoto(photoArray); else xml.setPhoto(new byte[0]); xml.setMotherLangCode(trunc(langCode, 5)); xml.setMotherLang(trunc(lang, 100)); xml.setPhotoType(photoType); // hsession.save(xml); // // SAVE THE DETAIL TABLE (ecv_driving_licence) NodeList drLst = XPathAPI.selectNodeList(doc, "//drivinglicence"); if (drLst.getLength() > 0) { for (int s = 0; s < drLst.getLength(); s++) { Node dr = drLst.item(s); String fst = (dr != null && dr.getFirstChild() != null) ? dr.getFirstChild().getNodeValue() : null; DrivingLicence dl = new DrivingLicence(); dl.setXml(xml); dl.setDrivingSkill(trunc(fst, 3)); // hsession.save(dl); // } } // SAVE THE DETAIL TABLE (ecv_nationality) NodeList natLst = XPathAPI.selectNodeList(doc, "//identification/demographics/nationality"); if (natLst.getLength() > 0) { for (int s = 0; s < natLst.getLength(); s++) { Node nat = natLst.item(s); if (nat.getNodeType() == Node.ELEMENT_NODE) { Node fstChild = XPathAPI.selectSingleNode(nat, "code"); String fst = (fstChild != null && fstChild.getFirstChild() != null) ? fstChild.getFirstChild().getNodeValue() : null; Node lstChild = XPathAPI.selectSingleNode(nat, "label"); String lst = (lstChild != null && lstChild.getFirstChild() != null) ? lstChild.getFirstChild().getNodeValue() : null; Nationality nt = new Nationality(); nt.setXml(xml); nt.setCode(trunc(fst, 10)); nt.setNationality(trunc(lst, 32)); // hsession.save(nt); // } } } // // SAVE THE DETAIL TABLE (ecv_work_experience) NodeList wList = XPathAPI.selectNodeList(doc, "//workexperience"); if (wList.getLength() > 0) { for (int s = 0; s < wList.getLength(); s++) { String activities = null; String name = null; String address = null; String mun = null; String zipCode = null; String fromDay = null; String fromMonth = null; String fromYear = null; String toDay = null; String toMonth = null; String toYear = null; String posCode = null; String pos = null; String countryCd = null; String countryName = null; String sectorCode = null; String sector = null; Node w = wList.item(s); if (w.getNodeType() == Node.ELEMENT_NODE) { Node tmp = XPathAPI.selectSingleNode(w, "position/code"); if (tmp != null && tmp.getFirstChild() != null) posCode = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "position/label"); if (tmp != null && tmp.getFirstChild() != null) pos = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "activities"); if (tmp != null && tmp.getFirstChild() != null) activities = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "employer/name"); if (tmp != null && tmp.getFirstChild() != null) name = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "employer/address/addressLine"); if (tmp != null && tmp.getFirstChild() != null) address = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "employer/address/municipality"); if (tmp != null && tmp.getFirstChild() != null) mun = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "employer/address/postalCode"); if (tmp != null && tmp.getFirstChild() != null) zipCode = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "employer/address/country/code"); if (tmp != null && tmp.getFirstChild() != null) countryCd = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "employer/address/country/label"); if (tmp != null && tmp.getFirstChild() != null) countryName = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "employer/sector/code"); if (tmp != null && tmp.getFirstChild() != null) sectorCode = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "employer/sector/label"); if (tmp != null && tmp.getFirstChild() != null) sector = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "period/from/year"); if (tmp != null && tmp.getFirstChild() != null) fromYear = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "period/from/month"); if (tmp != null && tmp.getFirstChild() != null) { fromMonth = tmp.getFirstChild().getNodeValue(); fromMonth = fromMonth.replaceAll("-", ""); } tmp = XPathAPI.selectSingleNode(w, "period/from/day"); if (tmp != null && tmp.getFirstChild() != null) { fromDay = tmp.getFirstChild().getNodeValue(); fromDay = fromDay.replaceAll("-", ""); } tmp = XPathAPI.selectSingleNode(w, "period/to/year"); if (tmp != null && tmp.getFirstChild() != null) toYear = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(w, "period/to/month"); if (tmp != null && tmp.getFirstChild() != null) { toMonth = tmp.getFirstChild().getNodeValue(); toMonth = toMonth.replaceAll("-", ""); } tmp = XPathAPI.selectSingleNode(w, "period/to/day"); if (tmp != null && tmp.getFirstChild() != null) { toDay = tmp.getFirstChild().getNodeValue(); toDay = toDay.replaceAll("-", ""); } WorkExperience we = new WorkExperience(); we.setXml(xml); we.setActivities(trunc(activities, 100)); we.setEmplName(trunc(name, 50)); we.setCodePos(trunc(posCode, 6)); we.setCodeSector(trunc(sectorCode, 3)); we.setCountry(trunc(countryName, 30)); we.setCountryCode(trunc(countryCd, 3)); we.setDayFrom(trunc(fromDay, 2)); we.setDayTo(trunc(toDay, 2)); we.setEmplAddress(trunc(address, 50)); we.setEmplMunic(trunc(mun, 50)); we.setEmplZcode(trunc(zipCode, 10)); we.setMonthFrom(trunc(fromMonth, 2)); we.setMonthTo(trunc(toMonth, 2)); we.setPos(trunc(pos, 1024)); we.setSector(trunc(sector, 1024)); we.setYearFrom(trunc(fromYear, 4)); we.setYearTo(trunc(toYear, 4)); // hsession.save(we); // } } } // //SAVE THE DETAIL TABLE (ecv_education) NodeList eduLst = XPathAPI.selectNodeList(doc, "//education"); if (eduLst.getLength() > 0) { for (int s = 0; s < eduLst.getLength(); s++) { String skills = null; String name = null; String address = null; String mun = null; String zipCode = null; String title = null; String fromDay = null; String fromMonth = null; String fromYear = null; String toDay = null; String toMonth = null; String toYear = null; String levCode = null; String lev = null; String countryCd = null; String countryName = null; String eduCode = null; String edu = null; Node e = eduLst.item(s); if (e.getNodeType() == Node.ELEMENT_NODE) { Node tmp = XPathAPI.selectSingleNode(e, "title"); if (tmp != null && tmp.getFirstChild() != null) title = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(e, "skills"); if (tmp != null && tmp.getFirstChild() != null) skills = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(e, "organisation/name"); if (tmp != null && tmp.getFirstChild() != null) name = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(e, "organisation/address/addressLine"); if (tmp != null && tmp.getFirstChild() != null) address = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(e, "organisation/address/municipality"); if (tmp != null && tmp.getFirstChild() != null) mun = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(e, "organisation/address/postalCode"); if (tmp != null && tmp.getFirstChild() != null) zipCode = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(e, "organisation/address/country/code"); if (tmp != null && tmp.getFirstChild() != null) countryCd = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(e, "organisation/address/country/label"); if (tmp != null && tmp.getFirstChild() != null) countryName = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(e, "organisation/type"); if (tmp != null && tmp.getFirstChild() != null) { } tmp = XPathAPI.selectSingleNode(e, "period/from/year"); if (tmp != null && tmp.getFirstChild() != null) fromYear = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(e, "period/from/month"); if (tmp != null && tmp.getFirstChild() != null) { fromMonth = tmp.getFirstChild().getNodeValue(); fromMonth = fromMonth.replaceAll("-", ""); } tmp = XPathAPI.selectSingleNode(e, "period/from/day"); if (tmp != null && tmp.getFirstChild() != null) { fromDay = tmp.getFirstChild().getNodeValue(); fromDay = fromDay.replaceAll("-", ""); } tmp = XPathAPI.selectSingleNode(e, "period/to/year"); if (tmp != null && tmp.getFirstChild() != null) toYear = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(e, "period/to/month"); if (tmp != null && tmp.getFirstChild() != null) { toMonth = tmp.getFirstChild().getNodeValue(); toMonth = toMonth.replaceAll("-", ""); } tmp = XPathAPI.selectSingleNode(e, "period/to/day"); if (tmp != null && tmp.getFirstChild() != null) { toDay = tmp.getFirstChild().getNodeValue(); toDay = toDay.replaceAll("-", ""); } tmp = XPathAPI.selectSingleNode(e, "level/code"); if (tmp != null && tmp.getFirstChild() != null) levCode = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(e, "level/label"); if (tmp != null && tmp.getFirstChild() != null) lev = tmp.getFirstChild().getNodeValue(); // Education ed = new Education(); ed.setXml(xml); ed.setTitle(trunc(title, 50)); ed.setSubject(trunc(skills, 200)); ed.setOrgName(trunc(name, 100)); ed.setCodeLevel(trunc(levCode, 2)); ed.setEduField(trunc(edu, 1024)); ed.setCountry(trunc(countryName, 50)); ed.setCountryCode(trunc(countryCd, 3)); ed.setDayFrom(trunc(fromDay, 2)); ed.setDayTo(trunc(toDay, 2)); ed.setOrgAddress(trunc(address, 100)); ed.setOrgMunic(trunc(mun, 50)); ed.setOrgZcode(trunc(zipCode, 10)); ed.setMonthFrom(trunc(fromMonth, 2)); ed.setMonthTo(trunc(toMonth, 2)); ed.setLevel(trunc(lev, 1024)); ed.setCodeEduField(trunc(eduCode, 5)); ed.setYearFrom(trunc(fromYear, 4)); ed.setYearTo(trunc(toYear, 4)); // hsession.save(ed); // } } } // SAVE THE DETAIL TABLE (ecv_language) NodeList fLangLst = XPathAPI.selectNodeList(doc, "//language[@type='europass:foreign']"); if (fLangLst.getLength() > 0) { for (int s = 0; s < fLangLst.getLength(); s++) { String lngCode = null; String lng = null; String listening = null; String reading = null; String spokeninteraction = null; String spokenproduction = null; String writing = null; Node fLang = fLangLst.item(s); if (fLang.getNodeType() == Node.ELEMENT_NODE) { Node tmp = XPathAPI.selectSingleNode(fLang, "code"); if (tmp.getFirstChild() != null) lngCode = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(fLang, "label"); if (tmp.getFirstChild() != null) lng = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(fLang, "level/listening"); if (tmp.getFirstChild() != null) listening = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(fLang, "level/reading"); if (tmp.getFirstChild() != null) reading = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(fLang, "level/spokeninteraction"); if (tmp.getFirstChild() != null) spokeninteraction = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(fLang, "level/spokenproduction"); if (tmp.getFirstChild() != null) spokenproduction = tmp.getFirstChild().getNodeValue(); tmp = XPathAPI.selectSingleNode(fLang, "level/writing"); if (tmp.getFirstChild() != null) writing = tmp.getFirstChild().getNodeValue(); Language lg = new Language(); lg.setXml(xml); lg.setCodeLang(trunc(lngCode, 3)); lg.setLang(trunc(lng, 30)); lg.setListening(trunc(listening, 2)); lg.setReading(trunc(reading, 2)); lg.setSpokenInteraction(trunc(spokeninteraction, 2)); lg.setSpokenProduction(trunc(spokenproduction, 2)); lg.setWriting(trunc(writing, 2)); // hsession.save(lg); // } } } // tx.commit(); // } catch (TransformerException te) { logs = "Error: " + "( transformer excpetion) " + te.getMessage(); } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); logs = "Error: " + "(parsing XML)" + e.getMessage(); } finally { hsession.close(); } return logs; }
From source file:net.algart.simagis.imageio.IIOMetadataToJsonConverter.java
private JSONObject compactTIFFSimilarNodes(Node node, String childrenName) throws JSONException { assert node != null; assert childrenName != null; long count = 0; for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling(), count++) { if (!childrenName.equals(child.getNodeName())) { return null; }// w ww . j a v a 2 s . co m // so, a child has the name, equal to childrenName if (child.getFirstChild() != null) { return null; } // so, a child has no own children final NamedNodeMap childAttributes = child.getAttributes(); if (childAttributes == null || childAttributes.getLength() != 1) { return null; } Node childAttr = childAttributes.item(0); String attrName = childAttr.getNodeName(); String attrValue = childAttr.getNodeValue(); if (!("value".equals(attrName)) || attrValue == null || attrValue.contains(",")) { return null; } // so, a child has only 1 attribute "value", which is a string, not containing "," } if (count <= MAX_NON_COMPACTED_SIMILAR_CHILDREN) { return null; } StringBuilder sb = new StringBuilder(); count = 0; for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling(), count++) { Node childAttr = child.getAttributes().item(0); if (count > 0) { sb.append(","); } sb.append(childAttr.getNodeValue()); } JSONObject result = new JSONObject(); result.put("childrenName", childrenName); result.put("childrenValue", sb.toString()); return result; }
From source file:de.elbe5.base.data.XmlData.java
public String getCData(Node node) { if (node.hasChildNodes()) { Node child = node.getFirstChild(); while (child != null) { if (child instanceof CDATASection) { return ((CDATASection) child).getData(); }/*from w w w . j a v a2 s . c o m*/ child = child.getNextSibling(); } } return ""; }
From source file:esg.security.yadis.XrdsDoc.java
protected Map extractElementsByParent(String ns, String elem, Set parents, Document document) { Map result = new HashMap(); NodeList nodes = document.getElementsByTagNameNS(ns, elem); Node node; for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i);/*from w w w . j a va 2s.co m*/ if (node == null || !parents.contains(node.getParentNode())) continue; String localId = node.getFirstChild() != null && node.getFirstChild().getNodeType() == Node.TEXT_NODE ? node.getFirstChild().getNodeValue() : null; result.put(node.getParentNode(), localId); } return result; }
From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.action.validation.BcrExperimentFieldValidator.java
/** * Determines if a tag is a simple text element * @param node to determine if it is a simple text element * @return true if a node is a simple text eleement *//*from ww w . jav a 2 s.c o m*/ private boolean isSimpleTextElement(final Node node) { // either the node has no children, or it has just one child of type text node // (i.e. false if it has other element nodes nested inside of it return !node.hasChildNodes() || (node.getChildNodes().getLength() == 1 && node.getFirstChild().getNodeType() == Node.TEXT_NODE); }
From source file:Main.java
/** * This method is a tree-search to help prevent against wrapping attacks. It checks that no * two Elements have ID Attributes that match the "value" argument, if this is the case then * "false" is returned. Note that a return value of "true" does not necessarily mean that * a matching Element has been found, just that no wrapping attack has been detected. *//*from w w w. j a v a 2 s. c o m*/ public static boolean protectAgainstWrappingAttack(Node startNode, String value) { Node startParent = startNode.getParentNode(); Node processedNode = null; Element foundElement = null; String id = value.trim(); if (id.charAt(0) == '#') { id = id.substring(1); } while (startNode != null) { if (startNode.getNodeType() == Node.ELEMENT_NODE) { Element se = (Element) startNode; NamedNodeMap attributes = se.getAttributes(); if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (attr.isId() && id.equals(attr.getValue())) { if (foundElement == null) { // Continue searching to find duplicates foundElement = attr.getOwnerElement(); } else { //log.debug("Multiple elements with the same 'Id' attribute value!"); return false; } } } } } processedNode = startNode; startNode = startNode.getFirstChild(); // no child, this node is done. if (startNode == null) { // close node processing, get sibling startNode = processedNode.getNextSibling(); } // no more siblings, get parent, all children // of parent are processed. while (startNode == null) { processedNode = processedNode.getParentNode(); if (processedNode == startParent) { return true; } // close parent node processing (processed node now) startNode = processedNode.getNextSibling(); } } return true; }
From source file:net.sourceforge.eclipsetrader.yahoo.FrenchNewsProvider.java
private void update() { Job job = new Job(NEWS_NAME) { @Override/*from w w w.j a v a2 s . com*/ protected IStatus run(IProgressMonitor monitor) { IPreferenceStore store = YahooPlugin.getDefault().getPreferenceStore(); List urls = new ArrayList(); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(FileLocator.openStream(YahooPlugin.getDefault().getBundle(), new Path(NEWS_CONFIG_FILE), false)); NodeList childNodes = document.getFirstChild().getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); String nodeName = node.getNodeName(); if (nodeName.equalsIgnoreCase("category")) //$NON-NLS-1$ { String id = (node).getAttributes().getNamedItem("id").getNodeValue(); //$NON-NLS-1$ NodeList list = node.getChildNodes(); for (int x = 0; x < list.getLength(); x++) { Node item = list.item(x); nodeName = item.getNodeName(); Node value = item.getFirstChild(); if (value != null) { if (nodeName.equalsIgnoreCase("url")) //$NON-NLS-1$ { if (store.getBoolean(id)) urls.add(value.getNodeValue()); } } } } } } catch (Exception ex) { ex.printStackTrace(); } // List securities = CorePlugin.getRepository().allSecurities(); // monitor.beginTask("Fetching" + NEWS_NAME, securities.size() + urls.size()); // // for (Iterator iter = securities.iterator(); iter.hasNext(); ) // { // Security security = (Security) iter.next(); // try { // String url = NEWS_SECURITY_URL_BASE + security.getCode().toLowerCase(); // monitor.subTask(url); // update(new URL(url), security); // } catch(Exception e) { // CorePlugin.logException(e); // } // monitor.worked(1); // } for (Iterator iter = urls.iterator(); iter.hasNext();) { String url = (String) iter.next(); try { monitor.subTask(url); update(new URL(url)); } catch (Exception e) { CorePlugin.logException(e); } monitor.worked(1); } monitor.done(); return Status.OK_STATUS; } }; job.setUser(false); job.schedule(); }
From source file:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java
private static <T> T nodeToType(XPathWorkspace w, Node node, Class<T> clazz, Converter converter) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ConversionException, DOMException, ParseException { if (clazz.isPrimitive()) { return converter.convert(resolveHref(w, node).getTextContent(), clazz); } else if (clazz.equals(String.class)) { return clazz.cast(resolveHref(w, node).getTextContent()); } else if (clazz.equals(byte[].class)) { try {/*from w w w .jav a 2s . c om*/ return clazz.cast(Base64.decodeBase64(resolveHref(w, node).getTextContent().getBytes("ISO8859-1"))); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } else if (clazz.equals(Calendar.class)) { Date date = fmt.get().parse(resolveHref(w, node).getTextContent()); Calendar c = Calendar.getInstance(); c.setTime(date); return clazz.cast(c); } else if (clazz.isArray()) { Class<?> ct = clazz.getComponentType(); List<Object> elements = new ArrayList<Object>(); node = resolveHref(w, node); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) continue; elements.add(nodeToType(w, child, ct, converter)); } return clazz.cast(elements.toArray((Object[]) Array.newInstance(ct, elements.size()))); } else { T instance = clazz.newInstance(); node = resolveHref(w, node); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) continue; String nn = child.getLocalName(); Method setter = ClassUtil.findSetter(clazz, nn); setter.invoke(instance, nodeToType(w, resolveHref(w, child), setter.getParameterTypes()[0], converter)); } return instance; } }
From source file:org.dasein.cloud.ibm.sce.identity.keys.SSHKeys.java
private @Nullable SSHKeypair toKeyPair(@Nonnull ProviderContext ctx, @Nullable Node node, boolean post) throws CloudException, InternalException { if (node == null || !node.hasChildNodes()) { return null; }/*w ww.j av a 2s . c om*/ NodeList attributes = node.getChildNodes(); String regionId = ctx.getRegionId(); if (regionId == null) { throw new CloudException("No region established for context"); } SSHKeypair kp = new SSHKeypair(); kp.setProviderOwnerId(ctx.getAccountNumber()); kp.setProviderRegionId(regionId); kp.setFingerprint("Fake out test cases because SCE does not provide a fingerprint"); for (int i = 0; i < attributes.getLength(); i++) { Node attr = attributes.item(i); String nodeName = attr.getNodeName(); if (nodeName.equalsIgnoreCase("KeyName") && attr.hasChildNodes()) { String id = attr.getFirstChild().getNodeValue().trim(); kp.setProviderKeypairId(id); kp.setName(id); } else if (nodeName.equalsIgnoreCase("KeyMaterial") && attr.hasChildNodes()) { String material = attr.getFirstChild().getNodeValue().trim(); try { if (post) { kp.setPrivateKey(material.getBytes("utf-8")); } else { kp.setPublicKey(material); } } catch (UnsupportedEncodingException e) { throw new InternalException(e); } } } if (kp.getProviderKeypairId() == null) { return null; } return kp; }