text:
using Mono.Cecil;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Signum.TSGenerator
{
    public class PreloadingAssemblyResolver : DefaultAssemblyResolver
    {
        public AssemblyDefinition SignumUtilities { get; private set; }
        public AssemblyDefinition SignumEntities { get; private set; }

        Dictionary<string, string> assemblyLocations;

        public PreloadingAssemblyResolver(string[] references)
        {
            this.assemblyLocations = references.ToDictionary(a => Path.GetFileNameWithoutExtension(a));

            foreach (var dll in references.Where(r => r.Contains("Signum")))
            {
                var assembly = ModuleDefinition.ReadModule(dll, new ReaderParameters { AssemblyResolver = this }).Assembly;

                if (assembly.Name.Name == "Signum.Entities")
                    SignumEntities = assembly;

                if (assembly.Name.Name == "Signum.Utilities")
                    SignumUtilities = assembly;

                RegisterAssembly(assembly);
            }
        }

        public override AssemblyDefinition Resolve(AssemblyNameReference name)
        {
            var assembly = ModuleDefinition.ReadModule(this.assemblyLocations[name.Name], new ReaderParameters { AssemblyResolver = this }).Assembly;

            this.RegisterAssembly(assembly);

            return assembly;
        }
    }

    static class EntityDeclarationGenerator
    {
        internal class TypeCache
        {
            public TypeDefinition ModifiableEntity;
            public TypeDefinition InTypeScriptAttribute;
            public TypeDefinition ImportInTypeScriptAttribute;
            public TypeDefinition IEntity;

            public TypeCache(AssemblyDefinition signumEntities)
            {
                ModifiableEntity = signumEntities.MainModule.GetType("Signum.Entities", "ModifiableEntity");
                InTypeScriptAttribute = signumEntities.MainModule.GetType("Signum.Entities", "InTypeScriptAttribute");
                ImportInTypeScriptAttribute = signumEntities.MainModule.GetType("Signum.Entities", "ImportInTypeScriptAttribute");
                IEntity = signumEntities.MainModule.GetType("Signum.Entities", "IEntity");
            }
        }

        static TypeCache Cache;

        static ConcurrentDictionary<TypeReference, bool> IEntityCache = new ConcurrentDictionary<TypeReference, bool>();

        internal static string Process(AssemblyOptions options, string templateFileName, string currentNamespace)
        {
            StringBuilder sb = new StringBuilder();

            var entities = options.Resolver.SignumEntities;

            Cache = new TypeCache(entities);

            var namespacesReferences = new Dictionary<string, Dictionary<string, NamespaceTSReference>>();

            namespacesReferences.GetNamespaceReference(options, Cache.ModifiableEntity);

            var exportedTypes = options.ModuleDefinition.Types.Where(a => a.Namespace == currentNamespace).ToList();
            if (exportedTypes.Count == 0)
                throw new InvalidOperationException($"Assembly '{options.CurrentAssembly}' has not types in namespace '{currentNamespace}'");

            var imported = options.ModuleDefinition.Assembly.CustomAttributes.Where(at => at.AttributeType.FullName == Cache.ImportInTypeScriptAttribute.FullName)
                .Where(at => (string)at.ConstructorArguments[1].Value ==  currentNamespace)
                .Select(at => ((TypeReference)at.ConstructorArguments[0].Value).Resolve())
                .ToList();

            var importedMessage = imported.Where(a => a.Name.EndsWith("Message")).ToList();
            var importedEnums = imported.Except(importedMessage).ToList();

            var entityResults = (from type in exportedTypes
                                 where !type.IsValueType && (type.InTypeScript() ?? IsModifiableEntity(type))
                                 select new
                                 {
                                     ns = type.Namespace,
                                     type,
                                     text = EntityInTypeScript(type, options, namespacesReferences),
                                 }).ToList();

            var interfacesResults = (from type in exportedTypes
                                     where type.IsInterface && (type.InTypeScript() ?? type.AnyInterfaces(IEntityCache, i => i.FullName ==  Cache.IEntity.FullName))
                                     select new
                                     {
                                         ns = type.Namespace,
                                         type,
                                         text = EntityInTypeScript(type, options, namespacesReferences),
                                     }).ToList();

            var usedEnums = (from type in entityResults.Select(a => a.type)
                             from p in GetAllProperties(type)
                             let pt = (p.PropertyType.ElementType() ?? p.PropertyType).UnNullify()
                             let def = pt.Resolve()
                             where def != null && def.IsEnum
                             select def).Distinct().ToList();

            var symbolResults = (from type in exportedTypes
                                 where !type.IsValueType && type.IsStaticClass() && type.ContainsAttribute("AutoInitAttribute")
                                 && (type.InTypeScript() ?? true)
                                 select new
                                 {
                                     ns = type.Namespace,
                                     type,
                                     text = SymbolInTypeScript(type, options, namespacesReferences),
                                 }).ToList();

            var enumResult = (from type in exportedTypes
                              where type.IsEnum && (type.InTypeScript() ?? usedEnums.Contains(type))
                              select new
                              {
                                  ns = type.Namespace,
                                  type,
                                  text = EnumInTypeScript(type, options),
                              }).ToList();

            var externalEnums = (from type in usedEnums.Where(options.IsExternal).Concat(importedEnums)
                                select new
                                {
                                    ns = currentNamespace + ".External",
                                    type,
                                    text = EnumInTypeScript(type, options),
                                }).ToList();

            var externalMessages = (from type in importedMessage
                                    select new
                                    {
                                        ns = currentNamespace + ".External",
                                        type,
                                        text = MessageInTypeScript(type, options),
                                    }).ToList();

            var messageResults = (from type in exportedTypes
                                  where type.IsEnum && type.Name.EndsWith("Message")
                                  select new
                                  {
                                      ns = type.Namespace,
                                      type,
                                      text = MessageInTypeScript(type, options),
                                  }).ToList();

            var queryResult = (from type in exportedTypes
                               where type.IsEnum && type.Name.EndsWith("Query")
                               select new
                               {
                                   ns = type.Namespace,
                                   type,
                                   text = QueryInTypeScript(type, options),
                               }).ToList();

            var namespaces = entityResults
                .Concat(interfacesResults)
                .Concat(enumResult)
                .Concat(messageResults)
                .Concat(queryResult)
                .Concat(symbolResults)
                .Concat(externalEnums)
                .Concat(externalMessages)
                .GroupBy(a => a.ns)
                .OrderBy(a => a.Key);

            foreach (var ns in namespaces)
            {
                var key = RemoveNamespace(ns.Key.ToString(), currentNamespace);

                if (key.Length == 0)
                {
                    foreach (var item in ns.OrderBy(a => a.type.Name))
                    {
                        sb.AppendLine(item.text);
                    }
                }
                else
                {
                    sb.AppendLine("export namespace " + key + " {");
                    sb.AppendLine();

                    foreach (var item in ns.OrderBy(a => a.type.Name))
                    {
                        foreach (var line in item.text.Split(new[] { "\r\n" }, StringSplitOptions.None))
                            sb.AppendLine("  " + line);
                    }

                    sb.AppendLine("}");
                    sb.AppendLine();
                }
            }

            var code = sb.ToString();

            return WriteFillFile(options, code, templateFileName, namespacesReferences);
        }

        private static string WriteFillFile(AssemblyOptions options, string code, string templateFileName, Dictionary<string, Dictionary<string, NamespaceTSReference>> namespacesReferences)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(@"//////////////////////////////////");
            sb.AppendLine(@"//Auto-generated. Do NOT modify!//");
            sb.AppendLine(@"//////////////////////////////////");
            sb.AppendLine();
            var path = namespacesReferences.GetOrThrow("Signum.Entities").GetOrThrow("Signum.Entities").Path.Replace("Signum.Entities.ts", "Reflection.ts");
            sb.AppendLine($"import {{ MessageKey, QueryKey, Type, EnumType, registerSymbol }} from '{RelativePath(path, templateFileName)}'");

            foreach (var a in namespacesReferences.Values)
            {
                foreach (var ns in a.Values)
                {
                    sb.AppendLine($"import * as {ns.VariableName} from '{RelativePath(ns.Path, templateFileName)}'");
                }
            }
            sb.AppendLine();
            sb.AppendLine(File.ReadAllText(templateFileName));

            sb.AppendLine(code);

            return sb.ToString();
        }

        private static string RelativePath(string path, string fileName)
        {
            Uri pathUri = new Uri(path.RemoveSuffix(".ts"), UriKind.Absolute);
            Uri fileNameUri = new Uri(fileName, UriKind.Absolute);

            string relPath = fileNameUri.MakeRelativeUri(pathUri).ToString();

            var result = relPath.Replace(@"\", "/");

            if (!result.StartsWith(".."))
                return "./" + result;

            return result;
        }

        private static string EnumInTypeScript(TypeDefinition type, AssemblyOptions options)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"export const {type.Name} = new EnumType<{type.Name}>(\"{type.Name}\");");

            sb.AppendLine($"export type {type.Name} =");
            var fields = type.Fields.OrderBy(a => a.Constant as IComparable).Where(a => a.IsPublic && a.IsStatic).ToList();
            for (int i = 0; i < fields.Count; i++)
            {
                sb.Append($"  \"{fields[i].Name}\"");
                if (i < fields.Count - 1)
                    sb.AppendLine(" |");
                else
                    sb.AppendLine(";");
            }

            return sb.ToString();
        }

        private static string MessageInTypeScript(TypeDefinition type, AssemblyOptions options)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"export module {type.Name} {{");
            var fields = type.Fields.OrderBy(a => a.MetadataToken.RID).Where(a => a.IsPublic && a.IsStatic).ToList();

            foreach (var field in fields)
            {
                string context = $"By type {type.Name} and field {field.Name}";
                sb.AppendLine($"  export const {field.Name} = new MessageKey(\"{type.Name}\", \"{field.Name}\");");
            }
            sb.AppendLine(@"}");

            return sb.ToString();
        }

        private static string QueryInTypeScript(TypeDefinition type, AssemblyOptions options)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"export module {type.Name} {{");
            var fields = type.Fields.OrderBy(a => a.MetadataToken.RID).Where(a => a.IsPublic && a.IsStatic).ToList();

            foreach (var field in fields)
            {
                string context = $"By type {type.Name} and field {field.Name}";
                sb.AppendLine($"  export const {field.Name} = new QueryKey(\"{type.Name}\", \"{field.Name}\");");
            }
            sb.AppendLine(@"}");

            return sb.ToString();
        }

        static ConcurrentDictionary<TypeReference, bool> IOperationSymbolCache = new ConcurrentDictionary<TypeReference, bool>();

        private static string SymbolInTypeScript(TypeDefinition type, AssemblyOptions options, Dictionary<string, Dictionary<string, NamespaceTSReference>> namespacesReferences)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"export module {type.Name} {{");
            var fields = type.Fields.OrderBy(a => a.MetadataToken.RID).Where(a => a.IsPublic && a.IsStatic).ToList();

            foreach (var field in fields)
            {
                string context = $"By type {type.Name} and field {field.Name}";
                var propertyType = TypeScriptName(field.FieldType, type, options, namespacesReferences, context);

                var fieldTypeDef = field.FieldType.Resolve();
                var cleanType = fieldTypeDef.IsInterface && fieldTypeDef.AnyInterfaces(IOperationSymbolCache, i => i.Name == "IOperationSymbolContainer") ? "Operation" : CleanTypeName(fieldTypeDef);
                sb.AppendLine($"  export const {field.Name} : {propertyType} = registerSymbol(\"{cleanType}\", \"{type.Name}.{field.Name}\");");
            }
            sb.AppendLine(@"}");

            return sb.ToString();
        }

        private static string EntityInTypeScript(TypeDefinition type, AssemblyOptions options, Dictionary<string, Dictionary<string, NamespaceTSReference>> namespacesReferences)
        {
            StringBuilder sb = new StringBuilder();
            if (!type.IsAbstract)
                sb.AppendLine($"export const {type.Name} = new Type<{type.Name}>(\"{CleanTypeName(type)}\");");

            List<string> baseTypes = new List<string>();
            if (type.BaseType != null)
                baseTypes.Add(TypeScriptName(type.BaseType, type, options, namespacesReferences, $"By type {type.Name}"));

            var baseInterfaces = Parents(type.BaseType?.Resolve()).SelectMany(t => t.Resolve()?.Interfaces.Select(a => a.InterfaceType) ?? Enumerable.Empty<TypeReference>()).Select(a => a.FullName).ToHashSet();

            var interfaces = type.Interfaces.Select(i => i.InterfaceType).Where(it => !baseInterfaces.Contains(it.FullName))
                .Where(it => it.FullName == Cache.IEntity.FullName || it.Resolve()?.Interfaces.Any(it2 => it2.InterfaceType.FullName == Cache.IEntity.FullName) == true);

            foreach (var i in interfaces)
                baseTypes.Add(TypeScriptName(i, type, options, namespacesReferences, $"By type {type.Name}"));

            sb.AppendLine($"export interface {TypeScriptName(type, type, options, namespacesReferences, "declaring " + type.Name)} extends {string.Join(", ", baseTypes.Distinct())} {{");
            if (!type.IsAbstract && Parents(type.BaseType?.Resolve()).All(a => a.IsAbstract))
                sb.AppendLine($"  Type: \"{CleanTypeName(type)}\";");

            var properties = GetProperties(type);

            var defaultNullableCustomAttribute = type.NullableContextAttribute();

            foreach (var prop in properties)
            {
                string context = $"By type {type.Name} and property {prop.Name}";
                var propertyType = TypeScriptNameInternal(prop.PropertyType, type, options, namespacesReferences, context) + (prop.GetTypescriptNull(defaultNullableCustomAttribute) ? " | null" : "");

                var undefined = prop.GetTypescriptUndefined() ? "?" : "";

                sb.AppendLine($"  {FirstLower(prop.Name)}{undefined}: {propertyType};");
            }
            sb.AppendLine(@"}");

            return sb.ToString();
        }

        static CustomAttribute NullableContextAttribute(this TypeDefinition type)
        {
            return type.CustomAttributes.SingleOrDefault(a => a.AttributeType.Name == "NullableContextAttribute") ?? type.DeclaringType?.NullableContextAttribute();
        }

        static ConcurrentDictionary<TypeReference, bool> IsModifiableDictionary = new ConcurrentDictionary<TypeReference, bool>();

        static bool IsModifiableEntity(TypeDefinition t)
        {
            if (t.IsValueType || t.IsInterface)
                return false;

            if (!InheritsFromModEntity(t))
                return false;

            return true;

            bool InheritsFromModEntity(TypeReference tr)
            {
                return IsModifiableDictionary.GetOrAdd(tr, tr =>
                {
                    if (tr.FullName == Cache.ModifiableEntity.FullName)
                        return true;

                    var td = tr.Resolve();

                    if (td.BaseType == null || td.BaseType.FullName == "System.Object")
                        return false;

                    return InheritsFromModEntity(td.BaseType);
                });
            }
        }

        private static IEnumerable<TypeDefinition> Parents(TypeDefinition type)
        {
            while (type != null && type.FullName != Cache.ModifiableEntity.FullName)
            {
                yield return type;
                type = type.BaseType?.Resolve();
            }
        }

        static string CleanTypeName(TypeDefinition t)
        {
            if (!t.AnyInterfaces(IEntityCache, tr => tr.FullName == Cache.IEntity.FullName))
                return t.Name;

            if (t.Name.EndsWith("Entity"))
                return t.Name.RemoveSuffix("Entity");

            if (t.Name.EndsWith("Model"))
                return t.Name.RemoveSuffix("Model");

            if (t.Name.EndsWith("Symbol"))
                return t.Name.RemoveSuffix("Symbol");

            return t.Name;
        }

        static string RemoveSuffix(this string text, string postfix)
        {
            if (text.EndsWith(postfix) && text != postfix)
                return text.Substring(0, text.Length - postfix.Length);

            return text;
        }

        private static IEnumerable<PropertyDefinition> GetAllProperties(TypeDefinition type) {

            return GetProperties(type).Concat(type.BaseType == null ? Enumerable.Empty<PropertyDefinition>() : GetAllProperties(type.BaseType.Resolve()));
        }

        private static IEnumerable<PropertyDefinition> GetProperties(TypeDefinition type)
        {
            return type.Properties.Where(p => p.HasThis && p.GetMethod.IsPublic)
                            .Where(p => p.InTypeScript() ?? !(p.ContainsAttribute("HiddenPropertyAttribute") || p.ContainsAttribute("ExpressionFieldAttribute") || p.ContainsAttribute("AutoExpressionFieldAttribute")));
        }

        public static bool ContainsAttribute(this IMemberDefinition p, string attributeName)
        {
            return p.CustomAttributes.Any(a => a.AttributeType.Name == attributeName);
        }

        public static CustomAttribute GetAttributeInherit(this TypeDefinition type, string attributeName)
        {
            if (type == null)
                return null;

            var att = type.CustomAttributes.SingleOrDefault(a => a.AttributeType.FullName == attributeName);
            if (att != null)
                return att;

            return GetAttributeInherit(type.BaseType?.Resolve(), attributeName);
        }

        public static bool? InTypeScript(this MemberReference mr)
        {
            var attr = mr.Resolve().CustomAttributes.SingleOrDefault(a => a.AttributeType.FullName == Cache.InTypeScriptAttribute.FullName);

            if (attr == null)
                return null;

            return (bool?)attr.ConstructorArguments.FirstOrDefault().Value;
        }

        public static bool GetTypescriptUndefined(this PropertyDefinition p)
        {
            var ainTSAttr = p.CustomAttributes.SingleOrDefault(a => a.AttributeType.FullName == Cache.InTypeScriptAttribute.FullName);

            var b = (bool?)ainTSAttr?.Properties.SingleOrDefault(a => a.Name == "Undefined").Argument.Value;

            if (b != null)
                return b.Value;

            return GetTypescriptUndefined(p.DeclaringType) ?? false;
        }

        private static bool? GetTypescriptUndefined(TypeDefinition declaringType)
        {
            var inTSAttr = GetAttributeInherit(declaringType, Cache.InTypeScriptAttribute.FullName);

            return (bool?)inTSAttr?.Properties.SingleOrDefault(a => a.Name == "Undefined").Argument.Value;
        }

        public static bool GetTypescriptNull(this PropertyDefinition p, CustomAttribute defaultCustomAttribute)
        {
            var inTSAttr = p.CustomAttributes.SingleOrDefault(a => a.AttributeType.FullName == Cache.InTypeScriptAttribute.FullName);

            var b = (bool?)inTSAttr?.Properties.SingleOrDefault(a => a.Name == "Null").Argument.Value;
            if (b != null)
                return b.Value;

            if (p.PropertyType.IsValueType)
                return p.PropertyType.IsNullable();
            else
            {
                var nullableAttr = p.CustomAttributes.SingleOrDefault(a => a.AttributeType.Name == "NullableAttribute");

                if (nullableAttr == null)
                    nullableAttr = defaultCustomAttribute;

                if (nullableAttr == null)
                    return false;

                var arg = nullableAttr.ConstructorArguments[0].Value;

                if (arg is byte val)
                    return val == 2;

                if (arg is CustomAttributeArgument[] args)
                    return ((byte)args[0].Value) == 2;

                throw new InvalidOperationException("Unexpected value of type " + arg.GetType() + " in NullableAttribute constructor");
            }
        }

        private static string FirstLower(string name)
        {
            return char.ToLowerInvariant(name[0]) + name.Substring(1);
        }

        public static bool IsNullable(this TypeReference type)
        {
            return type is GenericInstanceType gtype && gtype.ElementType.Name == "Nullable`1";
        }

        public static TypeReference UnNullify(this TypeReference type)
        {
            return type is GenericInstanceType gtype && gtype.ElementType.Name == "Nullable`1" ? gtype.GenericArguments.Single() : type;
        }

        static ConcurrentDictionary<TypeReference, bool> IEnumerableCache = new ConcurrentDictionary<TypeReference, bool>();

        public static TypeReference ElementType(this TypeReference type)
        {
            if (!(type is GenericInstanceType gen))
                return null;

            if (type.FullName == typeof(string).FullName || type.FullName == typeof(byte[]).FullName)
                return null;

            var def = type.Resolve();
            if (def == null)
                return null;

            if (!gen.AnyInterfaces(IEnumerableCache, tr => tr is GenericInstanceType git && git.ElementType.Name == "IEnumerable`1"))
                return null;

            return gen.GenericArguments.Single();
        }

        static string TypeScriptName(TypeReference type, TypeDefinition current, AssemblyOptions options, Dictionary<string, Dictionary<string, NamespaceTSReference>> namespacesReferences, string errorContext)
        {
            var ut = type.UnNullify();
            if (ut != type)
                return TypeScriptNameInternal(ut, current, options, namespacesReferences, errorContext) + " | null";

            return TypeScriptNameInternal(type, current, options, namespacesReferences, errorContext);
        }

        private static string TypeScriptNameInternal(TypeReference type, TypeDefinition current, AssemblyOptions options, Dictionary<string, Dictionary<string, NamespaceTSReference>> namespacesReferences, string errorContext)
        {
            type = type.UnNullify();

            if (type.FullName == typeof(Boolean).FullName)
                return "boolean";

            if (type.FullName == typeof(Char).FullName)
                return "string";

            if (type.FullName == typeof(SByte).FullName ||
                type.FullName == typeof(Byte).FullName ||
                type.FullName == typeof(Int16).FullName ||
                type.FullName == typeof(UInt16).FullName ||
                type.FullName == typeof(Int32).FullName ||
                type.FullName == typeof(UInt32).FullName ||
                type.FullName == typeof(Int64).FullName ||
                type.FullName == typeof(UInt64).FullName ||
                type.FullName == typeof(Decimal).FullName ||
                type.FullName == typeof(Single).FullName ||
                type.FullName == typeof(Double).FullName)
                return "number";

            if (type.FullName == typeof(String).FullName)
                return "string";

            if (type.FullName == typeof(DateTime).FullName ||
                type.FullName == "Signum.Utilities.Date"
                )
                return "string";

            if (type.FullName == typeof(DateTimeOffset).FullName)
                return "string";

            if (type.FullName == typeof(Guid).FullName ||
                type.FullName == typeof(Byte[]).FullName ||
                type.FullName == typeof(TimeSpan).FullName)
                return "string";

            if (type.IsGenericParameter)
                return type.Name;

            if (type is GenericInstanceType git)
                return RelativeName(type.Resolve(), current, options, namespacesReferences, errorContext) + "<" + string.Join(", ", git.GenericArguments.Select(a => TypeScriptName(a, current, options, namespacesReferences, errorContext)).ToList()) + ">";
            else if (type.HasGenericParameters)
                return RelativeName(type.Resolve(), current, options, namespacesReferences, errorContext) + "<" + string.Join(", ", type.GenericParameters.Select(gp => gp.Name)) + ">";
            else if (type is ArrayType at)
                return TypeScriptName(at.ElementType, current, options, namespacesReferences, errorContext) + "[]";
            else
                return RelativeName(type.Resolve(), current, options, namespacesReferences, errorContext);
        }

        private static string RelativeName(TypeDefinition type, TypeDefinition current, AssemblyOptions options, Dictionary<string, Dictionary<string, NamespaceTSReference>> namespacesReferences, string errorContext)
        {
            if (type.IsGenericParameter)
                return type.Name;

            if (type.DeclaringType != null)
                return RelativeName(type.DeclaringType, current, options, namespacesReferences, errorContext) + "_" + BaseTypeScriptName(type);

            if (type.Module.Assembly.Equals(current.Module.Assembly) && type.Namespace == current.Namespace)
            {
                string relativeNamespace = RelativeNamespace(type, current);

                return CombineNamespace(relativeNamespace, BaseTypeScriptName(type));
            }
            else if (type.IsEnum && options.IsExternal(type))
            {
                return "External." + BaseTypeScriptName(type);
            }
            else
            {
                var nsReference = GetNamespaceReference(namespacesReferences, options, type);
                if (nsReference == null)
                {
                    if (type.Interfaces.Any(i => i.InterfaceType.FullName == typeof(IEnumerable).FullName))
                        return "Array";

                    throw new InvalidOperationException($"{errorContext}:  Type {type.ToString()} is declared in the assembly '{type.Module.Assembly.Name}', but no React directory for it is found.");
                }

                return CombineNamespace(nsReference.VariableName, BaseTypeScriptName(type));
            }
        }

        public static bool AnyInterfaces(this TypeReference type, ConcurrentDictionary<TypeReference, bool> cache, Func<TypeReference, bool> func)
        {
            return cache.GetOrAdd(type, type =>
            {
                var td = type.Resolve();

                foreach (var item in td.Interfaces)
                {
                    if (func(item.InterfaceType))
                        return true;
                }

                if (td.BaseType == null)
                    return false;

                return td.BaseType.AnyInterfaces(cache, func);
            });
        }

        public static NamespaceTSReference GetNamespaceReference(this Dictionary<string, Dictionary<string, NamespaceTSReference>> references,  AssemblyOptions options, TypeDefinition type)
        {
            AssemblyReference assemblyReference;
            options.AssemblyReferences.TryGetValue(type.Module.Assembly.Name.Name, out assemblyReference);
            if (assemblyReference == null)
                return null;

            return references.GetOrCreate(type.Module.Assembly.Name.Name, () => new Dictionary<string, NamespaceTSReference>())
                .GetOrCreate(type.Namespace, () => new NamespaceTSReference
                {
                    Namespace = type.Namespace,
                    Path = FindDeclarationsFile(assemblyReference, type.Namespace, type),
                    VariableName = GetVariableName(references, type.Namespace.Split('.'))
                });
        }

        static T GetOrCreate<K, T>(this Dictionary<K, T> dictionary, K key, Func<T> create)
        {
            if (dictionary.TryGetValue(key, out var result))
                return result;

            return dictionary[key] = create();
        }

        private static string GetVariableName(Dictionary<string, Dictionary<string, NamespaceTSReference>> namespaceReferences, string[] nameParts)
        {
            var list = namespaceReferences.Values.SelectMany(a => a.Values.Select(ns => ns.VariableName));

            for (int i = 1; ; i++)
            {
                foreach (var item in nameParts.Reverse())
                {
                    var candidate = item + (i == 1 ? "" : i.ToString());
                    if (!list.Contains(candidate))
                        return candidate;
                }
            }
        }

        private static string FindDeclarationsFile(AssemblyReference assemblyReference, string @namespace, TypeDefinition typeForError)
        {
            var fileTS = @namespace + ".ts";

            var result = assemblyReference.AllTypescriptFiles.Where(a => Path.GetFileName(a) == fileTS).ToList();

            if (result.Count == 1)
                return result.Single();

            if (result.Count > 1)
                throw new InvalidOperationException($"importing '{typeForError}' required but multiple '{fileTS}' were found inside '{assemblyReference.ReactDirectory}':\r\n{string.Join("\r\n", result.Select(a => "  " + a).ToArray())}");

            var fileT4S = @namespace + ".t4s";

            result = assemblyReference.AllTypescriptFiles.Where(a => Path.GetFileName(a) == fileT4S).ToList();

            if (result.Count == 1)
                return result.Single().RemoveSuffix(".t4s") + ".ts";

            if (result.Count > 1)
                throw new InvalidOperationException($"importing '{typeForError}' required but multiple '{fileT4S}' were found inside '{assemblyReference.ReactDirectory}':\r\n{string.Join("\r\n", result.Select(a => "  " + a).ToArray())}");

            throw new InvalidOperationException($"importing '{typeForError}' required but no '{fileTS}' or '{fileT4S}' found inside '{assemblyReference.ReactDirectory}'");
        }

        private static string BaseTypeScriptName(TypeDefinition type)
        {
            if (type.FullName == Cache.IEntity.FullName)
                return "Entity";

            var name = type.Name;

            int pos = name.IndexOf('`');

            if (pos == -1)
                return name;

            return name.Substring(0, pos);
        }

        private static string RelativeNamespace(TypeDefinition referedType, TypeDefinition current)
        {
            var referedNS = referedType.Namespace.Split('.').ToList();
            var currentNS = current.Namespace.Split('.').ToList();

            var equal = referedNS.Zip(currentNS, (a, b) => new { a, b }).Where(p => p.a == p.b).Count();

            referedNS.RemoveRange(0, equal);
            return string.Join(".", referedNS);
        }

        private static string CombineNamespace(params string[] parts)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var p in parts)
            {
                if (!string.IsNullOrEmpty(p))
                {
                    if (sb.Length > 0)
                        sb.Append(".");

                    sb.Append(p);
                }
            }
            return sb.ToString();
        }

        private static string RemoveNamespace(string v, string baseNamespace)
        {
            if (v == baseNamespace)
                return "";

            if (v.StartsWith(baseNamespace + "."))
                return v.Substring((baseNamespace + ".").Length);

            return v;
        }

        public static bool IsStaticClass(this TypeDefinition type)
        {
            return type.IsAbstract && type.IsSealed;
        }
    }

    [Serializable]
    public class AssemblyOptions
    {
        public string CurrentAssembly;

        public Dictionary<string, AssemblyReference> AssemblyReferences;

        public Dictionary<string, string> AllReferences { get; internal set; }

        public PreloadingAssemblyResolver Resolver { get; internal set; }
        public ModuleDefinition ModuleDefinition { get; internal set; }

        public bool IsExternal(TypeDefinition type)
        {
            return type.Module.Assembly.Name.Name != CurrentAssembly &&
                 !AssemblyReferences.ContainsKey(type.Module.Assembly.Name.Name);
        }

        //public Assembly Default_Resolving(AssemblyLoadContext arg1, AssemblyName arg2)
        //{
        //    Console.WriteLine(arg2.Name);
        //    if (AllReferences.TryGetValue(arg2.Name, out string path))
        //        return arg1.LoadFromAssemblyPath(path);

        //    return null;
        //}

    }

    [Serializable]
    public class AssemblyReference
    {
        public string ReactDirectory;
        public string AssemblyFullPath;
        public string AssemblyName;

        public List<string> AllTypescriptFiles;
    }

    public class NamespaceTSReference
    {
        public string Namespace;
        public string Path;
        public string VariableName;
    }

    public static class DictionaryExtensions
    {
        public static V GetOrThrow<K, V>(this Dictionary<K, V> dictionary, K key)
        {
            V result;
            if (!dictionary.TryGetValue(key, out result))
                throw new KeyNotFoundException($"Key '{key}' not found");

            return result;
        }
    }
}


