public virtual void TestFileDistributionCalculator() { ByteArrayOutputStream output = new ByteArrayOutputStream(); TextWriter o = new TextWriter(output); new FileDistributionCalculator(new Configuration(), 0, 0, o).Visit(new RandomAccessFile (originalFsimage, "r")); o.Close(); string outputString = output.ToString(); Sharpen.Pattern p = Sharpen.Pattern.Compile("totalFiles = (\\d+)\n"); Matcher matcher = p.Matcher(outputString); NUnit.Framework.Assert.IsTrue(matcher.Find() && matcher.GroupCount() == 1); int totalFiles = System.Convert.ToInt32(matcher.Group(1)); NUnit.Framework.Assert.AreEqual(NumDirs * FilesPerDir, totalFiles); p = Sharpen.Pattern.Compile("totalDirectories = (\\d+)\n"); matcher = p.Matcher(outputString); NUnit.Framework.Assert.IsTrue(matcher.Find() && matcher.GroupCount() == 1); int totalDirs = System.Convert.ToInt32(matcher.Group(1)); // totalDirs includes root directory, empty directory, and xattr directory NUnit.Framework.Assert.AreEqual(NumDirs + 4, totalDirs); FileStatus maxFile = Sharpen.Collections.Max(writtenFiles.Values, new _IComparer_238 ()); p = Sharpen.Pattern.Compile("maxFileSize = (\\d+)\n"); matcher = p.Matcher(output.ToString("UTF-8")); NUnit.Framework.Assert.IsTrue(matcher.Find() && matcher.GroupCount() == 1); NUnit.Framework.Assert.AreEqual(maxFile.GetLen(), long.Parse(matcher.Group(1))); }
public virtual bool IsTrashAllowed(FilePath blockFile) { Matcher matcher = BlockPoolCurrentPathPattern.Matcher(blockFile.GetParent()); string previousDir = matcher.ReplaceFirst("$1$2" + StorageDirPrevious); return(!(new FilePath(previousDir)).Exists()); }
/// <summary> /// Read the md5 file stored alongside the given data file /// and match the md5 file content. /// </summary> /// <param name="dataFile">the file containing data</param> /// <returns> /// a matcher with two matched groups /// where group(1) is the md5 string and group(2) is the data file path. /// </returns> /// <exception cref="System.IO.IOException"/> private static Matcher ReadStoredMd5(FilePath md5File) { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream (md5File), Charsets.Utf8)); string md5Line; try { md5Line = reader.ReadLine(); if (md5Line == null) { md5Line = string.Empty; } md5Line = md5Line.Trim(); } catch (IOException ioe) { throw new IOException("Error reading md5 file at " + md5File, ioe); } finally { IOUtils.Cleanup(Log, reader); } Matcher matcher = LineRegex.Matcher(md5Line); if (!matcher.Matches()) { throw new IOException("Invalid MD5 file " + md5File + ": the content \"" + md5Line + "\" does not match the expected pattern."); } return(matcher); }
/// <exception cref="Sharpen.SAXException"></exception> public bool Start(NBoilerpipeContentHandler instance, string localName, HtmlAttributeCollection atts) { IList <string> labels = new AList <string>(5); labels.AddItem(DefaultLabels.MARKUP_PREFIX + localName); string classVal = atts["class"].Value; if (classVal != null && classVal.Length > 0) { classVal = PAT_NUM.Matcher(classVal).ReplaceAll("#"); classVal = classVal.Trim(); string[] vals = classVal.Split("[ ]+"); labels.AddItem(DefaultLabels.MARKUP_PREFIX + "." + classVal.Replace(' ', '.')); if (vals.Length > 1) { foreach (string s in vals) { labels.AddItem(DefaultLabels.MARKUP_PREFIX + "." + s); } } } var att = atts["id"]; var id = (atts != null) ? att.Name : ""; if (id != null && id.Length > 0) { id = PAT_NUM.Matcher(id).ReplaceAll("#"); labels.AddItem(DefaultLabels.MARKUP_PREFIX + "#" + id); } ICollection <string> ancestors = GetAncestorLabels(); IList <string> labelsWithAncestors = new AList <string>((ancestors.Count + 1) * labels .Count); foreach (string l in labels) { foreach (string an in ancestors) { labelsWithAncestors.AddItem(an); labelsWithAncestors.AddItem(an + " " + l); } labelsWithAncestors.AddItem(l); } instance.AddLabelAction(new LabelAction(Sharpen.Collections.ToArray(labelsWithAncestors , new string[labelsWithAncestors.Count]))); labelStack.AddItem(labels); return(isBlockLevel); }
private static bool ContainsOnlyLegalCharacters(string databaseName) { Sharpen.Pattern p = Sharpen.Pattern.Compile("^[abcdefghijklmnopqrstuvwxyz0123456789_$()+-/]+$" ); Matcher matcher = p.Matcher(databaseName); return(matcher.Matches()); }
private bool ValidateAuxServiceName(string name) { if (name == null || name.Trim().IsEmpty()) { return(false); } return(p.Matcher(name).Matches()); }
internal virtual string GetRestoreDirectory(FilePath blockFile) { Matcher matcher = BlockPoolTrashPathPattern.Matcher(blockFile.GetParent()); string restoreDirectory = matcher.ReplaceFirst("$1$2" + StorageDirCurrent + "$4"); Log.Info("Restoring " + blockFile + " to " + restoreDirectory); return(restoreDirectory); }
/// <summary>Associates an alias name with an actual name.</summary> /// <remarks> /// Associates an alias name with an actual name. /// <p> /// Define a alias mapping from one namespace/property to another. Both /// property names must be simple names. An alias can be a direct mapping, /// where the alias and actual have the same data type. It is also possible /// to map a simple alias to an item in an array. This can either be to the /// first item in the array, or to the 'x-default' item in an alt-text array. /// Multiple alias names may map to the same actual, as long as the forms /// match. It is a no-op to reregister an alias in an identical fashion. /// Note: This method is not locking because only called by registerStandardAliases /// which is only called by the constructor. /// Note2: The method is only package-private so that it can be tested with unittests /// </remarks> /// <param name="aliasNS"> /// The namespace URI for the alias. Must not be null or the empty /// string. /// </param> /// <param name="aliasProp"> /// The name of the alias. Must be a simple name, not null or the /// empty string and not a general path expression. /// </param> /// <param name="actualNS"> /// The namespace URI for the actual. Must not be null or the /// empty string. /// </param> /// <param name="actualProp"> /// The name of the actual. Must be a simple name, not null or the /// empty string and not a general path expression. /// </param> /// <param name="aliasForm"> /// Provides options for aliases for simple aliases to array /// items. This is needed to know what kind of array to create if /// set for the first time via the simple alias. Pass /// <code>XMP_NoOptions</code>, the default value, for all /// direct aliases regardless of whether the actual data type is /// an array or not (see /// <see cref="Com.Adobe.Xmp.Options.AliasOptions"/> /// ). /// </param> /// <exception cref="Com.Adobe.Xmp.XMPException">for inconsistant aliases.</exception> internal void RegisterAlias(string aliasNS, string aliasProp, string actualNS, string actualProp, AliasOptions aliasForm) { lock (this) { ParameterAsserts.AssertSchemaNS(aliasNS); ParameterAsserts.AssertPropName(aliasProp); ParameterAsserts.AssertSchemaNS(actualNS); ParameterAsserts.AssertPropName(actualProp); // Fix the alias options AliasOptions aliasOpts = aliasForm != null ? new AliasOptions(XMPNodeUtils.VerifySetOptions(aliasForm.ToPropertyOptions(), null).GetOptions()) : new AliasOptions(); if (p.Matcher(aliasProp).Find() || p.Matcher(actualProp).Find()) { throw new XMPException("Alias and actual property names must be simple", XMPErrorConstants.Badxpath); } // check if both namespaces are registered string aliasPrefix = GetNamespacePrefix(aliasNS); string actualPrefix = GetNamespacePrefix(actualNS); if (aliasPrefix == null) { throw new XMPException("Alias namespace is not registered", XMPErrorConstants.Badschema); } else { if (actualPrefix == null) { throw new XMPException("Actual namespace is not registered", XMPErrorConstants.Badschema); } } string key = aliasPrefix + aliasProp; // check if alias is already existing if (aliasMap.ContainsKey(key)) { throw new XMPException("Alias is already existing", XMPErrorConstants.Badparam); } else { if (aliasMap.ContainsKey(actualPrefix + actualProp)) { throw new XMPException("Actual property is already an alias, use the base property", XMPErrorConstants.Badparam); } } XMPAliasInfo aliasInfo = new _XMPAliasInfo_390(actualNS, actualPrefix, actualProp, aliasOpts); aliasMap.Put(key, aliasInfo); } }
internal virtual void VerifyUsernamePattern(string user) { if (!UserGroupInformation.IsSecurityEnabled() && !nonsecureLocalUserPattern.Matcher (user).Matches()) { throw new ArgumentException("Invalid user name '" + user + "'," + " it must match '" + nonsecureLocalUserPattern.Pattern() + "'"); } }
private bool IsBlockFileInPrevious(FilePath blockFile) { Sharpen.Pattern blockFilePattern = Sharpen.Pattern.Compile(string.Format("^(.*%1$scurrent%1$s.*%1$s)(current)(%1$s.*)$" , Sharpen.Pattern.Quote(FilePath.separator))); Matcher matcher = blockFilePattern.Matcher(blockFile.ToString()); string previousFileName = matcher.ReplaceFirst("$1" + "previous" + "$3"); return((new FilePath(previousFileName)).Exists()); }
/// <exception cref="System.IO.IOException"/> /// <exception cref="System.Exception"/> protected internal override void Map(K key, Text value, Mapper.Context context) { string text = value.ToString(); Matcher matcher = pattern.Matcher(text); while (matcher.Find()) { context.Write(new Text(matcher.Group(group)), new LongWritable(1)); } }
/// <summary>gets the data node storage directory based on block pool storage</summary> private static string GetDataNodeStorageRoot(string bpRoot) { Matcher matcher = BlockPoolPathPattern.Matcher(bpRoot); if (matcher.Matches()) { // return the data node root directory return(matcher.Group(1)); } return(bpRoot); }
/// <exception cref="System.Exception"/> protected internal override string Parse(string str) { if (pattern != null) { if (!pattern.Matcher(str).Matches()) { throw new ArgumentException("Invalid value"); } } return(str); }
/// <exception cref="Org.Apache.Hadoop.Yarn.Server.Resourcemanager.Scheduler.Fair.AllocationConfigurationException /// "/> private static int FindResource(string val, string units) { Sharpen.Pattern pattern = Sharpen.Pattern.Compile("(\\d+)\\s*" + units); Matcher matcher = pattern.Matcher(val); if (!matcher.Find()) { throw new AllocationConfigurationException("Missing resource: " + units); } return(System.Convert.ToInt32(matcher.Group(1))); }
/// <exception cref="System.IO.IOException"/> public virtual void Map(K key, Text value, OutputCollector <Text, LongWritable> output , Reporter reporter) { string text = value.ToString(); Matcher matcher = pattern.Matcher(text); while (matcher.Find()) { output.Collect(new Text(matcher.Group(group)), new LongWritable(1)); } }
internal sealed override string Parse(string str) { if (str != null && pattern != null) { if (!pattern.Matcher(str).Matches()) { throw new ArgumentException("Invalid value: \"" + str + "\" does not belong to the domain " + GetDomain()); } } return(str); }
/// <summary>Validate ZK path as valid for a DNS hostname.</summary> /// <param name="path">path to validate</param> /// <returns>the path parameter</returns> /// <exception cref="Org.Apache.Hadoop.Registry.Client.Exceptions.InvalidPathnameException /// ">if the pathname is invalid.</exception> public static string ValidateElementsAsDNS(string path) { IList <string> splitpath = Split(path); foreach (string fragment in splitpath) { if (!PathEntryValidationPattern.Matcher(fragment).Matches()) { throw new InvalidPathnameException(path, "Invalid Path element \"" + fragment + "\"" ); } } return(path); }
/// <summary>Checks whether the parameter is a valid compressed IPv6 address</summary> /// <param name="input">the address string to check for validity</param> /// <returns>true if the input parameter is a valid compressed IPv6 address</returns> public static bool IsIPv6HexCompressedAddress(string input) { int colonCount = 0; for (int i = 0; i < input.Length; i++) { if (input[i] == ColonChar) { colonCount++; } } return(colonCount <= MaxColonCount && Ipv6HexCompressedPattern.Matcher(input).Matches ()); }
// Ensure audit log has exactly N entries /// <exception cref="System.IO.IOException"/> private void VerifyAuditLogsRepeat(bool expectSuccess, int ndupe) { // Turn off the logs Logger logger = ((Log4JLogger)FSNamesystem.auditLog).GetLogger(); logger.SetLevel(Level.Off); // Close the appenders and force all logs to be flushed Enumeration <object> appenders = logger.GetAllAppenders(); while (appenders.MoveNext()) { Appender appender = (Appender)appenders.Current; appender.Close(); } BufferedReader reader = new BufferedReader(new FileReader(auditLogFile)); string line = null; bool ret = true; try { for (int i = 0; i < ndupe; i++) { line = reader.ReadLine(); NUnit.Framework.Assert.IsNotNull(line); NUnit.Framework.Assert.IsTrue("Expected audit event not found in audit log", auditPattern .Matcher(line).Matches()); ret &= successPattern.Matcher(line).Matches(); } NUnit.Framework.Assert.IsNull("Unexpected event in audit log", reader.ReadLine()); NUnit.Framework.Assert.IsTrue("Expected success=" + expectSuccess, ret == expectSuccess ); } finally { reader.Close(); } }
/// <summary> /// Verifies a value is a valid identifier, /// <code>[a-zA-z_][a-zA-Z0-9_\-]*</code>, up to a maximum length. /// </summary> /// <param name="value">string to check if it is a valid identifier.</param> /// <param name="maxLen">maximun length.</param> /// <param name="name">the name to use in the exception message.</param> /// <returns>the value.</returns> /// <exception cref="System.ArgumentException">if the string is not a valid identifier. /// </exception> public static string ValidIdentifier(string value, int maxLen, string name) { Check.NotEmpty(value, name); if (value.Length > maxLen) { throw new ArgumentException(MessageFormat.Format("[{0}] = [{1}] exceeds max len [{2}]" , name, value, maxLen)); } if (!IdentifierPattern.Matcher(value).Find()) { throw new ArgumentException(MessageFormat.Format("[{0}] = [{1}] must be '{2}'", name , value, IdentifierPatternStr)); } return(value); }
/// <summary>Extracts the timstamp component from the path.</summary> /// <param name="path"/> /// <returns>the timestamp component from the path</returns> public static string GetTimestampPartFromPath(string path) { Matcher matcher = TimestampDirPattern.Matcher(path); if (matcher.Find()) { string matched = matcher.Group(); string ret = string.Intern(matched); return(ret); } else { return(null); } }
private bool IsClause(CharSequence text) { Matcher m = PAT_WHITESPACE.Matcher(text.ToString()); int n = 1; while (m.Find()) { n++; if (n >= minWords) { return(true); } } return(n >= minWords); }
/// <exception cref="System.IO.IOException"/> private void CheckAndThrowLabelName(string label) { if (label == null || label.IsEmpty() || label.Length > MaxLabelLength) { throw new IOException("label added is empty or exceeds " + MaxLabelLength + " character(s)" ); } label = label.Trim(); bool match = LabelPattern.Matcher(label).Matches(); if (!match) { throw new IOException("label name should only contains " + "{0-9, a-z, A-Z, -, _} and should not started with {-,_}" + ", now it is=" + label); } }
/// <exception cref="NGit.Errors.InvalidPatternException"></exception> private static int FindGroupEnd(int indexOfStartBracket, string pattern) { int firstValidCharClassIndex = indexOfStartBracket + 1; int firstValidEndBracketIndex = indexOfStartBracket + 2; if (indexOfStartBracket + 1 >= pattern.Length) { throw new NoClosingBracketException(indexOfStartBracket, "[", "]", pattern); } if (pattern[firstValidCharClassIndex] == '!') { firstValidCharClassIndex++; firstValidEndBracketIndex++; } Matcher charClassStartMatcher = characterClassStartPattern.Matcher(pattern); int groupEnd = -1; while (groupEnd == -1) { int possibleGroupEnd = pattern.IndexOf(']', firstValidEndBracketIndex); if (possibleGroupEnd == -1) { throw new NoClosingBracketException(indexOfStartBracket, "[", "]", pattern); } bool foundCharClass = charClassStartMatcher.Find(firstValidCharClassIndex); if (foundCharClass && charClassStartMatcher.Start() < possibleGroupEnd) { string classStart = charClassStartMatcher.Group(0); string classEnd = classStart[1] + "]"; int classStartIndex = charClassStartMatcher.Start(); int classEndIndex = pattern.IndexOf(classEnd, classStartIndex + 2); if (classEndIndex == -1) { throw new NoClosingBracketException(classStartIndex, classStart, classEnd, pattern ); } firstValidCharClassIndex = classEndIndex + 2; firstValidEndBracketIndex = firstValidCharClassIndex; } else { groupEnd = possibleGroupEnd; } } return(groupEnd); }
/// <summary>Returns the path up to the random directory component.</summary> private Path GetPathToDelete(Path localPath) { Path delPath = localPath.GetParent(); string name = delPath.GetName(); Matcher matcher = RandomDirPattern.Matcher(name); if (matcher.Matches()) { return(delPath); } else { Log.Warn("Random directory component did not match. " + "Deleting localized path only" ); return(localPath); } }
private void FilterFiles(string pattern, ICollection <FilePath> candidates, bool exclusion ) { if (pattern != null && !pattern.IsEmpty()) { Sharpen.Pattern filterPattern = Sharpen.Pattern.Compile(pattern); for (IEnumerator <FilePath> candidatesItr = candidates.GetEnumerator(); candidatesItr .HasNext();) { FilePath candidate = candidatesItr.Next(); bool match = filterPattern.Matcher(candidate.GetName()).Find(); if ((!match && !exclusion) || (match && exclusion)) { candidatesItr.Remove(); } } } }
/// <exception cref="NBoilerpipe.BoilerpipeProcessingException"></exception> public bool Process(TextDocument doc) { bool changes = false; foreach (TextBlock tb in doc.GetTextBlocks()) { if (!tb.IsContent()) { continue; } string text = tb.GetText(); Matcher m = PAT_CLAUSE_DELIMITER.Matcher(text); bool found = m.Find(); int start = 0; int end; bool hasClause = false; while (found) { end = m.Start() + 1; hasClause = IsClause(text.SubSequence(start, end)); start = m.End(); if (hasClause) { break; } found = m.Find(); } end = text.Length; // since clauses should *always end* with a delimiter, we normally // don't consider text without one if (acceptClausesWithoutDelimiter) { hasClause |= IsClause(text.SubSequence(start, end)); } if (!hasClause) { tb.SetIsContent(false); changes = true; } } // System.err.println("IS NOT CONTENT: " + text); return(changes); }
/// <summary>Attempt to parse a storage uri with storage class and URI.</summary> /// <remarks> /// Attempt to parse a storage uri with storage class and URI. The storage /// class component of the uri is case-insensitive. /// </remarks> /// <param name="rawLocation"> /// Location string of the format [type]uri, where [type] is /// optional. /// </param> /// <returns> /// A StorageLocation object if successfully parsed, null otherwise. /// Does not throw any exceptions. /// </returns> /// <exception cref="System.IO.IOException"/> /// <exception cref="System.Security.SecurityException"/> public static Org.Apache.Hadoop.Hdfs.Server.Datanode.StorageLocation Parse(string rawLocation) { Matcher matcher = regex.Matcher(rawLocation); StorageType storageType = StorageType.Default; string location = rawLocation; if (matcher.Matches()) { string classString = matcher.Group(1); location = matcher.Group(2); if (!classString.IsEmpty()) { storageType = StorageType.ValueOf(StringUtils.ToUpperCase(classString)); } } return(new Org.Apache.Hadoop.Hdfs.Server.Datanode.StorageLocation(storageType, new Path(location).ToUri())); }
/// <exception cref="System.Exception"/> public override void Evaluate() { TestException testExceptionAnnotation = frameworkMethod.GetAnnotation <TestException >(); try { statement.Evaluate(); if (testExceptionAnnotation != null) { Type klass = testExceptionAnnotation.Exception(); NUnit.Framework.Assert.Fail("Expected Exception: " + klass.Name); } } catch (Exception ex) { if (testExceptionAnnotation != null) { Type klass = testExceptionAnnotation.Exception(); if (klass.IsInstanceOfType(ex)) { string regExp = testExceptionAnnotation.MsgRegExp(); Sharpen.Pattern pattern = Sharpen.Pattern.Compile(regExp); if (!pattern.Matcher(ex.Message).Find()) { NUnit.Framework.Assert.Fail("Expected Exception Message pattern: " + regExp + " got message: " + ex.Message); } } else { NUnit.Framework.Assert.Fail("Expected Exception: " + klass.Name + " got: " + ex.GetType ().Name); } } else { throw; } } }
/// <summary>Inform the parser of user-defined types.</summary> /// <exception cref="System.IO.IOException"/> private void AddUserIdentifiers(JobConf job) { Sharpen.Pattern x = Sharpen.Pattern.Compile("^mapred\\.join\\.define\\.(\\w+)$"); foreach (KeyValuePair <string, string> kv in job) { Matcher m = x.Matcher(kv.Key); if (m.Matches()) { try { Parser.CNode.AddIdentifier(m.Group(1), job.GetClass <ComposableRecordReader>(m.Group (0), null)); } catch (MissingMethodException e) { throw (IOException)Sharpen.Extensions.InitCause(new IOException("Invalid define for " + m.Group(1)), e); } } } }