private static JSSnippet createSnippetFromXml(String xml) { try { Document document = DOMUtil.createDocument(xml); XResult unid = DOMUtil.evaluateXPath(document, "unid"); XResult js = DOMUtil.evaluateXPath(document.getDocumentElement(), "js"); XResult html = DOMUtil.evaluateXPath(document.getDocumentElement(), "html"); XResult css = DOMUtil.evaluateXPath(document.getDocumentElement(), "css"); XResult docHtml = DOMUtil.evaluateXPath(document.getDocumentElement(), "docHtml"); XResult theme = DOMUtil.evaluateXPath(document.getDocumentElement(), "theme"); XResult description = DOMUtil.evaluateXPath(document.getDocumentElement(), "description"); XResult tags = DOMUtil.evaluateXPath(document.getDocumentElement(), "tags"); XResult labels = DOMUtil.evaluateXPath(document.getDocumentElement(), "labels"); JSSnippet snippet = new JSSnippet(); if (unid != null) snippet.setUnid(unid.getStringValue()); if (js != null) snippet.setJs(js.getStringValue()); if (html != null) snippet.setHtml(html.getStringValue()); if (css != null) snippet.setCss(css.getStringValue()); if (docHtml != null) snippet.setDocHtml(docHtml.getStringValue()); Properties p = new Properties(); snippet.init(p); if (theme != null && theme.getStringValue() != null) snippet.setTheme(theme.getStringValue()); if (description != null && description.getStringValue() != null) snippet.setDescription(description.getStringValue()); if (tags != null && tags.getValues() != null) snippet.setTags(tags.getValues()); if (labels != null && labels.getValues() != null) snippet.setLabels(labels.getValues()); return snippet; } catch (Exception e) { return null; } }
/** * this methd is called for every operation on the registered endpoint a test class is thus paired * with its own test data */ @Override protected Object testRequest(String method, Args args, Object content) throws ClientServicesException { // used by testLoad() if (method.equals("get") && args.getServiceUrl().equals("communities/service/atom/communities/all")) { try { return DOMUtil.createDocument(this.getClass().getResourceAsStream("allCommunities.xml")); } catch (XMLException e) { throw new ClientServicesException(e); } } // used by testCreate() if (method.equals("post") && args.getServiceUrl().equals("communities/service/atom/communities/my")) { // TODO: we should thest the API usage against the API XSD try { Node creationData = (DOMUtil.createDocument((String) content)); NamespaceContext ctx = new XMLFieldExtractor(new HashMap<String, XPathExpression>()).getNamespaceContext(); assertEquals( DOMUtil.evaluateXPath(creationData, "/entry/content", ctx).getStringValue(), CommunitiesTest.TEST_COMMUNITY_DESCRIPTION); assertEquals( DOMUtil.evaluateXPath(creationData, "/entry/title", ctx).getStringValue(), CommunitiesTest.NEW_COMMUNITY); } catch (Throwable e) { e.printStackTrace(); throw new ClientServicesException(e); } // TODO: use the standard response we would get return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; } // this is here just to trap and detect the requests that are to be implemented for the test to // work throw new ClientServicesException( new IllegalArgumentException( "Testing service only, please implement " + method + " at " + args.getServiceUrl() + " for " + args.getParameters() + " for " + content + " with " + args.getHeaders())); }
/** * Method to remove the user profile from cache. * * @param userId */ protected void removeProfileDataFromCache(String userId) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "removeProfileDataFromCache", userId); } if (isEmail(userId)) { String key; Set<String> keys = lruCache.getKeySet(); Iterator<String> itr = keys.iterator(); while (itr.hasNext()) { key = itr.next(); Document data = lruCache.get(key); // check if email in profile object is same as input userId String email = ""; try { email = DOMUtil.value(data, Profile.xpathMap.get("email")); } catch (XMLException e) { continue; } // cache hit if (StringUtil.equalsIgnoreCase(email, userId)) { lruCache.remove(key); } } // Cache miss } else { lruCache.remove(userId); } if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "removeProfileDataFromCache"); } }
private ConnectionsFeedXpath(String xpath) { XPathExpression xpathExpr = null; try { xpathExpr = DOMUtil.createXPath(xpath); } catch (XMLException e) { e.printStackTrace(); } this.path = xpathExpr; }
/** * This method returns a array of profiles. * * @param Data * @param ProfileService * @return Profile[] */ public static Profile[] convertToProfiles(ProfileService ps, Document data) { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "returnProfiles"); } Profile[] profile = null; if (data != null) { NodeList profileEntries = data.getElementsByTagName("entry"); profile = new Profile[profileEntries.getLength()]; if (profileEntries != null && profileEntries.getLength() > 0) { for (int i = 0; i < profileEntries.getLength(); i++) { Node entry = profileEntries.item(i); Document doc; try { doc = DOMUtil.createDocument(); Node dup = doc.importNode(entry, true); Element root = doc.createElement("feed"); root.appendChild(dup); doc.appendChild(root); profile[i] = new Profile(ps, DOMUtil.value(entry, "contributor/snx:userid")); profile[i].setData(doc); } catch (XMLException e) { if (logger.isLoggable(Level.SEVERE)) { logger.log( Level.SEVERE, "Error encountered while converting data into contacts profiles", e); } } } } } if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "returnProfiles"); } return profile; }
/* * Method to search the cache * * @param userId * @return Document */ private Document findInCache(String userId) { String key; Set<String> keys = lruCache.getKeySet(); Iterator<String> itr = keys.iterator(); while (itr.hasNext()) { key = itr.next(); Document data = lruCache.get(key); // check if email in profile object is same as input userId String email = ""; try { email = DOMUtil.value(data, Profile.xpathMap.get("email"), Profile.nameSpaceCtx); } catch (XMLException e) { continue; } // cache hit if (StringUtil.equalsIgnoreCase(email, userId)) { return data; } } // Cache miss return null; }
private String prettifyXML(String source, boolean compact) throws Exception { org.w3c.dom.Document d = DOMUtil.createDocument(source); return DOMUtil.getXMLString(d, compact); }