/** * If the outbound level is BINARY, convert the string field to binary, then pad to the left with * the appropriate number of zero bits to reach a number of bits specified by the bitLength * attribute of the TDT definition file. */ private void binaryPadding(Map<String, String> extraparams, Field tdtfield) { String fieldname = tdtfield.getName(); int reqbitlength = tdtfield.getBitLength(); String value; String binaryValue = fieldToBinary(tdtfield, extraparams); if (binaryValue.length() < reqbitlength) { int extraBitLength = reqbitlength - binaryValue.length(); StringBuilder zeroPaddedBinaryValue = new StringBuilder(""); for (int i = 0; i < extraBitLength; i++) { zeroPaddedBinaryValue.append("0"); } zeroPaddedBinaryValue.append(binaryValue); value = zeroPaddedBinaryValue.toString(); } else { if (binaryValue.length() > reqbitlength) throw new TDTException( "Binary value [" + binaryValue + "] for field " + fieldname + " exceeds maximum allowed " + reqbitlength + " bits. Decimal value was " + extraparams.get(fieldname)); value = binaryValue; } extraparams.put(fieldname, value); }
private AlbumBean addOrUpdateAlbum( Map<Integer, AlbumBean> map, SongBean song, boolean albumOnly) { // Add an album bean if (song != null && song.getAlbum() != null) { int hashCode = (song.getAlbum() + (albumOnly ? "" : '\uFFFF' + song.getArtist())).hashCode(); // Check if the album beam already exists AlbumBean album = map.get(hashCode); if (album == null) { album = new AlbumBean(); album.setAlbum(song.getAlbum()); map.put(hashCode, album); } // Update the album properties try { // Add the track id to the album bean album.addSong(song); album.incTracks(); album.setArtist(song.getArtist()); album.setGenre(song.getGenre()); album.setDisc_Count(song.getDisc_Count()); } catch (LibraryException le) { // There was an error adding the song to this album, remove it map.remove(hashCode); // Add to warning message warningList.add(song.getName() + " " + le); } return album; } return null; }
public static void testConfigurableForNonPrimitives() { Map<String, String> p = new HashMap<String, String>(); C config = Configurable.createConfigurable(C.class, p); assertNull(config.port()); p.put("port", "10"); config = Configurable.createConfigurable(C.class, p); assertEquals(Integer.valueOf(10), config.port()); // property port is // not set }
/** * converts a map to string * * @param map the map to convert * @param nvSep the nvp separator * @param entrySep the separator of each entry * @return the serialised map. */ public static String convertMapToString(Map<String, String> map, String nvSep, String entrySep) { StringBuffer sb = new StringBuffer(); if (map != null) { for (Entry<String, String> entry : map.entrySet()) { String el = entry.getKey(); sb.append(convertEntryToString(el, map.get(el), nvSep)).append(entrySep); } } String repr = sb.toString(); int pos = repr.lastIndexOf(entrySep); return repr.substring(0, pos); }
/** * The convert method translates a String input to a specified outbound level of the same coding * scheme. For example, the input string value may be a tag-encoding URI and the outbound level * specified by string outboundlevel may be BINARY, in which case the return value is a binary * representation expressed as a string. * * @param input the identifier to be converted. * @param inputParameters additional parameters which need to be provided because they cannot * always be determined from the input value alone. Examples include the taglength, * companyprefixlength and filter values. * @param outputLevel the outbound level required for the ouput. Permitted values include BINARY, * TAG_ENCODING, PURE_IDENTITY, LEGACY and ONS_HOSTNAME. * @return the identifier converted to the output level. */ public String convert( String input, Map<String, String> inputParameters, LevelTypeList outputLevel) { TagLengthList tagLength = null; if (inputParameters.containsKey("taglength")) { // in principle, the user should provide a // TagLengthList object in the parameter list. String s = inputParameters.get("taglength"); tagLength = TagLengthList.valueOf(s); } PrefixMatch match = findPrefixMatch(input, tagLength); return convertLevel(match.getScheme(), match.getLevel(), input, inputParameters, outputLevel); }
/** * Returns the value of a specified fieldname from the specified hashmap and returns an integer * value or throws an exception if the value is not an integer */ private int getIntValue(String fieldname, Map<String, String> extraparams) { Matcher checkint = Pattern.compile("^\\d+$").matcher(fieldname); int rv; if (checkint.matches()) { rv = Integer.parseInt(fieldname); } else { if (extraparams.containsKey(fieldname)) { rv = Integer.parseInt(extraparams.get(fieldname)); } else { rv = -1; throw new TDTException( "No integer value for " + fieldname + " can be found - check extraparams"); } } return rv; }
/** pad a value according the field definition. */ private void padField(Map<String, String> extraparams, Field field) { String name = field.getName(); String value = extraparams.get(name); PadDirectionList padDir = field.getPadDir(); int requiredLength = field.getLength(); // assert value != null; if (value == null) return; String padCharString = field.getPadChar(); // if no pad char specified, don't attempt padding if (padCharString == null) return; assert padCharString.length() > 0; char padChar = padCharString.charAt(0); StringBuilder buf = new StringBuilder(requiredLength); if (padDir == PadDirectionList.LEFT) { for (int i = 0; i < requiredLength - value.length(); i++) buf.append(padChar); buf.append(value); } else if (padDir == PadDirectionList.RIGHT) { buf.append(value); for (int i = 0; i < requiredLength - value.length(); i++) buf.append(padChar); } assert buf.length() == requiredLength; if (requiredLength != value.length()) { // System.out.println(" updated " + name + " to '" + buf + "'"); extraparams.put(name, buf.toString()); } /* else { StringBuilder mybuf = new StringBuilder(); for (int i = 0; i < value.length(); i++) { if (i > 0) mybuf.append(','); mybuf.append('\''); mybuf.append(value.charAt(i)); mybuf.append('\''); } System.out.println(" field " + name + " not padded as " + mybuf.toString() + " is already " + requiredLength + " characters long"); } */ }
/** * parses a map from a string. * * @param expStr the string with the serialised map. * @param nvSep the separator for keys and values. * @param entrySep the separator for entries in the map. * @param cleanTags if true the value is cleaned from any present html tag. * @return the parsed map. */ public static Map<String, String> convertStringToMap( final String expStr, final String nvSep, final String entrySep, boolean cleanTags) { String sanitisedExpStr = expStr.trim(); sanitisedExpStr = removeOpenEscape(sanitisedExpStr); sanitisedExpStr = removeCloseEscape(sanitisedExpStr); sanitisedExpStr = sanitisedExpStr.trim(); String[] nvpArray = sanitisedExpStr.split(entrySep); Map<String, String> ret = new HashMap<String, String>(); for (String nvp : nvpArray) { try { nvp = nvp.trim(); if ("".equals(nvp)) { continue; } nvp = removeOpenEscape(nvp).trim(); nvp = removeCloseEscape(nvp).trim(); String[] nvpArr = nvp.split(nvSep); String k, v; k = nvpArr[0].trim(); v = ""; if (nvpArr.length == 2) { v = nvpArr[1].trim(); } else if (nvpArr.length > 2) { int pos = nvp.indexOf(nvSep) + nvSep.length(); v = nvp.substring(pos).trim(); } if (cleanTags) { ret.put(k, fromSimpleTag(v)); } else { ret.put(k, v); } } catch (RuntimeException e) { throw new IllegalArgumentException( "Each entry in the must be separated by '" + entrySep + "' and each entry must be expressed as a name" + nvSep + "value"); } } return ret; }
/** * Returns a string built using a particular grammar. Single-quotes strings are counted as literal * strings, whereas all other strings appearing in the grammar require substitution with the * corresponding value from the extraparams hashmap. */ private String buildGrammar(String grammar, Map<String, String> extraparams) { StringBuilder outboundstring = new StringBuilder(); String[] fields = Pattern.compile("\\s+").split(grammar); for (int i = 0; i < fields.length; i++) { if (fields[i].substring(0, 1).equals("'")) { outboundstring.append(fields[i].substring(1, fields[i].length() - 1)); } else { outboundstring.append(extraparams.get(fields[i])); } } return outboundstring.toString(); }
/** * @param ns * @param xpathExpression * @return the parsed string as {@link XPathExpression} */ public static XPathExpression toExpression(Map<String, String> ns, String xpathExpression) { try { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); if (ns.size() > 0) { xpath.setNamespaceContext(toNsContext(ns)); } XPathExpression expr = xpath.compile(xpathExpression); return expr; } catch (XPathExpressionException e) { throw new IllegalArgumentException( "xPath expression can not be compiled: " + xpathExpression, e); } }
/** * Gets Xpath results * * @param contextNode * @param xpathValue * @return * @throws XPathExpressionException */ private NodeList getXPathResults(Node contextNode, String xpathValue) throws XPathExpressionException { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); // see if the request root is in a namespace String namespace = contextNode.getNamespaceURI(); // name spaces are used, so we need to lookup the correct // prefix to use in the search string NamedNodeMap namedNodeMap = contextNode.getAttributes(); Map<String, String> nsMap = new HashMap<String, String>(); for (int i = 0; i < namedNodeMap.getLength(); i++) { Node n = namedNodeMap.item(i); // we found the matching namespace, so get the prefix // and then break out String prefix = DOMHelper.getLocalName(n); String nodeValue = n.getNodeValue(); nsMap.put(prefix, nodeValue); } // if there is not any namespace is defined for content element, default XACML request // name space would be there. if (XACMLConstants.REQUEST_CONTEXT_3_0_IDENTIFIER.equals(namespace) || XACMLConstants.REQUEST_CONTEXT_2_0_IDENTIFIER.equals(namespace) || XACMLConstants.REQUEST_CONTEXT_1_0_IDENTIFIER.equals(namespace)) { nsMap.put("xacml", namespace); } NamespaceContext namespaceContext = new DefaultNamespaceContext(nsMap); xpath.setNamespaceContext(namespaceContext); XPathExpression expression = xpath.compile(xpathValue); return (NodeList) expression.evaluate(contextNode, XPathConstants.NODESET); }
/** * Converts the value of a specified fieldname from the extraparams map into binary, either * handling it as a large integer or taking into account the compaction of each ASCII byte that is * specified in the TDT definition file for that particular field */ private String fieldToBinary(Field field, Map<String, String> extraparams) { // really need an index to find field number given fieldname; String fieldname = field.getName(); String value = extraparams.get(fieldname); CompactionMethodList compaction = field.getCompaction(); if (compaction == null) { value = dec2bin(value); } else { if (compaction == CompactionMethodList.VALUE_5) { value = uppercasefive2bin(value); } else if (compaction == CompactionMethodList.VALUE_4) { value = alphanumsix2bin(value); } else if (compaction == CompactionMethodList.VALUE_3) { value = asciiseven2bin(value); } else if (compaction == CompactionMethodList.VALUE_2) { value = bytestring2bin(value); } else throw new Error("Unsupported compaction " + compaction); } return value; }
protected Document parse(InputStream inputStream) throws LibraryException { Map<Integer, SongBean> songMap = new HashMap<Integer, SongBean>(); Map<Integer, AlbumBean> albumMap = new HashMap<Integer, AlbumBean>(); Map<Integer, AlbumBean> secondaryAlbumMap = new HashMap<Integer, AlbumBean>(); warningList.clear(); try { // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); factory.setIgnoringElementContentWhitespace(true); factory.setIgnoringComments(true); // Create the builder and parse the file DocumentBuilder builder = factory.newDocumentBuilder(); // Set an error listener and parse the document builder.setErrorHandler(new iTradeTunesLibraryErrorHandler()); builder.setEntityResolver(new iTradeTunesLibraryResolver()); Document document = builder.parse(inputStream); synchronized (libraryList) { // Query the library document and build the library list XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression xPathExpression = xPath.compile(XPATH_LIBRARY_LIST); NodeList nodelist = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET); // Process the elements in the nodelist SongBean song = null; for (int i = 0; i < nodelist.getLength(); i++) { boolean isTrackID = false; // Get element and child nodes Element elem = (Element) nodelist.item(i); NodeList list = elem.getChildNodes(); // Get node value Node childKey = list.item(0); String key = childKey.getNodeValue(); // Check if we have to create a new bean if (SongBean.NAME_TRACK_ID.equals(key)) { isTrackID = true; SongBean previousSong = song; song = new SongBean(); if (previousSong != null && !("AAC audio file".equals(previousSong.getKind()) || "MPEG audio file".equals(previousSong.getKind()))) { songMap.remove(previousSong.getTrack_ID()); } else { // Add an album bean addOrUpdateAlbum(albumMap, previousSong, false); } } // The first parameter is the key String prop = childKey.getNodeValue().replace(' ', '_'); // The second parameter is the value i++; // Get element and child nodes elem = (Element) nodelist.item(i); // Check for boolean properties Object value = null; // Get node value list = elem.getChildNodes(); childKey = list.item(0); value = (childKey == null) ? elem.getNodeName() : childKey.getNodeValue(); if (isTrackID) { isTrackID = false; } // Set the property of the song bean Statement stmt = new Statement(song, "set" + prop, new Object[] {value}); try { stmt.execute(); } catch (Exception e) { // Ignore that field, we do not have it in our bean } // If the property is the track ID, add the song to the hash // map if (SongBean.NAME_TRACK_ID.equals(key)) { int trackID = Integer.valueOf((String) value); songMap.put(trackID, song); } } // Update album for last song addOrUpdateAlbum(albumMap, song, false); // Check the album map for inconsistencies Iterator<AlbumBean> albums = albumMap.values().iterator(); while (albums.hasNext()) { AlbumBean album = albums.next(); if (album.checkConsistency()) { libraryList.add(album); album.setHashCode(); } else { // Add an inconsistent album only using the album title SongBean[] songs = album.getSongs(); for (int i = 0; i < songs.length; i++) { addOrUpdateAlbum(secondaryAlbumMap, songs[i], true); } } } // Check secondary album map for consistency albums = secondaryAlbumMap.values().iterator(); while (albums.hasNext()) { AlbumBean album = albums.next(); if (album.checkConsistency()) { libraryList.add(album); album.setHashCode(); } else { // This album cannot be matched // TODO: Add to warning message } } setChanged(); } return document; } catch (IOException ioe) { // Log an expected connect exception throw new LibraryException(ioe); } catch (SAXException se) { // Catch all other exceptions throw new LibraryException(se); } catch (ParserConfigurationException pce) { // Catch all other exceptions Utils.logSevere(pce); throw new LibraryException(pce); } catch (XPathExpressionException xpe) { // Catch all other exceptions Utils.logSevere(xpe); throw new LibraryException(xpe); } catch (NumberFormatException nfe) { // Catch all other exceptions throw new LibraryException(nfe); } }
/** * Set the preconfigured event properties resolved by XPath expression. * * @param explicitXPathProperties are preconfigured event properties * @param additionalSchemaProperties the explicit properties */ protected void initialize( Collection<ConfigurationEventTypeXMLDOM.XPathPropertyDesc> explicitXPathProperties, List<ExplicitPropertyDescriptor> additionalSchemaProperties) { // make sure we override those explicitly provided with those derived from a metadataz Map<String, ExplicitPropertyDescriptor> namedProperties = new LinkedHashMap<String, ExplicitPropertyDescriptor>(); for (ExplicitPropertyDescriptor desc : additionalSchemaProperties) { namedProperties.put(desc.getDescriptor().getPropertyName(), desc); } String xpathExpression = null; try { for (ConfigurationEventTypeXMLDOM.XPathPropertyDesc property : explicitXPathProperties) { XPath xPath = xPathFactory.newXPath(); if (namespaceContext != null) { xPath.setNamespaceContext(namespaceContext); } xpathExpression = property.getXpath(); if (log.isInfoEnabled()) { log.info( "Compiling XPath expression for property '" + property.getName() + "' as '" + xpathExpression + "'"); } XPathExpression expression = xPath.compile(xpathExpression); FragmentFactoryXPathPredefinedGetter fragmentFactory = null; boolean isFragment = false; if (property.getOptionaleventTypeName() != null) { fragmentFactory = new FragmentFactoryXPathPredefinedGetter( this.getEventAdapterService(), property.getOptionaleventTypeName(), property.getName()); isFragment = true; } boolean isArray = false; if (property.getType().equals(XPathConstants.NODESET)) { isArray = true; } EventPropertyGetter getter = new XPathPropertyGetter( property.getName(), xpathExpression, expression, property.getType(), property.getOptionalCastToType(), fragmentFactory); Class returnType = SchemaUtil.toReturnType(property.getType(), property.getOptionalCastToType()); EventPropertyDescriptor desc = new EventPropertyDescriptor( property.getName(), returnType, null, false, false, isArray, false, isFragment); ExplicitPropertyDescriptor explicit = new ExplicitPropertyDescriptor( desc, getter, isArray, property.getOptionaleventTypeName()); namedProperties.put(desc.getPropertyName(), explicit); } } catch (XPathExpressionException ex) { throw new EPException( "XPath expression could not be compiled for expression '" + xpathExpression + '\'', ex); } super.initialize(new ArrayList<ExplicitPropertyDescriptor>(namedProperties.values())); // evaluate start and end timestamp properties if any startTimestampPropertyName = configurationEventTypeXMLDOM.getStartTimestampPropertyName(); endTimestampPropertyName = configurationEventTypeXMLDOM.getEndTimestampPropertyName(); EventTypeUtility.validateTimestampProperties( this, startTimestampPropertyName, endTimestampPropertyName); }
/** convert from a particular scheme / level */ private String convertLevel( Scheme tdtscheme, Level tdtlevel, String input, Map<String, String> inputParameters, LevelTypeList outboundlevel) { String outboundstring; Map<String, String> extraparams = // new NoisyMap (new HashMap<String, String>(inputParameters)); // get the scheme's option key, which is the name of a // parameter whose value is matched to the option key of the // level. String optionkey = tdtscheme.getOptionKey(); String optionValue = extraparams.get(optionkey); // the name of a parameter which allows the appropriate option // to be selected // now consider the various options within the scheme and // level for each option element inside the level, check // whether the pattern attribute matches as a regular // expression String matchingOptionKey = null; Option matchingOption = null; Matcher prefixMatcher = null; for (Enumeration e = tdtlevel.enumerateOption(); e.hasMoreElements(); ) { Option opt = (Option) e.nextElement(); if (optionValue == null || optionValue.equals(opt.getOptionKey())) { // possible match Matcher matcher = Pattern.compile(opt.getPattern()).matcher(input); if (matcher.matches()) { if (prefixMatcher != null) throw new TDTException("Multiple patterns matched"); prefixMatcher = matcher; matchingOptionKey = opt.getOptionKey(); matchingOption = opt; } } } if (prefixMatcher == null) throw new TDTException("No patterns matched"); optionValue = matchingOptionKey; for (Enumeration e = matchingOption.enumerateField(); e.hasMoreElements(); ) { Field field = (Field) e.nextElement(); int seq = field.getSeq(); String strfieldname = field.getName(); int fieldlength = field.getLength(); String strfieldvalue = prefixMatcher.group(seq); // System.out.println(" processing field " + strfieldname + " = '" + strfieldvalue + "'"); if (field.getCompaction() == null) { // if compaction is null, treat field as an integer if (field.getCharacterSet() != null) { // if the character set is specified Matcher charsetmatcher = Pattern.compile("^" + field.getCharacterSet() + "$").matcher(strfieldvalue); if (!charsetmatcher.matches()) { throw new TDTException( "field " + strfieldname + " (" + strfieldvalue + ") does not conform to the allowed character set (" + field.getCharacterSet() + ") "); } } BigInteger bigvalue = null; if (tdtlevel.getType() == LevelTypeList.BINARY) { // if the input was BINARY bigvalue = new BigInteger(strfieldvalue, 2); extraparams.put(strfieldname, bigvalue.toString()); } else { if (field.getDecimalMinimum() != null || field.getDecimalMaximum() != null) bigvalue = new BigInteger(strfieldvalue); extraparams.put(strfieldname, strfieldvalue); } if (field.getDecimalMinimum() != null) { // if the decimal minimum is specified BigInteger bigmin = new BigInteger(field.getDecimalMinimum()); if (bigvalue.compareTo(bigmin) == -1) { // throw an exception if the field value is less than the decimal minimum throw new TDTException( "field " + strfieldname + " (" + bigvalue + ") is less than DecimalMinimum (" + field.getDecimalMinimum() + ") allowed"); } } if (field.getDecimalMaximum() != null) { // if the decimal maximum is specified BigInteger bigmax = new BigInteger(field.getDecimalMaximum()); if (bigvalue.compareTo(bigmax) == 1) { // throw an excpetion if the field value is greater than the decimal maximum throw new TDTException( "field " + strfieldname + " (" + bigvalue + ") is greater than DecimalMaximum (" + field.getDecimalMaximum() + ") allowed"); } } // after extracting the field, it may be necessary to pad it. padField(extraparams, field); } else { // compaction is specified - interpret binary as a string value using a truncated byte per // character CompactionMethodList compaction = field.getCompaction(); PadDirectionList padDir = field.getPadDir(); String padchar = field.getPadChar(); String s; if (compaction == CompactionMethodList.VALUE_5) // "5-bit" s = bin2uppercasefive(strfieldvalue); else if (compaction == CompactionMethodList.VALUE_4) // 6-bit s = bin2alphanumsix(strfieldvalue); else if (compaction == CompactionMethodList.VALUE_3) // 7-bit s = bin2asciiseven(strfieldvalue); else if (compaction == CompactionMethodList.VALUE_2) // 8-bit s = bin2bytestring(strfieldvalue); else throw new Error("unsupported compaction method " + compaction); extraparams.put(strfieldname, stripPadChar(s, padDir, padchar)); } } // for each field; /** * the EXTRACT rules are performed after parsing the input, in order to determine additional * fields that are to be derived from the fields obtained by the pattern match process */ int seq = 0; for (Enumeration e = tdtlevel.enumerateRule(); e.hasMoreElements(); ) { Rule tdtrule = (Rule) e.nextElement(); if (tdtrule.getType() == ModeList.EXTRACT) { assert seq < tdtrule.getSeq() : "Rule out of sequence order"; seq = tdtrule.getSeq(); processRules(extraparams, tdtrule); } } /** * Now we need to consider the corresponding output level and output option. The scheme must * remain the same, as must the value of optionkey (to select the corresponding option element * nested within the required outbound level) */ Level tdtoutlevel = findLevel(tdtscheme, outboundlevel); Option tdtoutoption = findOption(tdtoutlevel, optionValue); /** * the FORMAT rules are performed before formatting the output, in order to determine additional * fields that are required for preparation of the outbound format */ seq = 0; for (Enumeration e = tdtoutlevel.enumerateRule(); e.hasMoreElements(); ) { Rule tdtrule = (Rule) e.nextElement(); if (tdtrule.getType() == ModeList.FORMAT) { assert seq < tdtrule.getSeq() : "Rule out of sequence order"; seq = tdtrule.getSeq(); processRules(extraparams, tdtrule); } } /** * Now we need to ensure that all fields required for the outbound grammar are suitably padded * etc. processPadding takes care of firstly padding the non-binary fields if padChar and * padDir, length are specified then (if necessary) converting to binary and padding the binary * representation to the left with zeros if the bit string is has fewer bits than the bitLength * attribute specifies. N.B. TDTv1.1 will be more specific about bit-level padding rather than * assuming that it is always to the left with the zero bit. */ // System.out.println(" prior to processPadding, " + extraparams); for (Enumeration e = tdtoutoption.enumerateField(); e.hasMoreElements(); ) { Field field = (Field) e.nextElement(); // processPadding(extraparams, field, outboundlevel, tdtoutoption); padField(extraparams, field); if (outboundlevel == LevelTypeList.BINARY) binaryPadding(extraparams, field); } /** * Construct the output from the specified grammar (in ABNF format) together with the field * values stored in inputparams */ outboundstring = buildGrammar(tdtoutoption.getGrammar(), extraparams); // System.out.println("final extraparams = " + extraparams); // System.out.println("returned " + outboundstring); return outboundstring; }
public static AUTH_TYPE getByConfigName(String configName) { return LOOKUP.get(configName); }
private void processTxt(Node operation) { List<Node> targets = getChildNodes(operation, "target"); List<Node> optionNodes = getChildNodes(operation, "opt"); List<Node> separatorNode = getChildNodes(operation, "separator"); if (targets.isEmpty() || optionNodes.isEmpty()) { return; } String defaultSeparator = "="; String globalSeparator = defaultSeparator; if (!separatorNode.isEmpty()) { globalSeparator = separatorNode.get(0).getTextContent(); if (globalSeparator.length() != 1) { globalSeparator = defaultSeparator; } } Map<String, String> options = new HashMap<String, String>(); Map<String, String> processedOptions = new HashMap<String, String>(); for (int i = 0; i < optionNodes.size(); i++) { Node option = optionNodes.get(i); String name = option.getAttributes().getNamedItem("name").getNodeValue(); String value = option.getTextContent(); if (options.containsKey(name)) { options.remove(name); } options.put(name, value); } for (int t = 0; t < targets.size(); t++) { File target = new File(absolutePath(targets.get(t).getTextContent())); File tmpFile = new File(Utils.timestamp()); BufferedWriter bw = null; BufferedReader br = null; try { Node separatorAttr = targets.get(t).getAttributes().getNamedItem("separator"); String separator = (separatorAttr == null) ? globalSeparator : separatorAttr.getNodeValue(); if (separator.length() != 1) { separator = globalSeparator; } bw = new BufferedWriter(new FileWriter(tmpFile)); if (target.exists()) { br = new BufferedReader(new FileReader(target)); for (String line; (line = br.readLine()) != null; ) { String[] parts = line.split(separator); if (parts.length < 2) { bw.write(line); bw.newLine(); continue; } String optName = parts[0].trim(); if (options.containsKey(optName)) { String optValue = options.get(optName); bw.write(optName + " " + separator + " " + optValue); bw.newLine(); processedOptions.put(optName, optValue); options.remove(optName); } else if (processedOptions.containsKey(optName)) { bw.write(optName + " " + separator + " " + processedOptions.get(optName)); bw.newLine(); } else { bw.write(line); bw.newLine(); } } br.close(); } for (Map.Entry<String, String> entry : options.entrySet()) { bw.write(entry.getKey() + " " + separator + " " + entry.getValue()); bw.newLine(); } bw.close(); FileUtils.copyFile(tmpFile, target); FileUtils.forceDelete(tmpFile); } catch (IOException ex) { Utils.onError(new Error.WriteTxtConfig(target.getPath())); } } }
static { for (AUTH_TYPE type : EnumSet.allOf(AUTH_TYPE.class)) LOOKUP.put(type.configName, type); }
/** * Adds additional entries to the extraparams hashmap by processing various rules defined in the * TDT definition files. Typically used for string processing functions, lookup in tables, * calculation of check digits etc. */ private void processRules(Map<String, String> extraparams, Rule tdtrule) { String tdtfunction = tdtrule.getFunction(); int openbracket = tdtfunction.indexOf("("); assert openbracket != -1; String params = tdtfunction.substring(openbracket + 1, tdtfunction.length() - 1); String rulename = tdtfunction.substring(0, openbracket); String[] parameter = params.split(","); String newfieldname = tdtrule.getNewFieldName(); // System.out.println(tdtfunction + " " + parameter[0] + " " + extraparams.get(parameter[0])); /** * Stores in the hashmap extraparams the value obtained from a lookup in a specified XML table. * * <p>The first parameter is the given value already known. This is denoted as $1 in the * corresponding XPath expression * * <p>The second parameter is the string filename of the table which must be present in the * auxiliary subdirectory * * <p>The third parameter is the column in which the supplied input value should be sought * * <p>The fourth parameter is the column whose value should be read for the corresponding row, * in order to obtain the result of the lookup. * * <p>The rule in the definition file may contain an XPath expression and a URL where the table * may be obtained. */ if (rulename.equals("TABLELOOKUP")) { // parameter[0] is given value // parameter[1] is table // parameter[2] is input column supplied // parameter[3] is output column required assert parameter.length == 4 : "incorrect number of parameters to tablelookup " + params; if (parameter[1].equals("tdt64bitcpi")) { String s = extraparams.get(parameter[0]); assert s != null : tdtfunction + " when " + parameter[0] + " is null"; String t = gs1cpi.get(s); assert t != null : "gs1cpi[" + s + "] is null"; assert newfieldname != null; extraparams.put(newfieldname, t); // extraparams.put(newfieldname, gs1cpi.get(extraparams.get(parameter[0]))); } else { // JPB! the following is untested String tdtxpath = tdtrule.getTableXPath(); String tdttableurl = tdtrule.getTableURL(); String tdtxpathsub = tdtxpath.replaceAll("\\$1", extraparams.get(parameter[0])); extraparams.put(newfieldname, xpathlookup("ManagerTranslation.xml", tdtxpathsub)); } } /** * Stores the length of the specified string under the new fieldname specified by the * corresponding rule of the definition file. */ if (rulename.equals("LENGTH")) { assert extraparams.get(parameter[0]) != null : tdtfunction + " when " + parameter[0] + " is null"; if (extraparams.get(parameter[0]) != null) { extraparams.put(newfieldname, Integer.toString(extraparams.get(parameter[0]).length())); } } /** * Stores a GS1 check digit in the extraparams hashmap, keyed under the new fieldname specified * by the corresponding rule of the definition file. */ if (rulename.equals("GS1CHECKSUM")) { assert extraparams.get(parameter[0]) != null : tdtfunction + " when " + parameter[0] + " is null"; if (extraparams.get(parameter[0]) != null) { extraparams.put(newfieldname, gs1checksum(extraparams.get(parameter[0]))); } } /** * Obtains a substring of the string provided as the first parameter. If only a single second * parameter is specified, then this is considered as the start index and all characters from * the start index onwards are stored in the extraparams hashmap under the key named * 'newfieldname' in the corresponding rule of the definition file. If a second and third * parameter are specified, then the second parameter is the start index and the third is the * length of characters required. A substring consisting characters from the start index up to * the required length of characters is stored in the extraparams hashmap, keyed under the new * fieldname specified by the corresponding rule of the defintion file. */ if (rulename.equals("SUBSTR")) { assert extraparams.get(parameter[0]) != null : tdtfunction + " when " + parameter[0] + " is null"; if (parameter.length == 2) { if (extraparams.get(parameter[0]) != null) { int start = getIntValue(parameter[1], extraparams); if (start >= 0) { extraparams.put(newfieldname, extraparams.get(parameter[0]).substring(start)); } } } if (parameter.length == 3) { // need to check that this variation is correct - c.f. Perl substr assert extraparams.get(parameter[0]) != null : tdtfunction + " when " + parameter[0] + " is null"; if (extraparams.get(parameter[0]) != null) { int start = getIntValue(parameter[1], extraparams); int end = getIntValue(parameter[2], extraparams); if ((start >= 0) && (end >= 0)) { extraparams.put( newfieldname, extraparams.get(parameter[0]).substring(start, start + end)); } } } } /** * Concatenates specified string parameters together. Literal values must be enclosed within * single or double quotes or consist of unquoted digits. Other unquoted strings are considered * as fieldnames and the corresponding value from the extraparams hashmap are inserted. The * result of the concatenation (and substitution) of the strings is stored as a new entry in the * extraparams hashmap, keyed under the new fieldname specified by the rule. */ if (rulename.equals("CONCAT")) { StringBuilder buffer = new StringBuilder(); for (int p1 = 0; p1 < parameter.length; p1++) { Matcher matcher = Pattern.compile("\"(.*?)\"|'(.*?)'|[0-9]").matcher(parameter[p1]); if (matcher.matches()) { buffer.append(parameter[p1]); } else { assert extraparams.get(parameter[p1]) != null : tdtfunction + " when " + parameter[p1] + " is null"; if (extraparams.get(parameter[p1]) != null) { buffer.append(extraparams.get(parameter[p1])); } } } extraparams.put(newfieldname, buffer.toString()); } }
public void parseStartup(File file) { Logger.getLogger(com.bombdiggity.amazon.ec2.install.Installer.class) .info((new StringBuilder("InstallParser.parseStartup: ")).append(file).toString()); try { Document doc = loadFile(file); if (doc != null) { XPathFactory factory = XMLUtils.newXPathFactory(); XPath xpath = factory.newXPath(); String rootXPath = "/Startup/Commands/*"; Element root = doc.getDocumentElement(); XPathExpression rootExp = xpath.compile(rootXPath); NodeList streamList = (NodeList) rootExp.evaluate(root, XPathConstants.NODESET); if (streamList != null) { for (int i = 0; i < streamList.getLength(); i++) { Node streamNode = streamList.item(i); Element streamElem = (Element) streamNode; if (streamElem.getNodeName().toLowerCase().equals("install")) { String packageName = null; String folderPath = null; for (Node child = streamNode.getFirstChild(); child != null; child = child.getNextSibling()) if (child.getNodeName().toLowerCase().equals("package")) packageName = XMLUtils.getNodeValue(child).trim(); else if (child.getNodeName().toLowerCase().equals("folder")) folderPath = XMLUtils.getNodeValue(child).trim(); if (packageName != null) InstallCommands.installPackage(session, packageName); else if (folderPath != null) InstallCommands.installFolder(session, folderPath); else Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class) .error("StartupParser.loadFile: <Install>: <Package> or <Folder> required"); } else if (streamElem.getNodeName().toLowerCase().equals("download")) { String url = null; String method = "get"; String data = null; String destination = "/opt"; String action = null; List headers = new ArrayList(); for (Node child = streamNode.getFirstChild(); child != null; child = child.getNextSibling()) if (child.getNodeName().toLowerCase().equals("url")) url = XMLUtils.getNodeValue(child).trim(); else if (child.getNodeName().toLowerCase().equals("method")) method = XMLUtils.getNodeValue(child).toLowerCase().trim(); else if (child.getNodeName().toLowerCase().equals("data")) data = XMLUtils.getNodeValue(child).trim(); else if (child.getNodeName().toLowerCase().equals("destination")) destination = XMLUtils.getNodeValue(child).trim(); else if (child.getNodeName().toLowerCase().equals("action")) action = XMLUtils.getNodeValue(child).toLowerCase().trim(); else if (child.getNodeName().toLowerCase().equals("header")) { Node nameNode = XMLUtils.getNodeByTagName((Element) child, "Name"); Node valueNode = XMLUtils.getNodeByTagName((Element) child, "Value"); if (nameNode != null && valueNode != null) { Map namePair = new HashMap(); namePair.put( XMLUtils.getNodeValue(nameNode).trim(), XMLUtils.getNodeValue(valueNode).trim()); headers.add(namePair); } else { Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class) .error( "StartupParser.loadFile: <Download/Header>: <Name> and <Value> required"); } } if (url != null && destination != null) InstallCommands.downloadFile( session, url, method, data, headers, destination, action); else Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class) .error("StartupParser.loadFile: <Download>: <URL> and <Destination> required"); } else if (streamElem.getNodeName().toLowerCase().equals("s3fetch")) { String awsAccessKeyId = null; String awsSecretAccessKey = null; String bucket = null; String key = null; String destination = null; String action = null; for (Node child = streamNode.getFirstChild(); child != null; child = child.getNextSibling()) if (child.getNodeName().toLowerCase().equals("awsaccesskeyid")) awsAccessKeyId = XMLUtils.getNodeValue(child).trim(); else if (child.getNodeName().toLowerCase().equals("awssecretaccesskey")) awsSecretAccessKey = XMLUtils.getNodeValue(child).trim(); else if (child.getNodeName().toLowerCase().equals("bucket")) bucket = XMLUtils.getNodeValue(child).trim(); else if (child.getNodeName().toLowerCase().equals("key")) key = XMLUtils.getNodeValue(child).trim(); else if (child.getNodeName().toLowerCase().equals("destination")) destination = XMLUtils.getNodeValue(child).trim(); else if (child.getNodeName().toLowerCase().equals("action")) action = XMLUtils.getNodeValue(child).toLowerCase().trim(); if (awsAccessKeyId != null && awsSecretAccessKey != null && bucket != null && key != null && destination != null) InstallCommands.fetchFile( session, awsAccessKeyId, awsSecretAccessKey, bucket, key, destination, action); else Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class) .error( "StartupParser.loadFile: <Fetch>: <AWSAccessKeyId>, <AWSSecretAccessKey>, <Bucket>, <Key> and <Destination> required"); } else if (streamElem.getNodeName().toLowerCase().equals("runscript")) { String script = null; List params = new ArrayList(); for (Node child = streamNode.getFirstChild(); child != null; child = child.getNextSibling()) if (child.getNodeName().toLowerCase().equals("script")) script = XMLUtils.getNodeValue(child).trim(); else if (child.getNodeName().toLowerCase().equals("param")) { String param = XMLUtils.getNodeValue(child).trim(); params.add(param); } if (script != null) InstallCommands.runScript(session, script, params); else Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class) .error("StartupParser.loadFile: <RunScript>: <Script> required"); } } } } } catch (Exception e) { Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class) .error( (new StringBuilder("InstallParser.parseStartup: ")).append(e.toString()).toString()); e.printStackTrace(); } }
public static void testNaming() throws Exception { Map<String, Object> map = Create.map(); map.put("_secret", "_secret"); map.put("_secret", "_secret"); map.put(".secret", ".secret"); map.put("$new", "$new"); map.put("new", "new"); map.put("secret", "secret"); map.put("a_b_c", "a_b_c"); map.put("a.b.c", "a.b.c"); map.put(".a_b", ".a_b"); map.put("$$$$a_b", "$$$$a_b"); map.put("$$$$a.b", "$$$$a.b"); map.put("a", "a"); map.put("a$", "a$"); map.put("a$$", "a$$"); map.put("a$.$", "a$.$"); map.put("a$_$", "a$_$"); map.put("a..", "a.."); map.put("noid", "noid"); map.put("nullid", "nullid"); Naming trt = Configurable.createConfigurable(Naming.class, map); // By name assertEquals("secret", trt.secret()); assertEquals("_secret", trt.__secret()); assertEquals(".secret", trt._secret()); assertEquals("new", trt.$new()); assertEquals("$new", trt.$$new()); assertEquals("a.b.c", trt.a_b_c()); assertEquals("a_b_c", trt.a__b__c()); assertEquals(".a_b", trt._a__b()); assertEquals("$$$$a.b", trt.$$$$$$$$a_b()); assertEquals("$$$$a_b", trt.$$$$$$$$a__b()); assertEquals("a", trt.a$()); assertEquals("a$", trt.a$$()); assertEquals("a$", trt.a$$$()); assertEquals("a$.$", trt.a$$_$$()); assertEquals("a$_$", trt.a$$__$$()); assertEquals("a..", trt.a_$_()); assertEquals("noid", trt.noid()); assertEquals("nullid", trt.nullid()); // By AD assertEquals("secret", trt.xsecret()); assertEquals("_secret", trt.x__secret()); assertEquals(".secret", trt.x_secret()); assertEquals("new", trt.x$new()); assertEquals("$new", trt.x$$new()); assertEquals("a.b.c", trt.xa_b_c()); assertEquals("a_b_c", trt.xa__b__c()); assertEquals(".a_b", trt.x_a__b()); assertEquals("$$$$a.b", trt.x$$$$$$$$a_b()); assertEquals("$$$$a_b", trt.x$$$$$$$$a__b()); assertEquals("a", trt.xa$()); assertEquals("a$", trt.xa$$()); assertEquals("a$", trt.xa$$$()); assertEquals("a$.$", trt.xa$$_$$()); Builder b = new Builder(); b.addClasspath(new File("bin")); b.setProperty("Export-Package", "test.metatype"); b.setProperty("-metatype", "*"); b.build(); assertEquals(0, b.getErrors().size()); assertEquals(0, b.getWarnings().size()); Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.MetatypeTest$Naming.xml"); IO.copy(r.openInputStream(), System.err); Document d = db.parse(r.openInputStream(), "UTF-8"); assertEquals( "http://www.osgi.org/xmlns/metatype/v1.1.0", d.getDocumentElement().getNamespaceURI()); }