text:
package org.apache.ibatis.submitted.dynsql2;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class DynSqlTest {

  protected static SqlSessionFactory sqlSessionFactory;

  @BeforeAll
  static void setUp() throws Exception {
    try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/dynsql2/MapperConfig.xml")) {
      sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    }

    BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
            "org/apache/ibatis/submitted/dynsql2/CreateDB.sql");
  }

  @Test
  void testDynamicSelectWithTypeHandler() {
    try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
      List<Name> names = new ArrayList<>();

      Name name = new Name();
      name.setFirstName("Fred");
      name.setLastName("Flintstone");
      names.add(name);

      name = new Name();
      name.setFirstName("Barney");
      name.setLastName("Rubble");
      names.add(name);

      Parameter parameter = new Parameter();
      parameter.setNames(names);

      List<Map<String, Object>> answer = sqlSession.selectList("org.apache.ibatis.submitted.dynsql2.dynamicSelectWithTypeHandler", parameter);

      assertEquals(2, answer.size());
    }
  }

  @Test
  void testSimpleSelect() {
    try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
      Map<String, Object> answer = sqlSession.selectOne("org.apache.ibatis.submitted.dynsql2.simpleSelect", 1);

      assertEquals(answer.get("ID"), 1);
      assertEquals(answer.get("FIRSTNAME"), "Fred");
      assertEquals(answer.get("LASTNAME"), "Flintstone");
    }
  }
}

