/** * WhiteboardObjectTextJabberImpl constructor. * * @param xml the XML string object to parse. */ public WhiteboardObjectTextJabberImpl(String xml) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); InputStream in = new ByteArrayInputStream(xml.getBytes()); Document doc = builder.parse(in); Element e = doc.getDocumentElement(); String elementName = e.getNodeName(); if (elementName.equals("text")) { // we have a text String id = e.getAttribute("id"); double x = Double.parseDouble(e.getAttribute("x")); double y = Double.parseDouble(e.getAttribute("y")); String fill = e.getAttribute("fill"); String fontFamily = e.getAttribute("font-family"); int fontSize = Integer.parseInt(e.getAttribute("font-size")); String text = e.getTextContent(); this.setID(id); this.setWhiteboardPoint(new WhiteboardPoint(x, y)); this.setFontName(fontFamily); this.setFontSize(fontSize); this.setText(text); this.setColor(Color.decode(fill).getRGB()); } } catch (ParserConfigurationException ex) { if (logger.isDebugEnabled()) logger.debug("Problem WhiteboardObject : " + xml); } catch (IOException ex) { if (logger.isDebugEnabled()) logger.debug("Problem WhiteboardObject : " + xml); } catch (Exception ex) { if (logger.isDebugEnabled()) logger.debug("Problem WhiteboardObject : " + xml); } }
/** used for cut and paste. */ public void addObjectFromClipboard(String a_value) throws CircularIncludeException { Reader reader = new StringReader(a_value); Document document = null; try { document = UJAXP.getDocument(reader); } catch (Exception e) { e.printStackTrace(); return; } // try-catch Element root = document.getDocumentElement(); if (!root.getNodeName().equals("clipboard")) { return; } // if Node child; for (child = root.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) { continue; } // if Element element = (Element) child; IGlyphFactory factory = GlyphFactory.getFactory(); if (XModule.isMatch(element)) { EModuleInvoke module = (EModuleInvoke) factory.createXModule(element); addModule(module); continue; } // if if (XContour.isMatch(element)) { EContour contour = (EContour) factory.createXContour(element); addContour(contour); continue; } // if if (XInclude.isMatch(element)) { EIncludeInvoke include = (EIncludeInvoke) factory.createXInclude(element); addInclude(include); continue; } // if } // while }
/** * ** Returns a ReverseGeocode instance containing address information ** @param gp The GeoPoint * ** @return The ReverseGeocode instance */ private ReverseGeocode getAddressReverseGeocode(GeoPoint gp, String localeStr) { /* URL */ String url = this.getAddressReverseGeocodeURL(gp); Print.logInfo("Address URL: " + url); /* create XML document */ Document xmlDoc = GetXMLDocument(url); if (xmlDoc == null) { return null; } /* create ReverseGeocode response */ Element reversegeocode = xmlDoc.getDocumentElement(); if (!reversegeocode.getTagName().equalsIgnoreCase(TAG_reversegeocode)) { return null; } /* init */ String address_val = null; // null address String house_val = null; // house number String road_val = null; // street name String city_val = null; // city name String county_val = null; // county name String state_val = null; // state/province String postcode_val = null; // postal code String country_name_val = null; // country name String country_code_val = null; // country code // full address NodeList resultList = XMLTools.getChildElements(reversegeocode, TAG_result); for (int r = 0; r < resultList.getLength(); r++) { Element result = (Element) resultList.item(r); // String osmType = XMLTools.getAttribute(result, ATTR_osm_type, null, false); address_val = XMLTools.getNodeText(result, " ", false); break; // only the first element } // address components NodeList addresspartsList = XMLTools.getChildElements(reversegeocode, TAG_addressparts); for (int a = 0; (a < addresspartsList.getLength()); a++) { Element addressparts = (Element) addresspartsList.item(a); NodeList addresspartsChildren = addressparts.getChildNodes(); for (int ac = 0; ac < addresspartsChildren.getLength(); ac++) { Node child = addresspartsChildren.item(ac); if (!(child instanceof Element)) { continue; } Element elem = (Element) child; String elemName = elem.getNodeName(); if (elemName.equalsIgnoreCase(TAG_house)) { house_val = XMLTools.getNodeText(elem, " ", false); } else if (elemName.equalsIgnoreCase(TAG_tram)) { // ignore } else if (elemName.equalsIgnoreCase(TAG_road)) { road_val = XMLTools.getNodeText(elem, " ", false); } else if (elemName.equalsIgnoreCase(TAG_residential)) { // ignore } else if (elemName.equalsIgnoreCase(TAG_village)) { // ignore } else if (elemName.equalsIgnoreCase(TAG_town)) { if (StringTools.isBlank(city_val)) { city_val = XMLTools.getNodeText(elem, " ", false); } } else if (elemName.equalsIgnoreCase(TAG_city)) { city_val = XMLTools.getNodeText(elem, " ", false); } else if (elemName.equalsIgnoreCase(TAG_county)) { county_val = XMLTools.getNodeText(elem, " ", false); } else if (elemName.equalsIgnoreCase(TAG_postcode)) { postcode_val = XMLTools.getNodeText(elem, " ", false); } else if (elemName.equalsIgnoreCase(TAG_state)) { state_val = XMLTools.getNodeText(elem, " ", false); } else if (elemName.equalsIgnoreCase(TAG_country)) { country_name_val = XMLTools.getNodeText(elem, " ", false); } else if (elemName.equalsIgnoreCase(TAG_country_code)) { country_code_val = StringTools.trim(XMLTools.getNodeText(elem, " ", false)).toUpperCase(); } else { // elemName unrecognized } } break; // only the first element } /* populate ReverseGeocode instance */ ReverseGeocode rg = new ReverseGeocode(); StringBuffer addr = new StringBuffer(); // house number /road if (!StringTools.isBlank(house_val)) { addr.append(house_val); if (!StringTools.isBlank(road_val)) { addr.append(" "); addr.append(road_val); rg.setStreetAddress(house_val + " " + road_val); } else { rg.setStreetAddress(house_val); } } else if (!StringTools.isBlank(road_val)) { addr.append(road_val); rg.setStreetAddress(road_val); } // city/county if (!StringTools.isBlank(city_val)) { if (addr.length() > 0) { addr.append(", "); } addr.append(city_val); rg.setCity(city_val); } if (!StringTools.isBlank(county_val)) { if (StringTools.isBlank(city_val)) { // "city" not provided, at least include the "county" if (addr.length() > 0) { addr.append(", "); } addr.append("[").append(county_val).append("]"); } // rg.setCounty(county_val); } // state/province if (!StringTools.isBlank(state_val)) { if (addr.length() > 0) { addr.append(", "); } addr.append(state_val); rg.setStateProvince(state_val); if (!StringTools.isBlank(postcode_val)) { addr.append(" ").append(postcode_val); rg.setPostalCode(postcode_val); } } else { if (!StringTools.isBlank(postcode_val)) { if (addr.length() > 0) { addr.append(", "); } addr.append(postcode_val); rg.setPostalCode(postcode_val); } } // country if (!StringTools.isBlank(country_code_val)) { if (country_code_val.equalsIgnoreCase("US")) { // if (addr.length() > 0) { addr.append(", "); } // addr.append("USA"); } else if (!StringTools.isBlank(country_name_val)) { if (addr.length() > 0) { addr.append(", "); } addr.append(country_name_val); } else { if (addr.length() > 0) { addr.append(", "); } addr.append(country_code_val); } rg.setCountryCode(country_code_val); } // full address rg.setFullAddress(addr.toString()); return rg; }
// http://code.google.com/apis/maps/documentation/geocoding/index.html public GeoPoint getGeocode(String address, String country) { /* URL */ String url = this.getGeoPointGeocodeURL(address, country); Print.logDebug("Google GC URL: " + url); /* create XML document */ Document xmlDoc = GetXMLDocument(url, this.getGeocodeTimeout()); if (xmlDoc == null) { return null; } /* vars */ String errCode = null; GeoPoint geoPoint = null; /* parse "xml" */ Element kml = xmlDoc.getDocumentElement(); if (kml.getTagName().equalsIgnoreCase(TAG_kml)) { NodeList ResponseList = XMLTools.getChildElements(kml, TAG_Response); for (int g = 0; (g < ResponseList.getLength()); g++) { Element response = (Element) ResponseList.item(g); NodeList responseNodes = response.getChildNodes(); for (int n = 0; n < responseNodes.getLength(); n++) { Node responseNode = responseNodes.item(n); if (!(responseNode instanceof Element)) { continue; } Element responseElem = (Element) responseNode; String responseNodeName = responseElem.getNodeName(); if (responseNodeName.equalsIgnoreCase(TAG_name)) { // <name>1600 Amphitheatre Parkway, Mountain View, CA</name> } else if (responseNodeName.equalsIgnoreCase(TAG_Status)) { // <Status> ... </Status> NodeList statusNodes = responseElem.getChildNodes(); for (int st = 0; st < statusNodes.getLength(); st++) { Node statusNode = statusNodes.item(st); if (!(statusNode instanceof Element)) { continue; } Element statusElem = (Element) statusNode; String statusNodeName = statusElem.getNodeName(); if (statusNodeName.equalsIgnoreCase(TAG_code)) { errCode = StringTools.trim(GoogleGeocodeV2.GetNodeText(statusElem)); // expect "200" break; // we only care about the 'code' } } } else if (responseNodeName.equalsIgnoreCase(TAG_Placemark)) { // <Placemark> ... </Placemark> String id = responseElem.getAttribute(ATTR_id); if ((id != null) && id.equals("p1")) { NodeList placemarkNodes = responseElem.getChildNodes(); for (int pm = 0; (geoPoint == null) && (pm < placemarkNodes.getLength()); pm++) { Node placemarkNode = placemarkNodes.item(pm); if (!(placemarkNode instanceof Element)) { continue; } Element placemarkElem = (Element) placemarkNode; String placemarkNodeName = placemarkElem.getNodeName(); if (placemarkNodeName.equalsIgnoreCase(TAG_Point)) { NodeList pointNodes = placemarkElem.getChildNodes(); for (int ptn = 0; (geoPoint == null) && (ptn < pointNodes.getLength()); ptn++) { Node pointNode = pointNodes.item(ptn); if (!(pointNode instanceof Element)) { continue; } Element pointElem = (Element) pointNode; String pointNodeName = pointElem.getNodeName(); if (pointNodeName.equalsIgnoreCase(TAG_coordinates)) { String ll[] = StringTools.split(GoogleGeocodeV2.GetNodeText(pointElem), ','); if (ll.length >= 2) { double lon = StringTools.parseDouble(ll[0], 0.0); // longitude is first double lat = StringTools.parseDouble(ll[1], 0.0); if (GeoPoint.isValid(lat, lon)) { geoPoint = new GeoPoint(lat, lon); break; // we only care about the 'GeoPoint' } } } } } } } else { // Print.logInfo("Skipping Placemark ID = %s", id); } } } } } /* create address */ if (geoPoint != null) { // GeoPoint found return geoPoint; } /* check for Google reverse-geocode limit exceeded */ if ((errCode != null) && errCode.equals("620")) { Print.logError("!!!! Google Reverse-Geocode Limit Exceeded [Error 620] !!!!"); } /* no reverse-geocode available */ return null; }
/* return reverse-geocode using nearest address */ public ReverseGeocode getAddressReverseGeocode(GeoPoint gp, String localeStr, boolean cache) { /* check for failover mode */ if (this.isReverseGeocodeFailoverMode()) { ReverseGeocodeProvider frgp = this.getFailoverReverseGeocodeProvider(); return frgp.getReverseGeocode(gp, localeStr, cache); } /* URL */ String url = this.getAddressReverseGeocodeURL(gp, localeStr); Print.logDebug("Google RG URL: " + url); // byte xmlBytes[] = HTMLTools.readPage(url); /* create XML document */ Document xmlDoc = GetXMLDocument(url, this.getReverseGeocodeTimeout()); if (xmlDoc == null) { return null; } /* vars */ String errCode = null; String address = null; /* parse "xml" */ Element kml = xmlDoc.getDocumentElement(); if (kml.getTagName().equalsIgnoreCase(TAG_kml)) { NodeList ResponseList = XMLTools.getChildElements(kml, TAG_Response); for (int g = 0; (g < ResponseList.getLength()); g++) { Element response = (Element) ResponseList.item(g); NodeList responseNodes = response.getChildNodes(); for (int n = 0; n < responseNodes.getLength(); n++) { Node responseNode = responseNodes.item(n); if (!(responseNode instanceof Element)) { continue; } Element responseElem = (Element) responseNode; String responseNodeName = responseElem.getNodeName(); if (responseNodeName.equalsIgnoreCase(TAG_name)) { // <name>40.479581,-117.773438</name> } else if (responseNodeName.equalsIgnoreCase(TAG_Status)) { // <Status> ... </Status> NodeList statusNodes = responseElem.getChildNodes(); for (int st = 0; st < statusNodes.getLength(); st++) { Node statusNode = statusNodes.item(st); if (!(statusNode instanceof Element)) { continue; } Element statusElem = (Element) statusNode; String statusNodeName = statusElem.getNodeName(); if (statusNodeName.equalsIgnoreCase(TAG_code)) { errCode = StringTools.trim(GoogleGeocodeV2.GetNodeText(statusElem)); // expect "200" break; // we only care about the 'code' } } } else if (responseNodeName.equalsIgnoreCase(TAG_Placemark)) { // <Placemark> ... </Placemark> String id = responseElem.getAttribute(ATTR_id); if ((id != null) && id.equals("p1")) { NodeList placemarkNodes = responseElem.getChildNodes(); for (int pm = 0; pm < placemarkNodes.getLength(); pm++) { Node placemarkNode = placemarkNodes.item(pm); if (!(placemarkNode instanceof Element)) { continue; } Element placemarkElem = (Element) placemarkNode; String placemarkNodeName = placemarkElem.getNodeName(); if (placemarkNodeName.equalsIgnoreCase(TAG_address)) { address = GoogleGeocodeV2.GetNodeText(placemarkElem); break; // we only care about the 'address' } } } else { // Print.logInfo("Skipping Placemark ID = %s", id); } } } } } /* create address */ if (FAILOVER_DEBUG) { errCode = "620"; } else if (!StringTools.isBlank(address)) { // address found ReverseGeocode rg = new ReverseGeocode(); rg.setFullAddress(address); return rg; } /* check for Google reverse-geocode limit exceeded */ if ((errCode != null) && errCode.equals("620")) { Print.logError("!!!! Google Reverse-Geocode Limit Exceeded [Error 620] !!!!"); if (this.hasFailoverReverseGeocodeProvider()) { this.startReverseGeocodeFailoverMode(); ReverseGeocodeProvider frgp = this.getFailoverReverseGeocodeProvider(); Print.logWarn("Failing over to '" + frgp.getName() + "'"); return frgp.getReverseGeocode(gp, localeStr, cache); } } /* no reverse-geocode available */ return null; }
void xml() { try { File file = new File("c:\\users\\jason\\desktop\\proxies.xml"); // Create instance of DocumentBuilderFactory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // Get the DocumentBuilder DocumentBuilder docBuilder = factory.newDocumentBuilder(); // Using existing XML Document Document doc = docBuilder.parse(file); // normalize the text doc.getDocumentElement().normalize(); // gets just the name of the root String simple_root = doc.getDocumentElement().getNodeName(); Element root = doc.getDocumentElement(); // gets the ip elements NodeList proxy = doc.getElementsByTagName("proxy"); // checks the make sure i got the ip elements by printing out the number of occurances int total = proxy.getLength(); NodeList list = doc.getElementsByTagName("*"); System.out.println("\nElements in the proxy file:"); int proxy_num = 1; for (int i = 0; i < list.getLength(); i++) { Node num2 = proxy.item(i); Element second = (Element) num2; Element element2 = (Element) list.item(i); NodeList text2 = element2.getChildNodes(); if (element2.getNodeName() != "proxies" && element2.getNodeName() != "proxy") { if (element2.getNodeName() == "ip") { System.out.println(""); System.out.println("Proxy #: " + proxy_num); proxy_num++; System.out.println( element2.getNodeName() + ": " + ((Node) text2.item(0)).getNodeValue().trim()); } else System.out.println( element2.getNodeName() + ": " + ((Node) text2.item(0)).getNodeValue().trim()); if (element2.getNodeName() == "source-ip") { ip = ((Node) text2.item(0)).getNodeValue().trim(); myArr.add(ip); jComboBox.addItem(ip); } } } // set up a transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); // create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); String xmlString = sw.toString(); OutputStream f0; byte buf[] = xmlString.getBytes(); f0 = new FileOutputStream("c:\\users\\jason\\desktop\\connections.xml"); for (int i = 0; i < buf.length; i++) { f0.write(buf[i]); } f0.close(); buf = null; } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
private ClassGraphics parse(Element grNode /*, boolean isRelation*/) { ClassGraphics newGraphics = new ClassGraphics(); newGraphics.setShowFields(Boolean.parseBoolean(grNode.getAttribute(ATR_SHOW_FIELDS))); // newGraphics.setRelation( isRelation ); NodeList list = grNode.getChildNodes(); for (int k = 0; k < list.getLength(); k++) { if (list.item(k).getNodeType() != Node.ELEMENT_NODE) continue; Element node = (Element) list.item(k); String nodeName = node.getNodeName(); Shape shape = null; if (EL_BOUNDS.equals(nodeName)) { Dim dim = getDim(node); newGraphics.setBounds(dim.x, dim.y, dim.width, dim.height); continue; } else if (EL_LINE.equals(nodeName)) { shape = makeLine(node, newGraphics); } else if (EL_RECT.equals(nodeName)) { Dim dim = getDim(node); Lineprops lp = getLineProps(node); shape = new Rect( dim.x, dim.y, dim.width, dim.height, getColor(node), isShapeFilled(node), lp.strokeWidth, lp.lineType); } else if (EL_OVAL.equals(nodeName)) { Dim dim = getDim(node); Lineprops lp = getLineProps(node); shape = new Oval( dim.x, dim.y, dim.width, dim.height, getColor(node), isShapeFilled(node), lp.strokeWidth, lp.lineType); } else if (EL_ARC.equals(nodeName)) { Dim dim = getDim(node); Lineprops lp = getLineProps(node); int startAngle = Integer.parseInt(node.getAttribute(ATR_START_ANGLE)); int arcAngle = Integer.parseInt(node.getAttribute(ATR_ARC_ANGLE)); shape = new Arc( dim.x, dim.y, dim.width, dim.height, startAngle, arcAngle, getColor(node), isShapeFilled(node), lp.strokeWidth, lp.lineType); } else if (EL_POLYGON.equals(nodeName)) { Lineprops lp = getLineProps(node); Polygon polygon = new Polygon(getColor(node), isShapeFilled(node), lp.strokeWidth, lp.lineType); // points NodeList points = node.getElementsByTagName(EL_POINT); int pointCount = points.getLength(); // arrays of polygon points int[] xs = new int[pointCount]; int[] ys = new int[pointCount]; // arrays of FIXED information about polygon points int[] fxs = new int[pointCount]; int[] fys = new int[pointCount]; int width = newGraphics.getBoundWidth(); int height = newGraphics.getBoundHeight(); for (int j = 0; j < pointCount; j++) { FixedCoords fc = getFixedCoords((Element) points.item(j), width, height, null); xs[j] = fc.x; fxs[j] = fc.fx; ys[j] = fc.y; fys[j] = fc.fy; } polygon.setPoints(xs, ys, fxs, fys); shape = polygon; } else if (EL_IMAGE.equals(nodeName)) { Dim dim = getDim(node); // image path should be relative to the package xml String imgPath = node.getAttribute(ATR_PATH); String fullPath = FileFuncs.preparePathOS(getWorkingDir() + imgPath); shape = new Image(dim.x, dim.y, fullPath, imgPath, isShapeFixed(node)); } else if (EL_TEXT.equals(nodeName)) { shape = makeText(node, newGraphics); /* * if (str.equals("*self")) newText.name = "self"; else if * (str.equals("*selfWithName")) newText.name = "selfName"; */ } if (shape != null) newGraphics.addShape(shape); } return newGraphics; }