// Serialize the bean using the specified namespace prefix & uri public String serialize(Object bean) throws IntrospectionException, IllegalAccessException { // Use the class name as the name of the root element String className = bean.getClass().getName(); String rootElementName = null; if (bean.getClass().isAnnotationPresent(ObjectXmlAlias.class)) { AnnotatedElement annotatedElement = bean.getClass(); ObjectXmlAlias aliasAnnotation = annotatedElement.getAnnotation(ObjectXmlAlias.class); rootElementName = aliasAnnotation.value(); } // Use the package name as the namespace URI Package pkg = bean.getClass().getPackage(); nsURI = pkg.getName(); // Remove a trailing semi-colon (;) if present (i.e. if the bean is an array) className = StringUtils.deleteTrailingChar(className, ';'); StringBuffer sb = new StringBuffer(className); String objectName = sb.delete(0, sb.lastIndexOf(".") + 1).toString(); domDocument = createDomDocument(objectName); document = domDocument.getDocument(); Element root = document.getDocumentElement(); // Parse the bean elements getBeanElements(root, rootElementName, className, bean); StringBuffer xml = new StringBuffer(); if (prettyPrint) xml.append(domDocument.serialize(lineSeperator, indentChars, includeXmlProlog)); else xml.append(domDocument.serialize(includeXmlProlog)); if (!includeTypeInfo) { int index = xml.indexOf(root.getNodeName()); xml.delete(index - 1, index + root.getNodeName().length() + 2); xml.delete(xml.length() - root.getNodeName().length() - 4, xml.length()); } return xml.toString(); }
private ClassLoaderStrategy getClassLoaderStrategy(String fullyQualifiedClassName) throws Exception { try { // Get just the package name StringBuffer sb = new StringBuffer(fullyQualifiedClassName); sb.delete(sb.lastIndexOf("."), sb.length()); currentPackage = sb.toString(); // Retrieve the Java classpath from the system properties String cp = System.getProperty("java.class.path"); String sepChar = System.getProperty("path.separator"); String[] paths = StringUtils.pieceList(cp, sepChar.charAt(0)); ClassLoaderStrategy cl = ClassLoaderUtil.getClassLoader(ClassLoaderUtil.FILE_SYSTEM_CLASS_LOADER, new String[] {}); // Iterate through paths until class with the specified name is found String classpath = StringUtils.replaceChar(currentPackage, '.', File.separatorChar); for (int i = 0; i < paths.length; i++) { Class[] classes = cl.getClasses(paths[i] + File.separatorChar + classpath, currentPackage); for (int j = 0; j < classes.length; j++) { if (classes[j].getName().equals(fullyQualifiedClassName)) { return ClassLoaderUtil.getClassLoader( ClassLoaderUtil.FILE_SYSTEM_CLASS_LOADER, new String[] {paths[i]}); } } } throw new Exception("Class could not be found."); } catch (Exception e) { System.err.println("Exception creating class loader strategy."); System.err.println(e.getMessage()); throw e; } }
private static String toString(NodeList self) { StringBuffer sb = new StringBuffer(); sb.append("["); Iterator it = DefaultGroovyMethods.iterator(self); while (it.hasNext()) { if (sb.length() > 1) sb.append(", "); sb.append(it.next().toString()); } sb.append("]"); return sb.toString(); }
/** * Initializes this object. * * @param key Property-key to use for locating initialization properties. * @param type Property-type to use for locating initialization properties. * @exception ProcessingException when initialization fails */ public void initialize(String key, String type) throws ProcessingException { super.initialize(key, type); StringBuffer errorBuf = new StringBuffer(); // serverName = getRequiredProperty(SERVER_NAME_PROP, errorBuf); headerLocation = getPropertyValue(NF_HEADER_LOCATION_PROP); isAsyncLocation = getPropertyValue(IS_ASYNCHRONOUS_LOCATION_PROP); orbAgentAddr = getPropertyValue(ORB_AGENT_ADDR_PROP); orbAgentPort = getPropertyValue(ORB_AGENT_PORT_PROP); orbAgentAddrLocation = getPropertyValue(ORB_AGENT_ADDR_PROP_LOCATION); orbAgentPortLocation = getPropertyValue(ORB_AGENT_PORT_PROP_LOCATION); if (!StringUtils.hasValue(isAsyncLocation)) { try { isAsync = StringUtils.getBoolean( (String) getRequiredPropertyValue(DEFAULT_IS_ASYNCHRONOUS_PROP, errorBuf)); } catch (FrameworkException fe) { errorBuf.append( "No value is specified for either " + IS_ASYNCHRONOUS_LOCATION_PROP + "or" + DEFAULT_IS_ASYNCHRONOUS_PROP + ". One of the values should be present" + fe.getMessage()); } } if (!StringUtils.hasValue(headerLocation)) { try { header = getRequiredPropertyValue(DEFAULT_HEADER_PROP, errorBuf); } catch (Exception e) { errorBuf.append( "No value is specified for " + NF_HEADER_LOCATION_PROP + "or" + DEFAULT_HEADER_PROP + ". One of the values should be present" + e.getMessage()); } } if (errorBuf.length() > 0) throw new ProcessingException(errorBuf.toString()); }
private static String FilterID(String id) { if (id == null) { return null; } else { StringBuffer newID = new StringBuffer(); int st = 0; for (int i = 0; i < id.length(); i++) { char ch = Character.toLowerCase(id.charAt(i)); if (Character.isLetterOrDigit(ch)) { newID.append(ch); st = 1; } else if (st == 1) { newID.append("_"); st = 0; } else { // ignore char } } while ((newID.length() > 0) && (newID.charAt(newID.length() - 1) == '_')) { newID.setLength(newID.length() - 1); } return newID.toString(); } }
/** * ** 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; }