redpajama_set_name: RedPajamaGithub


text:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <title>encode_to_url (OpenID::Server::CheckIDRequest)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />

# File lib/openid/server.rb, line 769
      def encode_to_url(server_url)
        # Encode this request as a URL to GET.
        #
        # server_url:: The URL of the OpenID server to make this
        #              request of.
        if !@return_to
          raise NoReturnToError
        end

        # Imported from the alternate reality where these classes are
        # used in both the client and server code, so Requests are
        # Encodable too.  That's right, code imported from alternate
        # realities all for the love of you, id_res/user_setup_url.
        q = {'mode' => @mode,
             'identity' => @identity,
             'claimed_id' => @claimed_id,
             'return_to' => @return_to}

        if @trust_root
          if @message.is_openid1
            q['trust_root'] = @trust_root
          else
            q['realm'] = @trust_root
          end
        end

        if @assoc_handle
          q['assoc_handle'] = @assoc_handle
        end

        response = Message.new(@message.get_openid_namespace)
        response.update_args(@message.get_openid_namespace, q)
        return response.to_url(server_url)
      end


text:
package germ.gui.windows;

import germ.app.Application;
import germ.configuration.ConfigurationManager;
import germ.i18n.Messages;
import germ.model.workspace.Workspace;
import germ.util.Cursors;

