List of usage examples for org.w3c.dom Node hasChildNodes
public boolean hasChildNodes();
From source file:com.naryx.tagfusion.cfm.document.cfDOCUMENT.java
private Node findNode(Node _node, String _tagName) { String name = _node.getNodeName(); if ((name != null) && name.equals(_tagName)) return _node; if (_node.hasChildNodes()) { NodeList children = _node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node n = findNode(children.item(i), _tagName); if (n != null) return n; }// w w w.java 2s . c o m } return null; }
From source file:org.dasein.cloud.aws.storage.S3.java
private String getRegion(@Nonnull String bucket, boolean reload) throws CloudException, InternalException { ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new CloudException("No context was set for this request"); }// w w w . j a va2 s . c o m Cache<Affinity> cache = Cache.getInstance(provider, "affinity", Affinity.class, CacheLevel.CLOUD_ACCOUNT, new TimePeriod<Day>(1, TimePeriod.DAY)); Iterable<Affinity> affinities = cache.get(ctx); Affinity affinity; if (affinities == null) { affinity = new Affinity(); cache.put(ctx, Collections.singletonList(affinity)); } else { affinity = affinities.iterator().next(); } Constraint c = affinity.constraints.get(bucket); if (reload || c == null || c.timeout <= System.currentTimeMillis()) { S3Method method = new S3Method(provider, S3Action.LOCATE_BUCKET); String location = null; S3Response response; method = new S3Method(provider, S3Action.LOCATE_BUCKET); try { response = method.invoke(bucket, "?location"); } catch (S3Exception e) { response = null; } if (response != null) { NodeList constraints = response.document.getElementsByTagName("LocationConstraint"); if (constraints.getLength() > 0) { Node constraint = constraints.item(0); if (constraint != null && constraint.hasChildNodes()) { location = constraint.getFirstChild().getNodeValue().trim(); } } } c = new Constraint(toRegion(location)); affinity.constraints.put(bucket, c); } return c.regionId; }
From source file:com.connexta.arbitro.AbstractPolicy.java
/** * Constructor used by child classes to initialize the shared data from a DOM root node. * * @param root the DOM root of the policy * @param policyPrefix either "Policy" or "PolicySet" * @param combiningName name of the field naming the combining alg * the XACML policy, if null use default factories * @throws ParsingException if the policy is invalid *//* w ww. ja v a 2 s . c o m*/ protected AbstractPolicy(Node root, String policyPrefix, String combiningName) throws ParsingException { // get the attributes, all of which are common to Policies NamedNodeMap attrs = root.getAttributes(); try { // get the attribute Id idAttr = new URI(attrs.getNamedItem(policyPrefix + "Id").getNodeValue()); } catch (Exception e) { throw new ParsingException("Error parsing required attribute " + policyPrefix + "Id", e); } // see if there's a version Node versionNode = attrs.getNamedItem("Version"); if (versionNode != null) { version = versionNode.getNodeValue(); } else { // assign the default version version = "1.0"; } // now get the combining algorithm... try { URI algId = new URI(attrs.getNamedItem(combiningName).getNodeValue()); CombiningAlgFactory factory = Balana.getInstance().getCombiningAlgFactory(); combiningAlg = factory.createAlgorithm(algId); } catch (Exception e) { throw new ParsingException("Error parsing combining algorithm" + " in " + policyPrefix, e); } // ...and make sure it's the right kind if (policyPrefix.equals("Policy")) { if (!(combiningAlg instanceof RuleCombiningAlgorithm)) throw new ParsingException("Policy must use a Rule " + "Combining Algorithm"); } else { if (!(combiningAlg instanceof PolicyCombiningAlgorithm)) throw new ParsingException("PolicySet must use a Policy " + "Combining Algorithm"); } // do an initial pass through the elements to pull out the // defaults, if any, so we can setup the meta-data NodeList children = root.getChildNodes(); String xpathVersion = null; for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (DOMHelper.getLocalName(child).equals(policyPrefix + "Defaults")) handleDefaults(child); } // with the defaults read, create the meta-data metaData = new PolicyMetaData(root.getNamespaceURI(), defaultVersion); // now read the remaining policy elements obligationExpressions = new HashSet<AbstractObligation>(); adviceExpressions = new HashSet<AdviceExpression>(); parameters = new ArrayList<CombinerParameter>(); children = root.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); String cname = DOMHelper.getLocalName(child); if (cname.equals("Description")) { if (child.hasChildNodes()) { description = child.getFirstChild().getNodeValue(); } } else if (cname.equals("Target")) { target = TargetFactory.getFactory().getTarget(child, metaData); } else if (cname.equals("ObligationExpressions") || cname.equals("Obligations")) { parseObligationExpressions(child); } else if (cname.equals("AdviceExpressions")) { parseAdviceExpressions(child); } else if (cname.equals("CombinerParameters")) { handleParameters(child); } } // finally, make sure the obligations and parameters are immutable obligationExpressions = Collections.unmodifiableSet(obligationExpressions); adviceExpressions = Collections.unmodifiableSet(adviceExpressions); parameters = Collections.unmodifiableList(parameters); }
From source file:org.dasein.cloud.aws.storage.S3.java
private void loadBuckets(@Nonnull String regionId, @Nonnull Jiterator<Blob> iterator) throws CloudException, InternalException { S3Method method = new S3Method(provider, S3Action.LIST_BUCKETS); S3Response response;/*w w w . j a v a 2s. co m*/ NodeList blocks; try { response = method.invoke(null, null); } catch (S3Exception e) { logger.error(e.getSummary()); throw new CloudException(e); } blocks = response.document.getElementsByTagName("Bucket"); for (int i = 0; i < blocks.getLength(); i++) { Node object = blocks.item(i); String name = null; NodeList attrs; long ts = 0L; attrs = object.getChildNodes(); for (int j = 0; j < attrs.getLength(); j++) { Node attr = attrs.item(j); if (attr.getNodeName().equals("Name")) { name = attr.getFirstChild().getNodeValue().trim(); } else if (attr.getNodeName().equals("CreationDate")) { ts = provider.parseTime(attr.getFirstChild().getNodeValue().trim()); } } if (name == null) { throw new CloudException("Bad response from server."); } if (provider.getEC2Provider().isAWS()) { String location = null; method = new S3Method(provider, S3Action.LOCATE_BUCKET); try { response = method.invoke(name, "?location"); } catch (S3Exception e) { response = null; } if (response != null) { NodeList constraints = response.document.getElementsByTagName("LocationConstraint"); if (constraints.getLength() > 0) { Node constraint = constraints.item(0); if (constraint != null && constraint.hasChildNodes()) { location = constraint.getFirstChild().getNodeValue().trim(); } } } if (toRegion(location).equals(regionId)) { iterator.push(Blob.getInstance(regionId, getLocation(name, null), name, ts)); } } else { iterator.push(Blob.getInstance(regionId, getLocation(name, null), name, ts)); } } }
From source file:org.dasein.cloud.aws.AWSCloud.java
public Map<String, String> getTagsFromTagSet(Node attr) { if (attr == null || !attr.hasChildNodes()) { return null; }/*from w w w . j a v a 2 s.com*/ Map<String, String> tags = new HashMap<String, String>(); NodeList tagNodes = attr.getChildNodes(); for (int j = 0; j < tagNodes.getLength(); j++) { Tag t = toTag(tagNodes.item(j)); if (t != null) { tags.put(t.getKey(), t.getValue()); } } return tags; }
From source file:org.dasein.cloud.aws.AWSCloud.java
public void setTags(@Nonnull Node attr, @Nonnull Taggable item) { if (attr.hasChildNodes()) { NodeList tags = attr.getChildNodes(); for (int j = 0; j < tags.getLength(); j++) { Tag t = toTag(tags.item(j)); if (t != null && t.getValue() != null) { item.setTag(t.getKey(), t.getValue()); }/*from www . j a v a 2 s . c om*/ } } }
From source file:org.dasein.cloud.aws.AWSCloud.java
public Tag toTag(@Nonnull Node tag) { if (tag.getNodeName().equals("item") && tag.hasChildNodes()) { NodeList parts = tag.getChildNodes(); String key = null, value = null; for (int k = 0; k < parts.getLength(); k++) { Node part = parts.item(k); if (part.getNodeName().equalsIgnoreCase("key")) { if (part.hasChildNodes()) { key = part.getFirstChild().getNodeValue().trim(); }//w ww . j av a2 s.c om } else if (part.getNodeName().equalsIgnoreCase("value")) { if (part.hasChildNodes()) { value = part.getFirstChild().getNodeValue().trim(); } } } if (key != null && value != null) { return new Tag(key, value); } } return null; }
From source file:org.dasein.cloud.aws.storage.S3.java
@Override public boolean isPublic(@Nullable String bucket, @Nullable String object) throws CloudException, InternalException { APITrace.begin(provider, "Blob.isPublic"); try {/*from w ww .j a v a 2s . c o m*/ if (bucket == null) { throw new CloudException("A bucket name was not specified"); } Document acl = getAcl(bucket, object); if (acl == null) { return false; } NodeList grants; grants = acl.getElementsByTagName("Grant"); for (int i = 0; i < grants.getLength(); i++) { boolean isAll = false, isRead = false; Node grant = grants.item(i); NodeList grantData; grantData = grant.getChildNodes(); for (int j = 0; j < grantData.getLength(); j++) { Node item = grantData.item(j); if (item.getNodeName().equals("Grantee")) { String type = item.getAttributes().getNamedItem("xsi:type").getNodeValue(); if (type.equals("Group")) { NodeList items = item.getChildNodes(); for (int k = 0; k < items.getLength(); k++) { Node n = items.item(k); if (n.getNodeName().equals("URI")) { if (n.hasChildNodes()) { String uri = n.getFirstChild().getNodeValue(); if (uri.equals("http://acs.amazonaws.com/groups/global/AllUsers")) { isAll = true; break; } } } if (isAll) { break; } } } } else if (item.getNodeName().equals("Permission")) { if (item.hasChildNodes()) { String perm = item.getFirstChild().getNodeValue(); isRead = (perm.equals("READ") || perm.equals("FULL_CONTROL")); } } } if (isAll) { return isRead; } } return false; } finally { APITrace.end(); } }
From source file:com.github.podd.resources.RestletPoddClientImpl.java
private static void printNote2(NodeList nodeList, int prevValue) { for (int count = 0; count < nodeList.getLength(); count++) { Node tempNode = nodeList.item(count); // make sure it's element node. if (tempNode.getNodeType() == Node.ELEMENT_NODE) { // get node name and value if (!tempNode.getNodeName().startsWith("rdf:") && tempNode.getTextContent().length() > 0) { if (tempNode.getNodeName().startsWith("rdfs:label")) { System.out.println("Name" + ": " + tempNode.getTextContent()); } else if (tempNode.getNodeName().startsWith("rdfs:comment")) { System.out.println("Description" + ": " + tempNode.getTextContent()); } else if (tempNode.getNodeName().startsWith("hasPotColumnNumberOverall")) { System.out.println("Lane" + ": " + tempNode.getTextContent()); } else if (tempNode.getNodeName().startsWith("hasPotPositionTray")) { System.out.println("Position" + ": " + tempNode.getTextContent()); } else if (tempNode.getNodeName().startsWith("hasValue")) { //if //prevValue = Integer.parseInt(tempNode.getNodeValue()); } else { System.out.println(tempNode.getNodeName() + ": " + tempNode.getTextContent()); }/*from www .j a v a 2 s .com*/ } if (tempNode.hasChildNodes()) { // loop again if has child node printNote(tempNode.getChildNodes()); System.out.println(""); } } } }
From source file:com.piusvelte.sonet.core.OAuthLogin.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setResult(RESULT_CANCELED);/* w w w. ja v a 2 s . c om*/ mHttpClient = SonetHttpClient.getThreadSafeClient(getApplicationContext()); mLoadingDialog = new ProgressDialog(this); mLoadingDialog.setMessage(getString(R.string.loading)); mLoadingDialog.setCancelable(true); mLoadingDialog.setOnCancelListener(this); mLoadingDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, getString(android.R.string.cancel), this); Intent intent = getIntent(); if (intent != null) { Bundle extras = intent.getExtras(); if (extras != null) { int service = extras.getInt(Sonet.Accounts.SERVICE, Sonet.INVALID_SERVICE); mServiceName = Sonet.getServiceName(getResources(), service); mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); mAccountId = extras.getLong(Sonet.EXTRA_ACCOUNT_ID, Sonet.INVALID_ACCOUNT_ID); mSonetWebView = new SonetWebView(); final ProgressDialog loadingDialog = new ProgressDialog(this); final AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() { @Override protected String doInBackground(String... args) { try { return mSonetOAuth.getAuthUrl(args[0], args[1], args[2], args[3], Boolean.parseBoolean(args[4]), mHttpClient); } catch (OAuthMessageSignerException e) { e.printStackTrace(); } catch (OAuthNotAuthorizedException e) { e.printStackTrace(); } catch (OAuthExpectationFailedException e) { e.printStackTrace(); } catch (OAuthCommunicationException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String url) { if (loadingDialog.isShowing()) loadingDialog.dismiss(); // load the webview if (url != null) { mSonetWebView.open(url); } else { (Toast.makeText(OAuthLogin.this, String.format(getString(R.string.oauth_error), mServiceName), Toast.LENGTH_LONG)).show(); OAuthLogin.this.finish(); } } }; loadingDialog.setMessage(getString(R.string.loading)); loadingDialog.setCancelable(true); loadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { if (!asyncTask.isCancelled()) asyncTask.cancel(true); dialog.cancel(); OAuthLogin.this.finish(); } }); loadingDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, getString(android.R.string.cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (!asyncTask.isCancelled()) asyncTask.cancel(true); dialog.cancel(); OAuthLogin.this.finish(); } }); switch (service) { case TWITTER: mSonetOAuth = new SonetOAuth(TWITTER_KEY, TWITTER_SECRET); asyncTask.execute(String.format(TWITTER_URL_REQUEST, TWITTER_BASE_URL), String.format(TWITTER_URL_ACCESS, TWITTER_BASE_URL), String.format(TWITTER_URL_AUTHORIZE, TWITTER_BASE_URL), TWITTER_CALLBACK.toString(), Boolean.toString(true)); loadingDialog.show(); break; case FACEBOOK: mSonetWebView.open(String.format(FACEBOOK_URL_AUTHORIZE, FACEBOOK_BASE_URL, FACEBOOK_ID, FACEBOOK_CALLBACK.toString())); break; case MYSPACE: mSonetOAuth = new SonetOAuth(MYSPACE_KEY, MYSPACE_SECRET); asyncTask.execute(MYSPACE_URL_REQUEST, MYSPACE_URL_ACCESS, MYSPACE_URL_AUTHORIZE, MYSPACE_CALLBACK.toString(), Boolean.toString(true)); loadingDialog.show(); break; case FOURSQUARE: mSonetWebView.open(String.format(FOURSQUARE_URL_AUTHORIZE, FOURSQUARE_KEY, FOURSQUARE_CALLBACK.toString())); break; case LINKEDIN: mSonetOAuth = new SonetOAuth(LINKEDIN_KEY, LINKEDIN_SECRET); asyncTask.execute(LINKEDIN_URL_REQUEST, LINKEDIN_URL_ACCESS, LINKEDIN_URL_AUTHORIZE, LINKEDIN_CALLBACK.toString(), Boolean.toString(true)); loadingDialog.show(); break; case SMS: Cursor c = getContentResolver().query(Accounts.getContentUri(this), new String[] { Accounts._ID }, Accounts.SERVICE + "=?", new String[] { Integer.toString(SMS) }, null); if (c.moveToFirst()) { (Toast.makeText(OAuthLogin.this, "SMS has already been added.", Toast.LENGTH_LONG)).show(); } else { addAccount(getResources().getStringArray(R.array.service_entries)[SMS], null, null, 0, SMS, null); } c.close(); finish(); break; case RSS: // prompt for RSS url final EditText rss_url = new EditText(this); rss_url.setSingleLine(); new AlertDialog.Builder(OAuthLogin.this).setTitle(R.string.rss_url).setView(rss_url) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, int which) { // test the url and add if valid, else Toast error mLoadingDialog.show(); (new AsyncTask<String, Void, String>() { String url; @Override protected String doInBackground(String... params) { url = rss_url.getText().toString(); return SonetHttpClient.httpResponse(mHttpClient, new HttpGet(url)); } @Override protected void onPostExecute(String response) { mLoadingDialog.dismiss(); if (response != null) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; try { db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(response)); Document doc = db.parse(is); // test parsing... NodeList nodes = doc.getElementsByTagName(Sitem); if (nodes.getLength() > 0) { // check for an image NodeList images = doc.getElementsByTagName(Simage); if (images.getLength() > 0) { NodeList imageChildren = images.item(0).getChildNodes(); Node n = imageChildren.item(0); if (n.getNodeName().toLowerCase().equals(Surl)) { if (n.hasChildNodes()) { n.getChildNodes().item(0).getNodeValue(); } } } NodeList children = nodes.item(0).getChildNodes(); String date = null; String title = null; String description = null; String link = null; int values_count = 0; for (int child = 0, c2 = children.getLength(); (child < c2) && (values_count < 4); child++) { Node n = children.item(child); if (n.getNodeName().toLowerCase().equals(Spubdate)) { values_count++; if (n.hasChildNodes()) { date = n.getChildNodes().item(0).getNodeValue(); } } else if (n.getNodeName().toLowerCase() .equals(Stitle)) { values_count++; if (n.hasChildNodes()) { title = n.getChildNodes().item(0) .getNodeValue(); } } else if (n.getNodeName().toLowerCase() .equals(Sdescription)) { values_count++; if (n.hasChildNodes()) { StringBuilder sb = new StringBuilder(); NodeList descNodes = n.getChildNodes(); for (int dn = 0, dn2 = descNodes .getLength(); dn < dn2; dn++) { Node descNode = descNodes.item(dn); if (descNode .getNodeType() == Node.TEXT_NODE) { sb.append(descNode.getNodeValue()); } } // strip out the html tags description = sb.toString() .replaceAll("\\<(.|\n)*?>", ""); } } else if (n.getNodeName().toLowerCase() .equals(Slink)) { values_count++; if (n.hasChildNodes()) { link = n.getChildNodes().item(0).getNodeValue(); } } } if (Sonet.HasValues( new String[] { title, description, link, date })) { final EditText url_name = new EditText(OAuthLogin.this); url_name.setSingleLine(); new AlertDialog.Builder(OAuthLogin.this) .setTitle(R.string.rss_channel) .setView(url_name) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog1, int which) { addAccount( url_name.getText() .toString(), null, null, 0, RSS, url); dialog1.dismiss(); dialog.dismiss(); finish(); } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog1, int which) { dialog1.dismiss(); dialog.dismiss(); finish(); } }) .show(); } else { (Toast.makeText(OAuthLogin.this, "Feed is missing standard fields", Toast.LENGTH_LONG)).show(); } } else { (Toast.makeText(OAuthLogin.this, "Invalid feed", Toast.LENGTH_LONG)).show(); dialog.dismiss(); finish(); } } catch (ParserConfigurationException e) { Log.e(TAG, e.toString()); (Toast.makeText(OAuthLogin.this, "Invalid feed", Toast.LENGTH_LONG)).show(); dialog.dismiss(); finish(); } catch (SAXException e) { Log.e(TAG, e.toString()); (Toast.makeText(OAuthLogin.this, "Invalid feed", Toast.LENGTH_LONG)).show(); dialog.dismiss(); finish(); } catch (IOException e) { Log.e(TAG, e.toString()); (Toast.makeText(OAuthLogin.this, "Invalid feed", Toast.LENGTH_LONG)).show(); dialog.dismiss(); finish(); } } else { (Toast.makeText(OAuthLogin.this, "Invalid URL", Toast.LENGTH_LONG)) .show(); dialog.dismiss(); finish(); } } }).execute(rss_url.getText().toString()); } }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); } }).show(); break; case IDENTICA: mSonetOAuth = new SonetOAuth(IDENTICA_KEY, IDENTICA_SECRET); asyncTask.execute(String.format(IDENTICA_URL_REQUEST, IDENTICA_BASE_URL), String.format(IDENTICA_URL_ACCESS, IDENTICA_BASE_URL), String.format(IDENTICA_URL_AUTHORIZE, IDENTICA_BASE_URL), IDENTICA_CALLBACK.toString(), Boolean.toString(true)); loadingDialog.show(); break; case GOOGLEPLUS: mSonetWebView.open( String.format(GOOGLEPLUS_AUTHORIZE, GOOGLE_CLIENTID, "urn:ietf:wg:oauth:2.0:oob")); break; case CHATTER: mSonetWebView .open(String.format(CHATTER_URL_AUTHORIZE, CHATTER_KEY, CHATTER_CALLBACK.toString())); break; case PINTEREST: Cursor pinterestAccount = getContentResolver().query(Accounts.getContentUri(this), new String[] { Accounts._ID }, Accounts.SERVICE + "=?", new String[] { Integer.toString(PINTEREST) }, null); if (pinterestAccount.moveToFirst()) { (Toast.makeText(OAuthLogin.this, "Pinterest has already been added.", Toast.LENGTH_LONG)) .show(); } else { (Toast.makeText(OAuthLogin.this, "Pinterest currently allows only public, non-authenticated viewing.", Toast.LENGTH_LONG)).show(); String[] values = getResources().getStringArray(R.array.service_values); String[] entries = getResources().getStringArray(R.array.service_entries); for (int i = 0, l = values.length; i < l; i++) { if (Integer.toString(PINTEREST).equals(values[i])) { addAccount(entries[i], null, null, 0, PINTEREST, null); break; } } } pinterestAccount.close(); finish(); break; default: this.finish(); } } } }