import java.awt.Container;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 * Klasa predstavlja prozor za izbor workspace-a u kome ce se raditi po pokretanju aplikacije.
 */
@SuppressWarnings("serial")
public class WorkspaceChooserWindow extends JDialog {

	private JLabel lblWorkspace = new JLabel(Messages.getString("WorkspaceChooserWindow.0")); //$NON-NLS-1$
	private JTextField tfWorkspace = new JTextField();
	private JButton btnBrowse = new JButton(Messages.getString("WorkspaceChooserWindow.1")); //$NON-NLS-1$
	private JCheckBox chbUseAsDefault = new JCheckBox(
			Messages.getString("WorkspaceChooserWindow.2")); //$NON-NLS-1$
	private JButton btnOK = new JButton(Messages.getString("WorkspaceChooserWindow.3")); //$NON-NLS-1$
	private JButton btnCancel = new JButton(Messages.getString("WorkspaceChooserWindow.4")); //$NON-NLS-1$
	private Box okCancelBox = Box.createHorizontalBox();
	private Box workspaceBox = Box.createHorizontalBox();
	/**
	 * Rezultat dijaloga:  false = cancel, true = ok
	 */
	private boolean dialogResult = false;

	public WorkspaceChooserWindow() {

		try{
			setIconImage(ImageIO.read(new File("germ/gui/windows/images/programIcon.png")));
			}catch(Exception ex){}
		setModalityType(ModalityType.APPLICATION_MODAL);
		setResizable(false);
		setTitle(Messages.getString("WorkspaceChooserWindow.5")); //$NON-NLS-1$
		setLayout(new GridBagLayout());
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getCenterPoint();

		String currentWorkspace = ConfigurationManager.getInstance().getString(
				"workspace"); //$NON-NLS-1$
		if (Workspace.isValidWorkspace(currentWorkspace)) {
			setWorkspace(currentWorkspace);
		} else {
			currentWorkspace = System.getProperty("user.home") + File.separator + "GERM"; //$NON-NLS-1$ //$NON-NLS-2$
			setWorkspace(currentWorkspace);
		}

		Container container = getContentPane();

		GridBagConstraints c0 = new GridBagConstraints();
		c0.gridx = 0;
		c0.gridy = 0;
		c0.gridwidth = 2;
		c0.fill = GridBagConstraints.HORIZONTAL;
		c0.anchor = GridBagConstraints.WEST;
		c0.insets = new Insets(20, 20, 0, 20);

		GridBagConstraints c01 = new GridBagConstraints();
		c01.gridx = 0;
		c01.gridy = 1;
		c01.anchor = GridBagConstraints.WEST;
		c01.insets = new Insets(20, 20, 0, 0);

		GridBagConstraints c11 = new GridBagConstraints();
		c11.gridx = 1;
		c11.gridy = 1;
		c11.anchor = GridBagConstraints.EAST;
		c11.insets = new Insets(20, 20, 10, 20);

		okCancelBox.add(btnOK);
		okCancelBox.add(Box.createHorizontalStrut(20));
		okCancelBox.add(btnCancel);

		workspaceBox.add(lblWorkspace);
		workspaceBox.add(Box.createHorizontalStrut(10));
		workspaceBox.add(tfWorkspace);
		workspaceBox.add(Box.createHorizontalStrut(15));
		workspaceBox.add(btnBrowse);

		container.add(workspaceBox, c0);
		container.add(chbUseAsDefault, c01);
		container.add(okCancelBox, c11);

		btnBrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setWorkspace(Workspace
						.workspaceChooser(WorkspaceChooserWindow.this));
			}
		});

		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterPressed();
			}
		});

		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				escapePressed();
			}
		});

		KeyboardFocusManager manager = KeyboardFocusManager
				.getCurrentKeyboardFocusManager();
		manager.addKeyEventDispatcher(new KeyEventDispatcher() {
			public boolean dispatchKeyEvent(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					escapePressed();
				} else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					enterPressed();
				}
				return false;
			}
		});

		setCursor(Cursors.getCursor("default")); //$NON-NLS-1$

		this.pack();
		setLocation(center.x - getSize().width / 2, center.y - getSize().height / 2);

	}

	public String getWorkspace() {
		return tfWorkspace.getText();
	}

	public void setWorkspace(String workspace) {
		this.tfWorkspace.setText(workspace);
	}

	public boolean isUseAsDefault() {
		return chbUseAsDefault.isSelected();
	}

	public boolean isDialogResult() {
		return dialogResult;
	}

	public void setDialogResult(boolean dialogResult) {
		this.dialogResult = dialogResult;
	}

	/**
	 * Funkcija definise reakciju programa na pritisak entera ili "ok" dugmeta na prozoru.
	 * Pre zatvaranja prozora ce se ukoliko unesena putanja za workspace ne postoji napraviti novi folder sa adekvatnim nazivom
	 * i uneti nova podesavanja u kofiguracioni menadzer.
	 * Rezultat dijaloga postaje potvrdan i dijalog se sakriva.
	 */
	public void enterPressed() {
		setDialogResult(true);
		File newProjectDir = new File(getWorkspace());
		if(!newProjectDir.exists()){
			if (!newProjectDir.mkdirs()){
				setAlwaysOnTop(false);
				JOptionPane.showMessageDialog(Application.getInstance().getMainWindow(), Messages.getString("WorkspaceChooserWindow.10")); //$NON-NLS-1$
			}
		}
		ConfigurationManager.getInstance().setString("workspace", getWorkspace()); //$NON-NLS-1$
		if(isUseAsDefault()) ConfigurationManager.getInstance().setBoolean("defaultWorkspace",true); //$NON-NLS-1$
		setVisible(false);
	}

	/**
	 * Funkcija definise reakciju programa na pritisak escape-a ili "Cancel" dugmeta na prozoru.
	 * Rezultat dijaloga postaje odrican i dijalog se sakriva.
	 */
	public void escapePressed() {
		setDialogResult(false);
		setVisible(false);
	}

}


text:
package POS;

//a Item class storing the itemname, price, and number
public class Item {
	public String itemName;
	public Integer number;
	public Float price;
	public Discount discount;
	public Item(String iName, Integer _number, Float _price){
		itemName = iName;
		number = _number;
		price = _price;
		discount = null;
	}
	public void setDiscount(Discount _discount) {
		discount = _discount;
	}
}


text:
'''
Author:
Description:
'''
from datetime import datetime
from core.config.settings import logger
from core.people.person import Profile, ProfileRequest, Session
from core.config.settings import REDIS
import redis
from core.people.person import update_list_from_jabber
from sqlalchemy import and_

def search_users_request(request, uuid):
        logger.info('Search for a request')

        prefix = 'client-request-'
        response = ''

        #need cache results for an hour because a lot of users
        #can have the same request
        #hour = datetime.now().hour
        minute = datetime.now().minute
        key = prefix + datetime.now().strftime("%Y-%m-%d-") + str(minute)
        old = prefix + datetime.now().strftime("%Y-%m-%d-") + str(minute - 1)

        #use unix socket
        r = redis.Redis(password=REDIS['password'],
                        unix_socket_path=REDIS['socket'])

        existing = r.get(key)

        sess = Session()

        #find user profile by uuid
        profile = sess.query(
            Profile).filter(Profile.uuid == uuid).one()

        #read for all for now
        allowed_to_read_all = True

        #here I need to know request to be public or not
        if profile.type == 'admin':
            allowed_to_read_all = True

        if existing:
            return existing
        else:
            if allowed_to_read_all:
                requests = sess.query(ProfileRequest).filter(
                    ProfileRequest.request.like('%' + request + '%')).all()
            else:
                requests = sess.query(ProfileRequest).filter(
                    and_(
                        ProfileRequest.uuid == uuid,
                        ProfileRequest.request.like('%' + request + '%')
                    )
                ).all()

            response = [req.request for req in requests]
            response = "\n".join(response)

            if r.set(key, response):
                r.delete(old)
                return response

def save_users_request(sender, text):
    """ should be in separate file"""
    #start sqlalchemy session
    sess = Session()
    profile = None

    #exctract sender email
    if sender:
        email = sender.split('/')[0]
        if email:
            update_list_from_jabber({email})
            #find user profile by primary email
            try:
                profile = sess.query(Profile).filter(
                    Profile.email == email).one()
            except Exception as e:
                logger.exception(e)
                return False

    if not profile:
        return False

    req = {}
    logger.info('save new request %s' % text)
    try:
        req['request'] = text
        req['type'] = 'command'
        req['uuid'] = profile.uuid
        request = ProfileRequest(**req)
        sess.add(request)
        sess.commit()
    except Exception as e:
        sess.rollback()
        logger.exception(e)


text:
Adds an AWS::EMR::InstanceGroupConfig.ScalingRule resource property to the template.
ScalingRule is a subproperty of the AutoScalingPolicy property type.
ScalingRule defines the scale-in or scale-out rules for scaling activity, including the CloudWatch metric alarm that triggers activity, how EC2 instances are added or removed, and the periodicity of adjustments.
The automatic scaling policy for an instance group can comprise one or more automatic scaling rules.

## SYNTAX

```
Add-VSEMRInstanceGroupConfigScalingRule [-Action] <Object> [[-Description] <Object>] [-Name] <Object>
 [-Trigger] <Object> [<CommonParameters>]
```

## DESCRIPTION
Adds an AWS::EMR::InstanceGroupConfig.ScalingRule resource property to the template.
ScalingRule is a subproperty of the AutoScalingPolicy property type.
ScalingRule defines the scale-in or scale-out rules for scaling activity, including the CloudWatch metric alarm that triggers activity, how EC2 instances are added or removed, and the periodicity of adjustments.
The automatic scaling policy for an instance group can comprise one or more automatic scaling rules.

## PARAMETERS

### -Action
The conditions that trigger an automatic scaling activity.

Documentation: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-scalingrule.html#cfn-elasticmapreduce-instancegroupconfig-scalingrule-action
Type: ScalingAction
UpdateType: Mutable

```yaml
Type: Object
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Description
A friendly, more verbose description of the automatic scaling rule.

Documentation: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-scalingrule.html#cfn-elasticmapreduce-instancegroupconfig-scalingrule-description
PrimitiveType: String
UpdateType: Mutable

```yaml
Type: Object
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Name
The name used to identify an automatic scaling rule.
Rule names must be unique within a scaling policy.

Documentation: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-scalingrule.html#cfn-elasticmapreduce-instancegroupconfig-scalingrule-name
PrimitiveType: String
UpdateType: Mutable

```yaml
Type: Object
Parameter Sets: (All)
Aliases:

Required: True
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Trigger
The CloudWatch alarm definition that determines when automatic scaling activity is triggered.

Documentation: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-scalingrule.html#cfn-elasticmapreduce-instancegroupconfig-scalingrule-trigger
Type: ScalingTrigger
UpdateType: Mutable

```yaml
Type: Object
Parameter Sets: (All)
Aliases:

Required: True
Position: 4
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).

## INPUTS

## OUTPUTS

### Vaporshell.Resource.EMR.InstanceGroupConfig.ScalingRule
## NOTES

## RELATED LINKS

[http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-scalingrule.html](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-scalingrule.html)


text:
var cheerio = require('cheerio');
var async = require('async');
var helpers = require('./helpers');

function Parser() {

}

Parser.prototype.defaultResult = {
	html: [],
	js: [],
	css: [],
	files: [],
	body: null
};

// Parse HTML pages, get images/css/js files URLs, replace with local filenames & return elements to be stacked and modified page contents
Parser.prototype.html = function(url, body, callback) {
	var _this = this;

	var result = helpers.cloneObject(this.defaultResult);

	var $ = cheerio.load(body);

	// Get base URL
	var $base = $('base');
	var baseUrl;
	if($base.length > 0) {
		baseUrl = $base.attr('href');
	} else {
		baseUrl = url.substring(0, url.lastIndexOf('/')+1)
	}

	// Get CSS files urls
	$('link[rel=stylesheet]').each(function(i, elem) {
		var url = $(this).attr('href');

		url = helpers.normalizeUrl(baseUrl, url);

		result.css.push(url);

		$(this).attr('href', helpers.urlToFilename(url));
	});

	// Get JS files urls
	$('script[src]').each(function(i, elem) {
		var url = $(this).attr('src');

		url = helpers.normalizeUrl(baseUrl, url);

		result.js.push(url);

		$(this).attr('src', helpers.urlToFilename(url));
	});

	// Get images urls
	$('img[src]').each(function(i, elem) {
		var url = $(this).attr('src');

		url = helpers.normalizeUrl(baseUrl, url);

		result.files.push(url);

		$(this).attr('src', helpers.urlToFilename(url));
	});

	result.body = $.html();

	callback(null, result);
};

// Parse CSS files
Parser.prototype.css = function(url, body, callback) {
	var result = helpers.cloneObject(this.defaultResult);

	var baseUrl = url.substring(0, url.lastIndexOf('/')+1);

	var imgRegex = /url\((.*?g)\)/ig;

	while ((match = imgRegex.exec(body)) !== null) {
		var imgUrl = helpers.normalizeUrl(baseUrl, match[1]);

		result.files.push(imgUrl);

		// TODO: why paths are not replaced here ??
		body.replace(match[1], helpers.urlToFilename(imgUrl));
	}

	result.body = body;

	callback(null, result);
};

// Parse JS files
Parser.prototype.js = function(url, body, callback) {
	var result = helpers.cloneObject(this.defaultResult);

	result.body = body;

	callback(null, result);
};

// Parse files (images, etc)
Parser.prototype.files = function(url, body, callback) {
	var result = helpers.cloneObject(this.defaultResult);

	result.body = body;

	callback(null, result);
};

module.exports = Parser;


text:
using System;
using System.Collections.Generic;
using Lab10.Models;

namespace Lab10.Repositories
{
    public interface IUserRepository : IDisposable
    {
        void CreateEmployee(Employee employee);
        void DeleteEmployee(Employee employee);

        List<Employee> GetEmployees();
    }
}


text:
using System.IO;
using Casper.Domain.Features.BlogPosts;

namespace Casper.Data.Git.Infrastructure
{
    public static class BlogPostSerialization
    {
        public static BlogPost Deserialize(string relativeUri, string markdownWithFrontMatter, IYamlMarkdown yamlMarkdown)
        {
            return MarkdownDocumentSerialization.Deserialize(
                (rUri, title, content, published, author) => new BlogPost(rUri, title, content, published, author),
                relativeUri,
                markdownWithFrontMatter,
                yamlMarkdown);
        }

        public static BlogPost DeserializeFromFile(string blogPostFile, DirectoryInfo publishedDirectory, IYamlMarkdown yamlMarkdown)
        {
            return MarkdownDocumentSerialization.DeserializeFromFile(
                (relativeUri, title, content, published, author) => new BlogPost(relativeUri, title, content, published, author),
                blogPostFile,
                publishedDirectory,
                yamlMarkdown);
        }
    }
}


text:
<?php

namespace Novosga;

use Exception;
use Novosga\Config\AppConfig;
use Novosga\Config\DatabaseConfig;
use Novosga\Service\AcessoService;
use Novosga\Util\I18n;

/**
 * Novo SGA App.
 *
 * @author Rogerio Lino <rogeriolino@gmail.com>
 */
class App extends \Slim\Slim
{
    const VERSION = '1.5.0';
    const CHARSET = 'utf-8';

    private $context;
    private $acessoService;

    private static $instance;

    public function __construct(array $userSettings = [])
    {
        $twig = new \Slim\Views\Twig();
        $userSettings = array_merge($userSettings, [
            'debug'          => NOVOSGA_DEV,
            'cache'          => NOVOSGA_CACHE,
            'templates.path' => NOVOSGA_TEMPLATES,
            'view'           => $twig,
        ]);
        if (!$userSettings['debug']) {
            $twig->parserOptions = [
                'cache' => $userSettings['cache'],
            ];
        }
        parent::__construct($userSettings);

        $this->view()->set('version', self::VERSION);

        $this->view()->parserExtensions = [
            new \Slim\Views\TwigExtension(),
            new \Twig_Extensions_Extension_I18n(),
            new Twig\Extensions(),
        ];

        if ($userSettings['debug']) {
            $this->view()->parserExtensions[] = new \Twig_Extension_Debug();
        }

        $app = $this;
        $app->notFound(function () use ($app) {
            $app->render(NOVOSGA_TEMPLATES.'/error/404.html.twig');
        });

        $app->error(function (\Exception $e) use ($app) {
            $app->view()->set('exception', $e);
            $app->render(NOVOSGA_TEMPLATES.'/error/500.html.twig');
        });
    }

    /**
     * @return App
     */
    public static function create(array $settings = [])
    {
        if (!self::$instance) {
            self::$instance = new self($settings);
        }

        return self::$instance;
    }

    public static function isInstalled()
    {
        return DatabaseConfig::getInstance()->isIntalled();
    }

    public function prepare()
    {
        // i18n
        I18n::bind();
        $this->view()->set('lang', I18n::lang());

        $db = DatabaseConfig::getInstance();
        $db->setDev(NOVOSGA_DEV);

        $this->context = new Context($this, $db);
        $this->acessoService = new AcessoService();

        $this->add(new \Novosga\Slim\InstallMiddleware($this->context));
        $this->add(new \Novosga\Slim\AuthMiddleware($this->context));
    }

    /**
     * @return Context
     */
    public function getContext()
    {
        return $this->context;
    }

    /**
     * @return AcessoService
     */
    public function getAcessoService()
    {
        return $this->acessoService;
    }

    public function gotoLogin()
    {
        $this->redirect($this->request()->getRootUri().'/login');
    }

    public function gotoHome()
    {
        $this->redirect($this->request()->getRootUri().'/home');
    }

    public function gotoModule()
    {
        $this->redirect($this->request()->getRootUri().'/modules/'.$this->getContext()->getModulo()->getChave());
    }

    public function moduleResource($moduleKey, $resource)
    {
        $filename = implode(DS, [
            MODULES_PATH, implode(DS, explode('.', $moduleKey)), 'public', $resource, ]
        );
        if (file_exists($filename)) {
            $mime = \Novosga\Util\FileUtils::mime($filename);
            header("Content-type: $mime");
            echo file_get_contents($filename);
            exit();
        } else {
            throw new Exception(sprintf('Resource not found: %s', $filename));
        }
    }

    /**
     * {@inheritdoc}
     */
    public function render($template, $data = [], $status = null)
    {
        $customTemplateDir = AppConfig::getInstance()->get('template.dir');
        if (!empty($customTemplateDir)) {
            array_unshift($this->view()->twigTemplateDirs, $customTemplateDir);
        }

        if (substr($template, 0, 1) === '/' || substr($template, 0, 2) === '..') {
            // defined a template outside the default twigTemplateDirs
            $dir = dirname($template);
            $this->view()->twigTemplateDirs[] = $dir;
            $template = basename($template);
        }
        // for security purposes allow only .html.twig files
        $ext = '.html.twig';
        if (substr($template, -strlen($ext)) !== $ext) {
            throw new Exception('Você está tentando exibir um arquivo de template inválido.');
        }

        return parent::render($template, $data, $status);
    }

    /**
     * @return \Novosga\Auth\AuthProviderFactory
     */
    public static function authenticationFactory()
    {
        $factory = null;
        $factoryClass = AppConfig::getInstance()->get('auth.factory');
        if (!empty($factoryClass) && class_exists($factoryClass)) {
            $factory = new $factoryClass();
        }
        if (!$factory || !($factory instanceof \Novosga\Auth\AuthProviderFactory)) {
            $factory = new \Novosga\Auth\DefaultAuthProviderFactory();
        }

        return $factory;
    }

    public static function defaultClientLanguage()
    {
        $lang = \Novosga\Util\I18n::locale();

        return current(explode('_', $lang));
    }

    /**
     * Retorna informacoes gerais sobre o sistema.
     */
    public static function info()
    {
        ob_start();
        phpinfo();
        $info = ob_get_contents();
        ob_end_clean();

        return $info;
    }
}


text:
package org.apache.lucene.search.spans;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.LowerCaseTokenizer;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Payload;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.payloads.PayloadHelper;
import org.apache.lucene.search.payloads.PayloadSpanUtil;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.LuceneTestCase;

public class TestPayloadSpans extends LuceneTestCase {
  private IndexSearcher searcher;
  private Similarity similarity = new DefaultSimilarity();
  protected IndexReader indexReader;

  public TestPayloadSpans(String s) {
    super(s);
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    PayloadHelper helper = new PayloadHelper();
    searcher = helper.setUp(similarity, 1000);
    indexReader = searcher.getIndexReader();
  }

  public void testSpanTermQuery() throws Exception {
    SpanTermQuery stq;
    Spans spans;
    stq = new SpanTermQuery(new Term(PayloadHelper.FIELD, "seventy"));
    spans = stq.getSpans(indexReader);
    assertTrue("spans is null and it shouldn't be", spans != null);
    checkSpans(spans, 100, 1, 1, 1);

    stq = new SpanTermQuery(new Term(PayloadHelper.NO_PAYLOAD_FIELD, "seventy"));
    spans = stq.getSpans(indexReader);
    assertTrue("spans is null and it shouldn't be", spans != null);
    checkSpans(spans, 100, 0, 0, 0);
  }

  public void testSpanFirst() throws IOException {

    SpanQuery match;
    SpanFirstQuery sfq;
    match = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one"));
    sfq = new SpanFirstQuery(match, 2);
    Spans spans = sfq.getSpans(indexReader);
    checkSpans(spans, 109, 1, 1, 1);
    //Test more complicated subclause
    SpanQuery[] clauses = new SpanQuery[2];
    clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one"));
    clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "hundred"));
    match = new SpanNearQuery(clauses, 0, true);
    sfq = new SpanFirstQuery(match, 2);
    checkSpans(sfq.getSpans(indexReader), 100, 2, 1, 1);

    match = new SpanNearQuery(clauses, 0, false);
    sfq = new SpanFirstQuery(match, 2);
    checkSpans(sfq.getSpans(indexReader), 100, 2, 1, 1);

  }

  public void testSpanNot() throws Exception {
    SpanQuery[] clauses = new SpanQuery[2];
    clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one"));
    clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "three"));
    SpanQuery spq = new SpanNearQuery(clauses, 5, true);
    SpanNotQuery snq = new SpanNotQuery(spq, new SpanTermQuery(new Term(PayloadHelper.FIELD, "two")));
    checkSpans(snq.getSpans(getSpanNotSearcher().getIndexReader()), 1,new int[]{2});
  }

  public IndexSearcher getSpanNotSearcher()
      throws IOException {
    RAMDirectory directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
        TEST_VERSION_CURRENT, new PayloadAnalyzer()).setSimilarity(
        similarity));

    Document doc = new Document();
    doc.add(new Field(PayloadHelper.FIELD, "one two three one four three",
        Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);

    writer.close();

    IndexSearcher searcher = new IndexSearcher(directory, true);
    searcher.setSimilarity(similarity);
    return searcher;

  }

  public void testNestedSpans() throws Exception {
    SpanTermQuery stq;
    Spans spans;
    IndexSearcher searcher = getSearcher();
    stq = new SpanTermQuery(new Term(PayloadHelper.FIELD, "mark"));
    spans = stq.getSpans(searcher.getIndexReader());
    assertTrue("spans is null and it shouldn't be", spans != null);
    checkSpans(spans, 0, null);

    SpanQuery[] clauses = new SpanQuery[3];
    clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "rr"));
    clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "yy"));
    clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "xx"));
    SpanNearQuery spanNearQuery = new SpanNearQuery(clauses, 12, false);

    spans = spanNearQuery.getSpans(searcher.getIndexReader());
    assertTrue("spans is null and it shouldn't be", spans != null);
    checkSpans(spans, 2, new int[]{3,3});

    clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "xx"));
    clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "rr"));
    clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "yy"));

    spanNearQuery = new SpanNearQuery(clauses, 6, true);

    spans = spanNearQuery.getSpans(searcher.getIndexReader());
    assertTrue("spans is null and it shouldn't be", spans != null);
    checkSpans(spans, 1, new int[]{3});

    clauses = new SpanQuery[2];

    clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "xx"));
    clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "rr"));

    spanNearQuery = new SpanNearQuery(clauses, 6, true);

    // xx within 6 of rr

    SpanQuery[] clauses2 = new SpanQuery[2];

    clauses2[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "yy"));
    clauses2[1] = spanNearQuery;

    SpanNearQuery nestedSpanNearQuery = new SpanNearQuery(clauses2, 6, false);

    // yy within 6 of xx within 6 of rr

    spans = nestedSpanNearQuery.getSpans(searcher.getIndexReader());
    assertTrue("spans is null and it shouldn't be", spans != null);
    checkSpans(spans, 2, new int[]{3,3});
  }

  public void testFirstClauseWithoutPayload() throws Exception {
    Spans spans;
    IndexSearcher searcher = getSearcher();

    SpanQuery[] clauses = new SpanQuery[3];
    clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "nopayload"));
    clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "qq"));
    clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "ss"));

    SpanNearQuery spanNearQuery = new SpanNearQuery(clauses, 6, true);

    SpanQuery[] clauses2 = new SpanQuery[2];

    clauses2[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "pp"));
    clauses2[1] = spanNearQuery;

    SpanNearQuery snq = new SpanNearQuery(clauses2, 6, false);

    SpanQuery[] clauses3 = new SpanQuery[2];

    clauses3[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "np"));
    clauses3[1] = snq;

    SpanNearQuery nestedSpanNearQuery = new SpanNearQuery(clauses3, 6, false);

    spans = nestedSpanNearQuery.getSpans(searcher.getIndexReader());
    assertTrue("spans is null and it shouldn't be", spans != null);
    checkSpans(spans, 1, new int[]{3});
  }

  public void testHeavilyNestedSpanQuery() throws Exception {
    Spans spans;
    IndexSearcher searcher = getSearcher();

    SpanQuery[] clauses = new SpanQuery[3];
    clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one"));
    clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "two"));
    clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "three"));

    SpanNearQuery spanNearQuery = new SpanNearQuery(clauses, 5, true);

    clauses = new SpanQuery[3];
    clauses[0] = spanNearQuery;
    clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "five"));
    clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "six"));

    SpanNearQuery spanNearQuery2 = new SpanNearQuery(clauses, 6, true);

    SpanQuery[] clauses2 = new SpanQuery[2];
    clauses2[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "eleven"));
    clauses2[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "ten"));
    SpanNearQuery spanNearQuery3 = new SpanNearQuery(clauses2, 2, false);

    SpanQuery[] clauses3 = new SpanQuery[3];
    clauses3[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "nine"));
    clauses3[1] = spanNearQuery2;
    clauses3[2] = spanNearQuery3;

    SpanNearQuery nestedSpanNearQuery = new SpanNearQuery(clauses3, 6, false);

    spans = nestedSpanNearQuery.getSpans(searcher.getIndexReader());
    assertTrue("spans is null and it shouldn't be", spans != null);
    checkSpans(spans, 2, new int[]{8, 8});
  }

  public void testShrinkToAfterShortestMatch() throws CorruptIndexException,
      LockObtainFailedException, IOException {
    RAMDirectory directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
        TEST_VERSION_CURRENT, new TestPayloadAnalyzer()));
    Document doc = new Document();
    doc.add(new Field("content", new StringReader("a b c d e f g h i j a k")));
    writer.addDocument(doc);
    writer.close();

    IndexSearcher is = new IndexSearcher(directory, true);

    SpanTermQuery stq1 = new SpanTermQuery(new Term("content", "a"));
    SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k"));
    SpanQuery[] sqs = { stq1, stq2 };
    SpanNearQuery snq = new SpanNearQuery(sqs, 1, true);
    Spans spans = snq.getSpans(is.getIndexReader());

    TopDocs topDocs = is.search(snq, 1);
    Set<String> payloadSet = new HashSet<String>();
    for (int i = 0; i < topDocs.scoreDocs.length; i++) {
      while (spans.next()) {
        Collection<byte[]> payloads = spans.getPayload();

        for (final byte [] payload : payloads) {
          payloadSet.add(new String(payload));
        }
      }
    }
    assertEquals(2, payloadSet.size());
    assertTrue(payloadSet.contains("a:Noise:10"));
    assertTrue(payloadSet.contains("k:Noise:11"));
  }

  public void testShrinkToAfterShortestMatch2() throws CorruptIndexException,
      LockObtainFailedException, IOException {
    RAMDirectory directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
        TEST_VERSION_CURRENT, new TestPayloadAnalyzer()));
    Document doc = new Document();
    doc.add(new Field("content", new StringReader("a b a d k f a h i k a k")));
    writer.addDocument(doc);
    writer.close();

    IndexSearcher is = new IndexSearcher(directory, true);

    SpanTermQuery stq1 = new SpanTermQuery(new Term("content", "a"));
    SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k"));
    SpanQuery[] sqs = { stq1, stq2 };
    SpanNearQuery snq = new SpanNearQuery(sqs, 0, true);
    Spans spans = snq.getSpans(is.getIndexReader());

    TopDocs topDocs = is.search(snq, 1);
    Set<String> payloadSet = new HashSet<String>();
    for (int i = 0; i < topDocs.scoreDocs.length; i++) {
      while (spans.next()) {
        Collection<byte[]> payloads = spans.getPayload();
        for (final byte[] payload : payloads) {
          payloadSet.add(new String(payload));
        }
      }
    }
    assertEquals(2, payloadSet.size());
    assertTrue(payloadSet.contains("a:Noise:10"));
    assertTrue(payloadSet.contains("k:Noise:11"));
  }

  public void testShrinkToAfterShortestMatch3() throws CorruptIndexException,
      LockObtainFailedException, IOException {
    RAMDirectory directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
        TEST_VERSION_CURRENT, new TestPayloadAnalyzer()));
    Document doc = new Document();
    doc.add(new Field("content", new StringReader("j k a l f k k p a t a k l k t a")));
    writer.addDocument(doc);
    writer.close();

    IndexSearcher is = new IndexSearcher(directory, true);

    SpanTermQuery stq1 = new SpanTermQuery(new Term("content", "a"));
    SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k"));
    SpanQuery[] sqs = { stq1, stq2 };
    SpanNearQuery snq = new SpanNearQuery(sqs, 0, true);
    Spans spans = snq.getSpans(is.getIndexReader());

    TopDocs topDocs = is.search(snq, 1);
    Set<String> payloadSet = new HashSet<String>();
    for (int i = 0; i < topDocs.scoreDocs.length; i++) {
      while (spans.next()) {
        Collection<byte[]> payloads = spans.getPayload();

        for (final byte [] payload : payloads) {
          payloadSet.add(new String(payload));
        }
      }
    }
    assertEquals(2, payloadSet.size());
    if(VERBOSE) {
      for (final String payload : payloadSet)
        System.out.println("match:" +  payload);

    }
    assertTrue(payloadSet.contains("a:Noise:10"));
    assertTrue(payloadSet.contains("k:Noise:11"));
  }

  public void testPayloadSpanUtil() throws Exception {
    RAMDirectory directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
        TEST_VERSION_CURRENT, new PayloadAnalyzer()).setSimilarity(
        similarity));
    Document doc = new Document();
    doc.add(new Field(PayloadHelper.FIELD,"xx rr yy mm  pp", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);

    writer.close();

    IndexSearcher searcher = new IndexSearcher(directory, true);

    IndexReader reader = searcher.getIndexReader();
    PayloadSpanUtil psu = new PayloadSpanUtil(reader);

    Collection<byte[]> payloads = psu.getPayloadsForQuery(new TermQuery(new Term(PayloadHelper.FIELD, "rr")));
    if(VERBOSE)
      System.out.println("Num payloads:" + payloads.size());
    for (final byte [] bytes : payloads) {
      if(VERBOSE)
        System.out.println(new String(bytes));
    }

  }

  private void checkSpans(Spans spans, int expectedNumSpans, int expectedNumPayloads,
                          int expectedPayloadLength, int expectedFirstByte) throws IOException {
    assertTrue("spans is null and it shouldn't be", spans != null);
    //each position match should have a span associated with it, since there is just one underlying term query, there should
    //only be one entry in the span
    int seen = 0;
    while (spans.next() == true)
    {
      //if we expect payloads, then isPayloadAvailable should be true
      if (expectedNumPayloads > 0) {
        assertTrue("isPayloadAvailable is not returning the correct value: " + spans.isPayloadAvailable()
                + " and it should be: " + (expectedNumPayloads >  0),
                spans.isPayloadAvailable() == true);
      } else {
        assertTrue("isPayloadAvailable should be false", spans.isPayloadAvailable() == false);
      }
      //See payload helper, for the PayloadHelper.FIELD field, there is a single byte payload at every token
      if (spans.isPayloadAvailable()) {
        Collection<byte[]> payload = spans.getPayload();
        assertTrue("payload Size: " + payload.size() + " is not: " + expectedNumPayloads, payload.size() == expectedNumPayloads);
        for (final byte [] thePayload : payload) {
          assertTrue("payload[0] Size: " + thePayload.length + " is not: " + expectedPayloadLength,
                  thePayload.length == expectedPayloadLength);
          assertTrue(thePayload[0] + " does not equal: " + expectedFirstByte, thePayload[0] == expectedFirstByte);

        }

      }
      seen++;
    }
    assertTrue(seen + " does not equal: " + expectedNumSpans, seen == expectedNumSpans);
  }

  private IndexSearcher getSearcher() throws Exception {
    RAMDirectory directory = new RAMDirectory();
    String[] docs = new String[]{"xx rr yy mm  pp","xx yy mm rr pp", "nopayload qq ss pp np", "one two three four five six seven eight nine ten eleven", "nine one two three four five six seven eight eleven ten"};
    IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
        TEST_VERSION_CURRENT, new PayloadAnalyzer()).setSimilarity(similarity));

    Document doc = null;
    for(int i = 0; i < docs.length; i++) {
      doc = new Document();
      String docText = docs[i];
      doc.add(new Field(PayloadHelper.FIELD,docText, Field.Store.YES, Field.Index.ANALYZED));
      writer.addDocument(doc);
    }

    writer.close();

    IndexSearcher searcher = new IndexSearcher(directory, true);
    return searcher;
  }

  private void checkSpans(Spans spans, int numSpans, int[] numPayloads) throws IOException {
    int cnt = 0;

    while (spans.next() == true) {
      if(VERBOSE)
        System.out.println("\nSpans Dump --");
      if (spans.isPayloadAvailable()) {
        Collection<byte[]> payload = spans.getPayload();
        if(VERBOSE)
          System.out.println("payloads for span:" + payload.size());
        for (final byte [] bytes : payload) {
          if(VERBOSE)
            System.out.println("doc:" + spans.doc() + " s:" + spans.start() + " e:" + spans.end() + " "
              + new String(bytes));
        }

        assertEquals(numPayloads[cnt],payload.size());
      } else {
        assertFalse("Expected spans:" + numPayloads[cnt] + " found: 0",numPayloads.length > 0 && numPayloads[cnt] > 0 );
      }
      cnt++;
    }

    assertEquals(numSpans, cnt);
  }

  class PayloadAnalyzer extends Analyzer {

    @Override
    public TokenStream tokenStream(String fieldName, Reader reader) {
      TokenStream result = new LowerCaseTokenizer(TEST_VERSION_CURRENT, reader);
      result = new PayloadFilter(result, fieldName);
      return result;
    }
  }

  class PayloadFilter extends TokenFilter {
    String fieldName;
    int numSeen = 0;
    Set<String> entities = new HashSet<String>();
    Set<String> nopayload = new HashSet<String>();
    int pos;
    PayloadAttribute payloadAtt;
    TermAttribute termAtt;
    PositionIncrementAttribute posIncrAtt;

    public PayloadFilter(TokenStream input, String fieldName) {
      super(input);
      this.fieldName = fieldName;
      pos = 0;
      entities.add("xx");
      entities.add("one");
      nopayload.add("nopayload");
      nopayload.add("np");
      termAtt = addAttribute(TermAttribute.class);
      posIncrAtt = addAttribute(PositionIncrementAttribute.class);
      payloadAtt = addAttribute(PayloadAttribute.class);
    }

    @Override
    public boolean incrementToken() throws IOException {
      if (input.incrementToken()) {
        String token = new String(termAtt.termBuffer(), 0, termAtt.termLength());

        if (!nopayload.contains(token)) {
          if (entities.contains(token)) {
            payloadAtt.setPayload(new Payload((token + ":Entity:"+ pos ).getBytes()));
          } else {
            payloadAtt.setPayload(new Payload((token + ":Noise:" + pos ).getBytes()));
          }
        }
        pos += posIncrAtt.getPositionIncrement();
        return true;
      }
      return false;
    }
  }

  public class TestPayloadAnalyzer extends Analyzer {

    @Override
    public TokenStream tokenStream(String fieldName, Reader reader) {
      TokenStream result = new LowerCaseTokenizer(TEST_VERSION_CURRENT, reader);
      result = new PayloadFilter(result, fieldName);
      return result;
    }
  }
}


text:
package os::linux::local::mode::listinterfaces;

use base qw(centreon::plugins::mode);

use strict;
use warnings;
use centreon::plugins::misc;

sub new {
    my ($class, %options) = @_;
    my $self = $class->SUPER::new(package => __PACKAGE__, %options);
    bless $self, $class;

    $self->{version} = '1.0';
    $options{options}->add_options(arguments =>
                                {
                                  "hostname:s"        => { name => 'hostname' },
                                  "remote"            => { name => 'remote' },
                                  "ssh-option:s@"     => { name => 'ssh_option' },
                                  "ssh-path:s"        => { name => 'ssh_path' },
                                  "ssh-command:s"     => { name => 'ssh_command', default => 'ssh' },
                                  "timeout:s"         => { name => 'timeout', default => 30 },
                                  "sudo"              => { name => 'sudo' },
                                  "command:s"         => { name => 'command', default => 'ifconfig' },
                                  "command-path:s"    => { name => 'command_path', default => '/sbin' },
                                  "command-options:s" => { name => 'command_options', default => '-a 2>&1' },
                                  "filter-name:s"     => { name => 'filter_name', },
                                  "filter-state:s"    => { name => 'filter_state', },
                                  "no-loopback"       => { name => 'no_loopback', },
                                  "skip-novalues"     => { name => 'skip_novalues', },
                                });
    $self->{result} = {};
    return $self;
}

sub check_options {
    my ($self, %options) = @_;
    $self->SUPER::init(%options);
}

sub manage_selection {
    my ($self, %options) = @_;

    my $stdout = centreon::plugins::misc::execute(output => $self->{output},
                                                  options => $self->{option_results},
                                                  sudo => $self->{option_results}->{sudo},
                                                  command => $self->{option_results}->{command},
                                                  command_path => $self->{option_results}->{command_path},
                                                  command_options => $self->{option_results}->{command_options});
    while ($stdout =~ /^(\S+)(.*?)(\n\n|\n$)/msg) {
        my ($interface_name, $values) = ($1, $2);
        $interface_name =~ s/:$//;
        my $states = '';
        $states .= 'R' if ($values =~ /RUNNING/ms);
        $states .= 'U' if ($values =~ /UP/ms);

        if (defined($self->{option_results}->{no_loopback}) && $values =~ /LOOPBACK/ms) {
            $self->{output}->output_add(long_msg => "Skipping interface '" . $interface_name . "': option --no-loopback");
            next;
        }
        if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
            $interface_name !~ /$self->{option_results}->{filter_name}/) {
            $self->{output}->output_add(long_msg => "Skipping interface '" . $interface_name . "': no matching filter name");
            next;
        }
        if (defined($self->{option_results}->{filter_state}) && $self->{option_results}->{filter_state} ne '' &&
            $states !~ /$self->{option_results}->{filter_state}/) {
            $self->{output}->output_add(long_msg => "Skipping interface '" . $interface_name . "': no matching filter state");
            next;
        }

        $values =~ /RX bytes:(\S+).*?TX bytes:(\S+)/msi;
        if (defined($self->{option_results}->{skip_novalues}) && !defined($1)) {
            $self->{output}->output_add(long_msg => "Skipping interface '" . $interface_name . "': no values");
            next;
        }
        $self->{result}->{$interface_name} = {state => $states};
    }
}

sub run {
    my ($self, %options) = @_;

    $self->manage_selection();
    foreach my $name (sort(keys %{$self->{result}})) {
        $self->{output}->output_add(long_msg => "'" . $name . "' [state = '" . $self->{result}->{$name}->{state} . "']");
    }

    $self->{output}->output_add(severity => 'OK',
                                short_msg => 'List interfaces:');
    $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
    $self->{output}->exit();
}

sub disco_format {
    my ($self, %options) = @_;

    $self->{output}->add_disco_format(elements => ['name', 'state']);
}

sub disco_show {
    my ($self, %options) = @_;

    $self->manage_selection();
    foreach my $name (sort(keys %{$self->{result}})) {
        $self->{output}->add_disco_entry(name => $name,
                                         state => $self->{result}->{$name}->{state}
                                         );
    }
}

1;

__END__

=head1 MODE

List storages.

=over 8

=item B<--remote>

Execute command remotely in 'ssh'.

=item B<--hostname>

Hostname to query (need --remote).

=item B<--ssh-option>

Specify multiple options like the user (example: --ssh-option='-l=centreon-engine' --ssh-option='-p=52').

=item B<--ssh-path>

Specify ssh command path (default: none)

=item B<--ssh-command>

Specify ssh command (default: 'ssh'). Useful to use 'plink'.

=item B<--timeout>

Timeout in seconds for the command (Default: 30).

=item B<--sudo>

Use 'sudo' to execute the command.

=item B<--command>

Command to get information (Default: 'ifconfig').
Can be changed if you have output in a file.

=item B<--command-path>

Command path (Default: '/sbin').

=item B<--command-options>

Command options (Default: '-a 2>&1').

=item B<--filter-name>

Filter interface name (regexp can be used).

=item B<--filter-state>

Filter state (regexp can be used).
Can be: 'R' (running), 'U' (up).

=item B<--no-loopback>

Don't display loopback interfaces.

=item B<--skip-novalues>

Filter interface without in/out byte values.

=back

=cut


text:
/**
 * Eric Meyer's Reset CSS v2.0 (http://meyerweb.com/eric/tools/css/reset/)
 * http://cssreset.com
 */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video, button, input {
	margin: 0;
	padding: 0;
	border: 0;
	vertical-align: baseline;

    font-family: GillSans, "Gill Sans MT", Calibri, sans-serif;
    font-size: 13px;
    font-weight: normal;
    letter-spacing: 0.1px;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, main {
	display: block;
}
html {
  	height: 100%;
}
body {
	line-height: 1;
	width: 100%;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}
a {
	color: inherit;
	text-decoration: none;
}


text:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.automation.models;

import com.azure.core.annotation.Fluent;
import com.fasterxml.jackson.annotation.JsonProperty;

/** Settings concerning key vault encryption for a configuration store. */
@Fluent
public final class KeyVaultProperties {
    /*
     * The URI of the key vault key used to encrypt data.
     */
    @JsonProperty(value = "keyvaultUri")
    private String keyvaultUri;

    /*
     * The name of key used to encrypt data.
     */
    @JsonProperty(value = "keyName")
    private String keyName;

    /*
     * The key version of the key used to encrypt data.
     */
    @JsonProperty(value = "keyVersion")
    private String keyVersion;

    /**
     * Get the keyvaultUri property: The URI of the key vault key used to encrypt data.
     *
     * @return the keyvaultUri value.
     */
    public String keyvaultUri() {
        return this.keyvaultUri;
    }

    /**
     * Set the keyvaultUri property: The URI of the key vault key used to encrypt data.
     *
     * @param keyvaultUri the keyvaultUri value to set.
     * @return the KeyVaultProperties object itself.
     */
    public KeyVaultProperties withKeyvaultUri(String keyvaultUri) {
        this.keyvaultUri = keyvaultUri;
        return this;
    }

    /**
     * Get the keyName property: The name of key used to encrypt data.
     *
     * @return the keyName value.
     */
    public String keyName() {
        return this.keyName;
    }

    /**
     * Set the keyName property: The name of key used to encrypt data.
     *
     * @param keyName the keyName value to set.
     * @return the KeyVaultProperties object itself.
     */
    public KeyVaultProperties withKeyName(String keyName) {
        this.keyName = keyName;
        return this;
    }

    /**
     * Get the keyVersion property: The key version of the key used to encrypt data.
     *
     * @return the keyVersion value.
     */
    public String keyVersion() {
        return this.keyVersion;
    }

    /**
     * Set the keyVersion property: The key version of the key used to encrypt data.
     *
     * @param keyVersion the keyVersion value to set.
     * @return the KeyVaultProperties object itself.
     */
    public KeyVaultProperties withKeyVersion(String keyVersion) {
        this.keyVersion = keyVersion;
        return this;
    }

    /**
     * Validates the instance.
     *
     * @throws IllegalArgumentException thrown if the instance is not valid.
     */
    public void validate() {
    }
}