text:
'use strict';

import paths = require('path');
import fs = require('fs');
import os = require('os');
import crypto = require('crypto');
import assert = require('assert');

import { isParent, FileOperation, FileOperationEvent, IContent, IFileService, IResolveFileOptions, IResolveFileResult, IResolveContentOptions, IFileStat, IStreamContent, FileOperationError, FileOperationResult, IUpdateContentOptions, FileChangeType, IImportResult, FileChangesEvent, ICreateFileOptions, IContentData } from 'vs/platform/files/common/files';
import { MAX_FILE_SIZE } from 'vs/platform/files/node/files';
import { isEqualOrParent } from 'vs/base/common/paths';
import { ResourceMap } from 'vs/base/common/map';
import arrays = require('vs/base/common/arrays');
import baseMime = require('vs/base/common/mime');
import { TPromise } from 'vs/base/common/winjs.base';
import objects = require('vs/base/common/objects');
import extfs = require('vs/base/node/extfs');
import { nfcall, ThrottledDelayer } from 'vs/base/common/async';
import uri from 'vs/base/common/uri';
import nls = require('vs/nls');
import { isWindows, isLinux } from 'vs/base/common/platform';
import { dispose, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import pfs = require('vs/base/node/pfs');
import encoding = require('vs/base/node/encoding');
import { detectMimeAndEncodingFromBuffer, IMimeAndEncoding } from 'vs/base/node/mime';
import flow = require('vs/base/node/flow');
import { FileWatcher as UnixWatcherService } from 'vs/workbench/services/files/node/watcher/unix/watcherService';
import { FileWatcher as WindowsWatcherService } from 'vs/workbench/services/files/node/watcher/win32/watcherService';
import { toFileChangesEvent, normalize, IRawFileChange } from 'vs/workbench/services/files/node/watcher/common';
import Event, { Emitter } from 'vs/base/common/event';
import { FileWatcher as NsfwWatcherService } from 'vs/workbench/services/files/node/watcher/nsfw/watcherService';
import { ITextResourceConfigurationService } from 'vs/editor/common/services/resourceConfiguration';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { ILifecycleService, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { getBaseLabel } from 'vs/base/common/labels';
import { assign } from 'vs/base/common/objects';

export interface IEncodingOverride {
	resource: uri;
	encoding: string;
}

export interface IFileServiceOptions {
	tmpDir?: string;
	errorLogger?: (msg: string) => void;
	encodingOverride?: IEncodingOverride[];
	watcherIgnoredPatterns?: string[];
	disableWatcher?: boolean;
	verboseLogging?: boolean;
	useExperimentalFileWatcher?: boolean;
	writeElevated?: (source: string, target: string) => TPromise<void>;
	elevationSupport?: {
		cliPath: string;
		promptTitle: string;
		promptIcnsPath?: string;
	};
}

function etag(stat: fs.Stats): string;
function etag(size: number, mtime: number): string;
function etag(arg1: any, arg2?: any): string {
	let size: number;
	let mtime: number;
	if (typeof arg2 === 'number') {
		size = arg1;
		mtime = arg2;
	} else {
		size = (<fs.Stats>arg1).size;
		mtime = (<fs.Stats>arg1).mtime.getTime();
	}

	return `"${crypto.createHash('sha1').update(String(size) + String(mtime)).digest('hex')}"`;
}

class BufferPool {

	static _64K = new BufferPool(64 * 1024, 5);

	constructor(
		readonly bufferSize: number,
		private readonly _capacity: number,
		private readonly _free: Buffer[] = [],
	) {
		//
	}

	acquire(): Buffer {
		if (this._free.length === 0) {
			return Buffer.allocUnsafe(this.bufferSize);
		} else {
			return this._free.shift();
		}
	}

	release(buf: Buffer): void {
		if (this._free.length <= this._capacity) {
			this._free.push(buf);
		}
	}
}

export class FileService implements IFileService {

	public _serviceBrand: any;

	private static readonly FS_EVENT_DELAY = 50; // aggregate and only emit events when changes have stopped for this duration (in ms)
	private static readonly FS_REWATCH_DELAY = 300; // delay to rewatch a file that was renamed or deleted (in ms)

	private tmpPath: string;
	private options: IFileServiceOptions;

	private _onFileChanges: Emitter<FileChangesEvent>;
	private _onAfterOperation: Emitter<FileOperationEvent>;

	private toDispose: IDisposable[];

	private activeFileChangesWatchers: ResourceMap<fs.FSWatcher>;
	private fileChangesWatchDelayer: ThrottledDelayer<void>;
	private undeliveredRawFileChangesEvents: IRawFileChange[];

	private activeWorkspaceFileChangeWatcher: IDisposable;

	constructor(
		private contextService: IWorkspaceContextService,
		private textResourceConfigurationService: ITextResourceConfigurationService,
		private configurationService: IConfigurationService,
		private lifecycleService: ILifecycleService,
		options: IFileServiceOptions
	) {
		this.toDispose = [];
		this.options = options || Object.create(null);
		this.tmpPath = this.options.tmpDir || os.tmpdir();

		this._onFileChanges = new Emitter<FileChangesEvent>();
		this.toDispose.push(this._onFileChanges);

		this._onAfterOperation = new Emitter<FileOperationEvent>();
		this.toDispose.push(this._onAfterOperation);

		if (!this.options.errorLogger) {
			this.options.errorLogger = console.error;
		}

		this.activeFileChangesWatchers = new ResourceMap<fs.FSWatcher>();
		this.fileChangesWatchDelayer = new ThrottledDelayer<void>(FileService.FS_EVENT_DELAY);
		this.undeliveredRawFileChangesEvents = [];

		lifecycleService.when(LifecyclePhase.Running).then(() => {
			this.setupFileWatching(); // wait until we are fully running before starting file watchers
		});

		this.registerListeners();
	}

	private registerListeners(): void {
		this.toDispose.push(this.contextService.onDidChangeWorkbenchState(() => {
			if (this.lifecycleService.phase === LifecyclePhase.Running) {
				this.setupFileWatching();
			}
		}));
	}

	public get onFileChanges(): Event<FileChangesEvent> {
		return this._onFileChanges.event;
	}

	public get onAfterOperation(): Event<FileOperationEvent> {
		return this._onAfterOperation.event;
	}

	public updateOptions(options: IFileServiceOptions): void {
		if (options) {
			objects.mixin(this.options, options); // overwrite current options
		}
	}

	private setupFileWatching(): void {

		// dispose old if any
		if (this.activeWorkspaceFileChangeWatcher) {
			this.activeWorkspaceFileChangeWatcher.dispose();
		}

		// Return if not aplicable
		const workbenchState = this.contextService.getWorkbenchState();
		if (workbenchState === WorkbenchState.EMPTY || this.options.disableWatcher) {
			return;
		}

		// new watcher: use it if setting tells us so or we run in multi-root environment
		if (this.options.useExperimentalFileWatcher || workbenchState === WorkbenchState.WORKSPACE) {
			this.activeWorkspaceFileChangeWatcher = toDisposable(this.setupNsfwWorkspaceWatching().startWatching());
		}

		// old watcher
		else {
			if (isWindows) {
				this.activeWorkspaceFileChangeWatcher = toDisposable(this.setupWin32WorkspaceWatching().startWatching());
			} else {
				this.activeWorkspaceFileChangeWatcher = toDisposable(this.setupUnixWorkspaceWatching().startWatching());
			}
		}
	}

	private setupWin32WorkspaceWatching(): WindowsWatcherService {
		return new WindowsWatcherService(this.contextService, this.options.watcherIgnoredPatterns, e => this._onFileChanges.fire(e), this.options.errorLogger, this.options.verboseLogging);
	}

	private setupUnixWorkspaceWatching(): UnixWatcherService {
		return new UnixWatcherService(this.contextService, this.options.watcherIgnoredPatterns, e => this._onFileChanges.fire(e), this.options.errorLogger, this.options.verboseLogging);
	}

	private setupNsfwWorkspaceWatching(): NsfwWatcherService {
		return new NsfwWatcherService(this.contextService, this.configurationService, e => this._onFileChanges.fire(e), this.options.errorLogger, this.options.verboseLogging);
	}

	public resolveFile(resource: uri, options?: IResolveFileOptions): TPromise<IFileStat> {
		return this.resolve(resource, options);
	}

	public resolveFiles(toResolve: { resource: uri, options?: IResolveFileOptions }[]): TPromise<IResolveFileResult[]> {
		return TPromise.join(toResolve.map(resourceAndOptions => this.resolve(resourceAndOptions.resource, resourceAndOptions.options)
			.then(stat => ({ stat, success: true }), error => ({ stat: void 0, success: false }))));
	}

	public existsFile(resource: uri): TPromise<boolean> {
		return this.resolveFile(resource).then(() => true, () => false);
	}

	public resolveContent(resource: uri, options?: IResolveContentOptions): TPromise<IContent> {
		return this.resolveStreamContent(resource, options).then(streamContent => {
			return new TPromise<IContent>((resolve, reject) => {

				const result: IContent = {
					resource: streamContent.resource,
					name: streamContent.name,
					mtime: streamContent.mtime,
					etag: streamContent.etag,
					encoding: streamContent.encoding,
					value: ''
				};

				streamContent.value.on('data', chunk => result.value += chunk);
				streamContent.value.on('error', err => reject(err));
				streamContent.value.on('end', _ => resolve(result));

				return result;
			});
		});
	}

	public resolveStreamContent(resource: uri, options?: IResolveContentOptions): TPromise<IStreamContent> {

		// Guard early against attempts to resolve an invalid file path
		if (resource.scheme !== 'file' || !resource.fsPath) {
			return TPromise.wrapError<IStreamContent>(new FileOperationError(
				nls.localize('fileInvalidPath', "Invalid file resource ({0})", resource.toString(true)),
				FileOperationResult.FILE_INVALID_PATH,
				options
			));
		}

		const result: IStreamContent = {
			resource: void 0,
			name: void 0,
			mtime: void 0,
			etag: void 0,
			encoding: void 0,
			value: void 0
		};

		const contentResolverToken = new CancellationTokenSource();

		const onStatError = error => {

			// error: stop reading the file the stat and content resolve call
			// usually race, mostly likely the stat call will win and cancel
			// the content call
			contentResolverToken.cancel();

			// forward error
			return TPromise.wrapError(error);
		};

		const statsPromise = this.resolveFile(resource).then(stat => {
			result.resource = stat.resource;
			result.name = stat.name;
			result.mtime = stat.mtime;
			result.etag = stat.etag;

			// Return early if resource is a directory
			if (stat.isDirectory) {
				return onStatError(new FileOperationError(
					nls.localize('fileIsDirectoryError', "File is directory"),
					FileOperationResult.FILE_IS_DIRECTORY,
					options
				));
			}

			// Return early if file not modified since
			if (options && options.etag && options.etag === stat.etag) {
				return onStatError(new FileOperationError(
					nls.localize('fileNotModifiedError', "File not modified since"),
					FileOperationResult.FILE_NOT_MODIFIED_SINCE,
					options
				));
			}

			// Return early if file is too large to load
			if (typeof stat.size === 'number' && stat.size > MAX_FILE_SIZE) {
				return onStatError(new FileOperationError(
					nls.localize('fileTooLargeError', "File too large to open"),
					FileOperationResult.FILE_TOO_LARGE,
					options
				));
			}

			return void 0;
		}, err => {

			// Wrap file not found errors
			if (err.code === 'ENOENT') {
				return onStatError(new FileOperationError(
					nls.localize('fileNotFoundError', "File not found ({0})", resource.toString(true)),
					FileOperationResult.FILE_NOT_FOUND,
					options
				));
			}

			return onStatError(err);
		});

		let completePromise: Thenable<any>;

		// await the stat iff we already have an etag so that we compare the
		// etag from the stat before we actually read the file again.
		if (options && options.etag) {
			completePromise = statsPromise.then(() => {
				return this.fillInContents(result, resource, options, contentResolverToken.token); // Waterfall -> only now resolve the contents
			});
		}

		// a fresh load without a previous etag which means we can resolve the file stat
		// and the content at the same time, avoiding the waterfall.
		else {
			completePromise = Promise.all([statsPromise, this.fillInContents(result, resource, options, contentResolverToken.token)]);
		}

		return TPromise.wrap(completePromise).then(() => result);
	}

	private fillInContents(content: IStreamContent, resource: uri, options: IResolveContentOptions, token: CancellationToken): Thenable<any> {
		return this.resolveFileData(resource, options, token).then(data => {
			content.encoding = data.encoding;
			content.value = data.stream;
		});
	}

	private resolveFileData(resource: uri, options: IResolveContentOptions, token: CancellationToken): Thenable<IContentData> {

		const chunkBuffer = BufferPool._64K.acquire();

		const result: IContentData = {
			encoding: void 0,
			stream: void 0
		};

		return new Promise<IContentData>((resolve, reject) => {
			fs.open(this.toAbsolutePath(resource), 'r', (err, fd) => {
				if (err) {
					if (err.code === 'ENOENT') {
						// Wrap file not found errors
						err = new FileOperationError(
							nls.localize('fileNotFoundError', "File not found ({0})", resource.toString(true)),
							FileOperationResult.FILE_NOT_FOUND,
							options
						);
					}

					return reject(err);
				}

				let decoder: NodeJS.ReadWriteStream;
				let totalBytesRead = 0;

				const finish = (err?: any) => {

					if (err) {
						if (err.code === 'EISDIR') {
							// Wrap EISDIR errors (fs.open on a directory works, but you cannot read from it)
							err = new FileOperationError(
								nls.localize('fileIsDirectoryError', "File is directory"),
								FileOperationResult.FILE_IS_DIRECTORY,
								options
							);
						}
						if (decoder) {
							// If the decoder already started, we have to emit the error through it as
							// event because the promise is already resolved!
							decoder.emit('error', err);
						} else {
							reject(err);
						}
					}
					if (decoder) {
						decoder.end();
					}

					// return the shared buffer
					BufferPool._64K.release(chunkBuffer);

					if (fd) {
						fs.close(fd, err => {
							if (err) {
								this.options.errorLogger(`resolveFileData#close(): ${err.toString()}`);
							}
						});
					}
				};

				const handleChunk = (bytesRead) => {
					if (token.isCancellationRequested) {
						// cancellation -> finish
						finish(new Error('cancelled'));
					} else if (bytesRead === 0) {
						// no more data -> finish
						finish();
					} else if (bytesRead < chunkBuffer.length) {
						// write the sub-part of data we received -> repeat
						decoder.write(chunkBuffer.slice(0, bytesRead), readChunk);
					} else {
						// write all data we received -> repeat
						decoder.write(chunkBuffer, readChunk);
					}
				};

				let currentPosition: number = (options && options.position) || null;

				const readChunk = () => {
					fs.read(fd, chunkBuffer, 0, chunkBuffer.length, currentPosition, (err, bytesRead) => {
						totalBytesRead += bytesRead;

						if (typeof currentPosition === 'number') {
							// if we received a position argument as option we need to ensure that
							// we advance the position by the number of bytesread
							currentPosition += bytesRead;
						}

						if (totalBytesRead > MAX_FILE_SIZE) {
							// stop when reading too much
							finish(new FileOperationError(
								nls.localize('fileTooLargeError', "File too large to open"),
								FileOperationResult.FILE_TOO_LARGE,
								options
							));
						} else if (err) {
							// some error happened
							finish(err);

						} else if (decoder) {
							// pass on to decoder
							handleChunk(bytesRead);

						} else {
							// when receiving the first chunk of data we need to create the
							// decoding stream which is then used to drive the string stream.
							TPromise.as(detectMimeAndEncodingFromBuffer(
								{ buffer: chunkBuffer, bytesRead },
								options && options.autoGuessEncoding || this.configuredAutoGuessEncoding(resource)
							)).then(value => {

								if (options && options.acceptTextOnly && value.mimes.indexOf(baseMime.MIME_BINARY) >= 0) {
									// Return error early if client only accepts text and this is not text
									finish(new FileOperationError(
										nls.localize('fileBinaryError', "File seems to be binary and cannot be opened as text"),
										FileOperationResult.FILE_IS_BINARY,
										options
									));

								} else {
									result.encoding = this.getEncoding(resource, this.getPeferredEncoding(resource, options, value));
									result.stream = decoder = encoding.decodeStream(result.encoding);
									resolve(result);
									handleChunk(bytesRead);
								}

							}).then(void 0, err => {
								// failed to get encoding
								finish(err);
							});
						}
					});
				};

				// start reading
				readChunk();
			});
		});
	}

	public updateContent(resource: uri, value: string, options: IUpdateContentOptions = Object.create(null)): TPromise<IFileStat> {
		if (this.options.elevationSupport && options.writeElevated) {
			return this.doUpdateContentElevated(resource, value, options);
		}

		return this.doUpdateContent(resource, value, options);
	}

	private doUpdateContent(resource: uri, value: string, options: IUpdateContentOptions = Object.create(null)): TPromise<IFileStat> {
		const absolutePath = this.toAbsolutePath(resource);

		// 1.) check file
		return this.checkFile(absolutePath, options).then(exists => {
			let createParentsPromise: TPromise<boolean>;
			if (exists) {
				createParentsPromise = TPromise.as(null);
			} else {
				createParentsPromise = pfs.mkdirp(paths.dirname(absolutePath));
			}

			// 2.) create parents as needed
			return createParentsPromise.then(() => {
				const encodingToWrite = this.getEncoding(resource, options.encoding);
				let addBomPromise: TPromise<boolean> = TPromise.as(false);

				// UTF_16 BE and LE as well as UTF_8 with BOM always have a BOM
				if (encodingToWrite === encoding.UTF16be || encodingToWrite === encoding.UTF16le || encodingToWrite === encoding.UTF8_with_bom) {
					addBomPromise = TPromise.as(true);
				}

				// Existing UTF-8 file: check for options regarding BOM
				else if (exists && encodingToWrite === encoding.UTF8) {
					if (options.overwriteEncoding) {
						addBomPromise = TPromise.as(false); // if we are to overwrite the encoding, we do not preserve it if found
					} else {
						addBomPromise = encoding.detectEncodingByBOM(absolutePath).then(enc => enc === encoding.UTF8); // otherwise preserve it if found
					}
				}

				// 3.) check to add UTF BOM
				return addBomPromise.then(addBom => {

					// 4.) set contents and resolve
					return this.doSetContentsAndResolve(resource, absolutePath, value, addBom, encodingToWrite).then(void 0, error => {
						if (!exists || error.code !== 'EPERM' || !isWindows) {
							return TPromise.wrapError(error);
						}

						// On Windows and if the file exists with an EPERM error, we try a different strategy of saving the file
						// by first truncating the file and then writing with r+ mode. This helps to save hidden files on Windows
						// (see https://github.com/Microsoft/vscode/issues/931)

						// 5.) truncate
						return pfs.truncate(absolutePath, 0).then(() => {

							// 6.) set contents (this time with r+ mode) and resolve again
							return this.doSetContentsAndResolve(resource, absolutePath, value, addBom, encodingToWrite, { flag: 'r+' });
						});
					});
				});
			});
		}).then(null, error => {
			if (error.code === 'EACCES' || error.code === 'EPERM') {
				return TPromise.wrapError(new FileOperationError(
					nls.localize('filePermission', "Permission denied writing to file ({0})", resource.toString(true)),
					FileOperationResult.FILE_PERMISSION_DENIED,
					options
				));
			}

			return TPromise.wrapError(error);
		});
	}

	private doSetContentsAndResolve(resource: uri, absolutePath: string, value: string, addBOM: boolean, encodingToWrite: string, options?: { mode?: number; flag?: string; }): TPromise<IFileStat> {
		let writeFilePromise: TPromise<void>;

		// Write fast if we do UTF 8 without BOM
		if (!addBOM && encodingToWrite === encoding.UTF8) {
			writeFilePromise = pfs.writeFile(absolutePath, value, options);
		}

		// Otherwise use encoding lib
		else {
			const encoded = encoding.encode(value, encodingToWrite, { addBOM });
			writeFilePromise = pfs.writeFile(absolutePath, encoded, options);
		}

		// set contents
		return writeFilePromise.then(() => {

			// resolve
			return this.resolve(resource);
		});
	}

	private doUpdateContentElevated(resource: uri, value: string, options: IUpdateContentOptions = Object.create(null)): TPromise<IFileStat> {
		const absolutePath = this.toAbsolutePath(resource);

		// 1.) check file
		return this.checkFile(absolutePath, options, options.overwriteReadonly /* ignore readonly if we overwrite readonly, this is handled via sudo later */).then(exists => {
			const writeOptions: IUpdateContentOptions = assign(Object.create(null), options);
			writeOptions.writeElevated = false;
			writeOptions.encoding = this.getEncoding(resource, options.encoding);

			// 2.) write to a temporary file to be able to copy over later
			const tmpPath = paths.join(this.tmpPath, `code-elevated-${Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 6)}`);
			return this.updateContent(uri.file(tmpPath), value, writeOptions).then(() => {

				// 3.) invoke our CLI as super user
				return (import('sudo-prompt')).then(sudoPrompt => {
					return new TPromise<void>((c, e) => {
						const promptOptions = { name: this.options.elevationSupport.promptTitle.replace('-', ''), icns: this.options.elevationSupport.promptIcnsPath };

						const sudoCommand: string[] = [`"${this.options.elevationSupport.cliPath}"`];
						if (options.overwriteReadonly) {
							sudoCommand.push('--file-chmod');
						}
						sudoCommand.push('--file-write', `"${tmpPath}"`, `"${absolutePath}"`);

						sudoPrompt.exec(sudoCommand.join(' '), promptOptions, (error: string, stdout: string, stderr: string) => {
							if (error || stderr) {
								e(error || stderr);
							} else {
								c(void 0);
							}
						});
					});
				}).then(() => {

					// 3.) delete temp file
					return pfs.del(tmpPath, this.tmpPath).then(() => {

						// 4.) resolve again
						return this.resolve(resource);
					});
				});
			});
		}).then(null, error => {
			if (this.options.verboseLogging) {
				this.options.errorLogger(`Unable to write to file '${resource.toString(true)}' as elevated user (${error})`);
			}

			if (!(error instanceof FileOperationError)) {
				error = new FileOperationError(
					nls.localize('filePermission', "Permission denied writing to file ({0})", resource.toString(true)),
					FileOperationResult.FILE_PERMISSION_DENIED,
					options
				);
			}

			return TPromise.wrapError(error);
		});
	}

	public createFile(resource: uri, content: string = '', options: ICreateFileOptions = Object.create(null)): TPromise<IFileStat> {
		const absolutePath = this.toAbsolutePath(resource);

		let checkFilePromise: TPromise<boolean>;
		if (options.overwrite) {
			checkFilePromise = TPromise.as(false);
		} else {
			checkFilePromise = pfs.exists(absolutePath);
		}

		// Check file exists
		return checkFilePromise.then(exists => {
			if (exists && !options.overwrite) {
				return TPromise.wrapError<IFileStat>(new FileOperationError(
					nls.localize('fileExists', "File to create already exists ({0})", resource.toString(true)),
					FileOperationResult.FILE_MODIFIED_SINCE,
					options
				));
			}

			// Create file
			return this.updateContent(resource, content).then(result => {

				// Events
				this._onAfterOperation.fire(new FileOperationEvent(resource, FileOperation.CREATE, result));

				return result;
			});
		});
	}

	public createFolder(resource: uri): TPromise<IFileStat> {

		// 1.) Create folder
		const absolutePath = this.toAbsolutePath(resource);
		return pfs.mkdirp(absolutePath).then(() => {

			// 2.) Resolve
			return this.resolve(resource).then(result => {

				// Events
				this._onAfterOperation.fire(new FileOperationEvent(resource, FileOperation.CREATE, result));

				return result;
			});
		});
	}

	public touchFile(resource: uri): TPromise<IFileStat> {
		const absolutePath = this.toAbsolutePath(resource);

		// 1.) check file
		return this.checkFile(absolutePath).then(exists => {
			let createPromise: TPromise<IFileStat>;
			if (exists) {
				createPromise = TPromise.as(null);
			} else {
				createPromise = this.createFile(resource);
			}

			// 2.) create file as needed
			return createPromise.then(() => {

				// 3.) update atime and mtime
				return pfs.touch(absolutePath).then(() => {

					// 4.) resolve
					return this.resolve(resource);
				});
			});
		});
	}

	public rename(resource: uri, newName: string): TPromise<IFileStat> {
		const newPath = paths.join(paths.dirname(resource.fsPath), newName);

		return this.moveFile(resource, uri.file(newPath));
	}

	public moveFile(source: uri, target: uri, overwrite?: boolean): TPromise<IFileStat> {
		return this.moveOrCopyFile(source, target, false, overwrite);
	}

	public copyFile(source: uri, target: uri, overwrite?: boolean): TPromise<IFileStat> {
		return this.moveOrCopyFile(source, target, true, overwrite);
	}

	private moveOrCopyFile(source: uri, target: uri, keepCopy: boolean, overwrite: boolean): TPromise<IFileStat> {
		const sourcePath = this.toAbsolutePath(source);
		const targetPath = this.toAbsolutePath(target);

		// 1.) move / copy
		return this.doMoveOrCopyFile(sourcePath, targetPath, keepCopy, overwrite).then(() => {

			// 2.) resolve
			return this.resolve(target).then(result => {

				// Events
				this._onAfterOperation.fire(new FileOperationEvent(source, keepCopy ? FileOperation.COPY : FileOperation.MOVE, result));

				return result;
			});
		});
	}

	private doMoveOrCopyFile(sourcePath: string, targetPath: string, keepCopy: boolean, overwrite: boolean): TPromise<boolean /* exists */> {

		// 1.) check if target exists
		return pfs.exists(targetPath).then(exists => {
			const isCaseRename = sourcePath.toLowerCase() === targetPath.toLowerCase();
			const isSameFile = sourcePath === targetPath;

			// Return early with conflict if target exists and we are not told to overwrite
			if (exists && !isCaseRename && !overwrite) {
				return TPromise.wrapError<boolean>(new FileOperationError(nls.localize('fileMoveConflict', "Unable to move/copy. File already exists at destination."), FileOperationResult.FILE_MOVE_CONFLICT));
			}

			// 2.) make sure target is deleted before we move/copy unless this is a case rename of the same file
			let deleteTargetPromise = TPromise.wrap<void>(void 0);
			if (exists && !isCaseRename) {
				if (isEqualOrParent(sourcePath, targetPath, !isLinux /* ignorecase */)) {
					return TPromise.wrapError<boolean>(new Error(nls.localize('unableToMoveCopyError', "Unable to move/copy. File would replace folder it is contained in."))); // catch this corner case!
				}

				deleteTargetPromise = this.del(uri.file(targetPath));
			}

			return deleteTargetPromise.then(() => {

				// 3.) make sure parents exists
				return pfs.mkdirp(paths.dirname(targetPath)).then(() => {

					// 4.) copy/move
					if (isSameFile) {
						return TPromise.wrap(null);
					} else if (keepCopy) {
						return nfcall(extfs.copy, sourcePath, targetPath);
					} else {
						return nfcall(extfs.mv, sourcePath, targetPath);
					}
				}).then(() => exists);
			});
		});
	}

	public importFile(source: uri, targetFolder: uri): TPromise<IImportResult> {
		const sourcePath = this.toAbsolutePath(source);
		const targetResource = uri.file(paths.join(targetFolder.fsPath, paths.basename(source.fsPath)));
		const targetPath = this.toAbsolutePath(targetResource);

		// 1.) resolve
		return pfs.stat(sourcePath).then(stat => {
			if (stat.isDirectory()) {
				return TPromise.wrapError<IImportResult>(new Error(nls.localize('foldersCopyError', "Folders cannot be copied into the workspace. Please select individual files to copy them."))); // for now we do not allow to import a folder into a workspace
			}

			// 2.) copy
			return this.doMoveOrCopyFile(sourcePath, targetPath, true, true).then(exists => {

				// 3.) resolve
				return this.resolve(targetResource).then(stat => {

					// Events
					this._onAfterOperation.fire(new FileOperationEvent(source, FileOperation.IMPORT, stat));

					return <IImportResult>{ isNew: !exists, stat: stat };
				});
			});
		});
	}

	public del(resource: uri): TPromise<void> {
		const absolutePath = this.toAbsolutePath(resource);

		return pfs.del(absolutePath, this.tmpPath).then(() => {

			// Events
			this._onAfterOperation.fire(new FileOperationEvent(resource, FileOperation.DELETE));
		});
	}

	// Helpers

	private toAbsolutePath(arg1: uri | IFileStat): string {
		let resource: uri;
		if (arg1 instanceof uri) {
			resource = <uri>arg1;
		} else {
			resource = (<IFileStat>arg1).resource;
		}

		assert.ok(resource && resource.scheme === 'file', 'Invalid resource: ' + resource);

		return paths.normalize(resource.fsPath);
	}

	private resolve(resource: uri, options: IResolveFileOptions = Object.create(null)): TPromise<IFileStat> {
		return this.toStatResolver(resource)
			.then(model => model.resolve(options));
	}

	private toStatResolver(resource: uri): TPromise<StatResolver> {
		const absolutePath = this.toAbsolutePath(resource);

		return pfs.stat(absolutePath).then(stat => {
			return new StatResolver(resource, stat.isDirectory(), stat.mtime.getTime(), stat.size, this.options.verboseLogging ? this.options.errorLogger : void 0);
		});
	}

	private getPeferredEncoding(resource: uri, options: IResolveContentOptions, detected: IMimeAndEncoding): string {
		let preferredEncoding: string;
		if (options && options.encoding) {
			if (detected.encoding === encoding.UTF8 && options.encoding === encoding.UTF8) {
				preferredEncoding = encoding.UTF8_with_bom; // indicate the file has BOM if we are to resolve with UTF 8
			} else {
				preferredEncoding = options.encoding; // give passed in encoding highest priority
			}
		} else if (detected.encoding) {
			if (detected.encoding === encoding.UTF8) {
				preferredEncoding = encoding.UTF8_with_bom; // if we detected UTF-8, it can only be because of a BOM
			} else {
				preferredEncoding = detected.encoding;
			}
		} else if (this.configuredEncoding(resource) === encoding.UTF8_with_bom) {
			preferredEncoding = encoding.UTF8; // if we did not detect UTF 8 BOM before, this can only be UTF 8 then
		}
		return preferredEncoding;
	}

	public getEncoding(resource: uri, preferredEncoding?: string): string {
		let fileEncoding: string;

		const override = this.getEncodingOverride(resource);
		if (override) {
			fileEncoding = override;
		} else if (preferredEncoding) {
			fileEncoding = preferredEncoding;
		} else {
			fileEncoding = this.configuredEncoding(resource);
		}

		if (!fileEncoding || !encoding.encodingExists(fileEncoding)) {
			fileEncoding = encoding.UTF8; // the default is UTF 8
		}

		return fileEncoding;
	}

	private configuredAutoGuessEncoding(resource: uri): boolean {
		return this.textResourceConfigurationService.getValue(resource, 'files.autoGuessEncoding');
	}

	private configuredEncoding(resource: uri): string {
		return this.textResourceConfigurationService.getValue(resource, 'files.encoding');
	}

	private getEncodingOverride(resource: uri): string {
		if (resource && this.options.encodingOverride && this.options.encodingOverride.length) {
			for (let i = 0; i < this.options.encodingOverride.length; i++) {
				const override = this.options.encodingOverride[i];

				// check if the resource is a child of the resource with override and use
				// the provided encoding in that case
				if (isParent(resource.fsPath, override.resource.fsPath, !isLinux /* ignorecase */)) {
					return override.encoding;
				}
			}
		}

		return null;
	}

	private checkFile(absolutePath: string, options: IUpdateContentOptions = Object.create(null), ignoreReadonly?: boolean): TPromise<boolean /* exists */> {
		return pfs.exists(absolutePath).then(exists => {
			if (exists) {
				return pfs.stat(absolutePath).then(stat => {
					if (stat.isDirectory()) {
						return TPromise.wrapError<boolean>(new Error('Expected file is actually a directory'));
					}

					// Dirty write prevention
					if (typeof options.mtime === 'number' && typeof options.etag === 'string' && options.mtime < stat.mtime.getTime()) {

						// Find out if content length has changed
						if (options.etag !== etag(stat.size, options.mtime)) {
							return TPromise.wrapError<boolean>(new FileOperationError(nls.localize('fileModifiedError', "File Modified Since"), FileOperationResult.FILE_MODIFIED_SINCE, options));
						}
					}

					// Throw if file is readonly and we are not instructed to overwrite
					if (!ignoreReadonly && !(stat.mode & 128) /* readonly */) {
						if (!options.overwriteReadonly) {
							return this.readOnlyError<boolean>(options);
						}

						// Try to change mode to writeable
						let mode = stat.mode;
						mode = mode | 128;
						return pfs.chmod(absolutePath, mode).then(() => {

							// Make sure to check the mode again, it could have failed
							return pfs.stat(absolutePath).then(stat => {
								if (!(stat.mode & 128) /* readonly */) {
									return this.readOnlyError<boolean>(options);
								}

								return exists;
							});
						});
					}

					return TPromise.as<boolean>(exists);
				});
			}

			return TPromise.as<boolean>(exists);
		});
	}

	private readOnlyError<T>(options: IUpdateContentOptions): TPromise<T> {
		return TPromise.wrapError<T>(new FileOperationError(
			nls.localize('fileReadOnlyError', "File is Read Only"),
			FileOperationResult.FILE_READ_ONLY,
			options
		));
	}

	public watchFileChanges(resource: uri): void {
		assert.ok(resource && resource.scheme === 'file', `Invalid resource for watching: ${resource}`);

		// Create or get watcher for provided path
		let watcher = this.activeFileChangesWatchers.get(resource);
		if (!watcher) {
			const fsPath = resource.fsPath;
			const fsName = paths.basename(resource.fsPath);

			try {
				watcher = extfs.watch(fsPath, (eventType: string, filename: string) => {
					const renamedOrDeleted = ((filename && filename !== fsName) || eventType === 'rename');

					// The file was either deleted or renamed. Many tools apply changes to files in an
					// atomic way ("Atomic Save") by first renaming the file to a temporary name and then
					// renaming it back to the original name. Our watcher will detect this as a rename
					// and then stops to work on Mac and Linux because the watcher is applied to the
					// inode and not the name. The fix is to detect this case and trying to watch the file
					// again after a certain delay.
					// In addition, we send out a delete event if after a timeout we detect that the file
					// does indeed not exist anymore.
					if (renamedOrDeleted) {

						// Very important to dispose the watcher which now points to a stale inode
						this.unwatchFileChanges(resource);

						// Wait a bit and try to install watcher again, assuming that the file was renamed quickly ("Atomic Save")
						setTimeout(() => {
							this.existsFile(resource).done(exists => {

								// File still exists, so reapply the watcher
								if (exists) {
									this.watchFileChanges(resource);
								}

								// File seems to be really gone, so emit a deleted event
								else {
									this.onRawFileChange({
										type: FileChangeType.DELETED,
										path: fsPath
									});
								}
							});
						}, FileService.FS_REWATCH_DELAY);
					}

					// Handle raw file change
					this.onRawFileChange({
						type: FileChangeType.UPDATED,
						path: fsPath
					});
				});
			} catch (error) {
				return; // the path might not exist anymore, ignore this error and return
			}

			this.activeFileChangesWatchers.set(resource, watcher);

			// Errors
			watcher.on('error', (error: string) => {
				this.options.errorLogger(error);
			});
		}
	}

	private onRawFileChange(event: IRawFileChange): void {

		// add to bucket of undelivered events
		this.undeliveredRawFileChangesEvents.push(event);

		if (this.options.verboseLogging) {
			console.log('%c[node.js Watcher]%c', 'color: green', 'color: black', event.type === FileChangeType.ADDED ? '[ADDED]' : event.type === FileChangeType.DELETED ? '[DELETED]' : '[CHANGED]', event.path);
		}

		// handle emit through delayer to accommodate for bulk changes
		this.fileChangesWatchDelayer.trigger(() => {
			const buffer = this.undeliveredRawFileChangesEvents;
			this.undeliveredRawFileChangesEvents = [];

			// Normalize
			const normalizedEvents = normalize(buffer);

			// Logging
			if (this.options.verboseLogging) {
				normalizedEvents.forEach(r => {
					console.log('%c[node.js Watcher]%c >> normalized', 'color: green', 'color: black', r.type === FileChangeType.ADDED ? '[ADDED]' : r.type === FileChangeType.DELETED ? '[DELETED]' : '[CHANGED]', r.path);
				});
			}

			// Emit
			this._onFileChanges.fire(toFileChangesEvent(normalizedEvents));

			return TPromise.as(null);
		});
	}

	public unwatchFileChanges(resource: uri): void {
		const watcher = this.activeFileChangesWatchers.get(resource);
		if (watcher) {
			watcher.close();
			this.activeFileChangesWatchers.delete(resource);
		}
	}

	public dispose(): void {
		this.toDispose = dispose(this.toDispose);

		if (this.activeWorkspaceFileChangeWatcher) {
			this.activeWorkspaceFileChangeWatcher.dispose();
			this.activeWorkspaceFileChangeWatcher = null;
		}

		this.activeFileChangesWatchers.forEach(watcher => watcher.close());
		this.activeFileChangesWatchers.clear();
	}
}

export class StatResolver {
	private resource: uri;
	private isDirectory: boolean;
	private mtime: number;
	private name: string;
	private etag: string;
	private size: number;
	private errorLogger: (msg) => void;

	constructor(resource: uri, isDirectory: boolean, mtime: number, size: number, errorLogger?: (msg) => void) {
		assert.ok(resource && resource.scheme === 'file', 'Invalid resource: ' + resource);

		this.resource = resource;
		this.isDirectory = isDirectory;
		this.mtime = mtime;
		this.name = getBaseLabel(resource);
		this.etag = etag(size, mtime);
		this.size = size;

		this.errorLogger = errorLogger;
	}

	public resolve(options: IResolveFileOptions): TPromise<IFileStat> {

		// General Data
		const fileStat: IFileStat = {
			resource: this.resource,
			isDirectory: this.isDirectory,
			hasChildren: undefined,
			name: this.name,
			etag: this.etag,
			size: this.size,
			mtime: this.mtime
		};

		// File Specific Data
		if (!this.isDirectory) {
			return TPromise.as(fileStat);
		}

		// Directory Specific Data
		else {

			// Convert the paths from options.resolveTo to absolute paths
			let absoluteTargetPaths: string[] = null;
			if (options && options.resolveTo) {
				absoluteTargetPaths = [];
				options.resolveTo.forEach(resource => {
					absoluteTargetPaths.push(resource.fsPath);
				});
			}

			return new TPromise<IFileStat>((c, e) => {

				// Load children
				this.resolveChildren(this.resource.fsPath, absoluteTargetPaths, options && options.resolveSingleChildDescendants, children => {
					children = arrays.coalesce(children); // we don't want those null children (could be permission denied when reading a child)
					fileStat.hasChildren = children && children.length > 0;
					fileStat.children = children || [];

					c(fileStat);
				});
			});
		}
	}

	private resolveChildren(absolutePath: string, absoluteTargetPaths: string[], resolveSingleChildDescendants: boolean, callback: (children: IFileStat[]) => void): void {
		extfs.readdir(absolutePath, (error: Error, files: string[]) => {
			if (error) {
				if (this.errorLogger) {
					this.errorLogger(error);
				}

				return callback(null); // return - we might not have permissions to read the folder
			}

			// for each file in the folder
			flow.parallel(files, (file: string, clb: (error: Error, children: IFileStat) => void) => {
				const fileResource = uri.file(paths.resolve(absolutePath, file));
				let fileStat: fs.Stats;
				const $this = this;

				flow.sequence(
					function onError(error: Error): void {
						if ($this.errorLogger) {
							$this.errorLogger(error);
						}

						clb(null, null); // return - we might not have permissions to read the folder or stat the file
					},

					function stat(this: any): void {
						fs.stat(fileResource.fsPath, this);
					},

					function countChildren(this: any, fsstat: fs.Stats): void {
						fileStat = fsstat;

						if (fileStat.isDirectory()) {
							extfs.readdir(fileResource.fsPath, (error, result) => {
								this(null, result ? result.length : 0);
							});
						} else {
							this(null, 0);
						}
					},

					function resolve(childCount: number): void {
						const childStat: IFileStat = {
							resource: fileResource,
							isDirectory: fileStat.isDirectory(),
							hasChildren: childCount > 0,
							name: file,
							mtime: fileStat.mtime.getTime(),
							etag: etag(fileStat),
							size: fileStat.size
						};

						// Return early for files
						if (!fileStat.isDirectory()) {
							return clb(null, childStat);
						}

						// Handle Folder
						let resolveFolderChildren = false;
						if (files.length === 1 && resolveSingleChildDescendants) {
							resolveFolderChildren = true;
						} else if (childCount > 0 && absoluteTargetPaths && absoluteTargetPaths.some(targetPath => isEqualOrParent(targetPath, fileResource.fsPath, !isLinux /* ignorecase */))) {
							resolveFolderChildren = true;
						}

						// Continue resolving children based on condition
						if (resolveFolderChildren) {
							$this.resolveChildren(fileResource.fsPath, absoluteTargetPaths, resolveSingleChildDescendants, children => {
								children = arrays.coalesce(children);  // we don't want those null children
								childStat.hasChildren = children && children.length > 0;
								childStat.children = children || [];

								clb(null, childStat);
							});
						}

						// Otherwise return result
						else {
							clb(null, childStat);
						}
					});
			}, (errors, result) => {
				callback(result);
			});
		});
	}
}


text:
<CrawlItem>
  <Text>Hladna osrednja pizza.</Text>
  <Rating>2</Rating>
  <Source>http://pauza.hr/menu/pizzeria-zagabria</Source>
  <Sentences>
    <SentenceInfo>
      <Text>hladna osrednja pizza . </Text>
      <StartPosition>0</StartPosition>
      <TaggedWords>
        <CROTaggedWord>
          <Word>hladna</Word>
          <Lemma>hladan</Lemma>
          <MolexLemmas>
            <string>hladan</string>
          </MolexLemmas>
          <POSTag>A</POSTag>
          <BasicStem>hladn</BasicStem>
          <MSDs>
            <string>Afpfsnn</string>
            <string>Afpfsny</string>
            <string>Afpfsvn</string>
            <string>Afpfsvy</string>
            <string>Afpmsgn</string>
            <string>Afpnpan</string>
            <string>Afpnpay</string>
            <string>Afpnpnn</string>
            <string>Afpnpny</string>
            <string>Afpnpvn</string>
            <string>Afpnpvy</string>
            <string>Afpnsgn</string>
          </MSDs>
          <Position>0</Position>
          <SentenceIndex>0</SentenceIndex>
        </CROTaggedWord>
        <CROTaggedWord>
          <Word>osrednja</Word>
          <Lemma>osrednji</Lemma>
          <MolexLemmas>
            <string>osrednji</string>
          </MolexLemmas>
          <POSTag>A</POSTag>
          <BasicStem>osrednj</BasicStem>
          <MSDs>
            <string>Afpfsny</string>
            <string>Afpfsvy</string>
            <string>Afpnpay</string>
            <string>Afpnpny</string>
            <string>Afpnpvy</string>
          </MSDs>
          <Position>7</Position>
          <SentenceIndex>1</SentenceIndex>
        </CROTaggedWord>
        <CROTaggedWord>
          <Word>pizza</Word>
          <Lemma>pizza</Lemma>
          <MolexLemmas>
            <string>pizza</string>
          </MolexLemmas>
          <POSTag>N</POSTag>
          <BasicStem>pizz</BasicStem>
          <MSDs>
            <string>N=fpg</string>
            <string>N=fsn</string>
          </MSDs>
          <Position>16</Position>
          <SentenceIndex>2</SentenceIndex>
        </CROTaggedWord>
        <CROTaggedWord>
          <Word>.</Word>
          <Lemma>.</Lemma>
          <MolexLemmas>
            <string>.</string>
          </MolexLemmas>
          <POSTag>Z</POSTag>
          <BasicStem>.</BasicStem>
          <MSDs />
          <Position>22</Position>
          <SentenceIndex>3</SentenceIndex>
        </CROTaggedWord>
      </TaggedWords>
      <DependencyRelations>
        <DependencyRelation>
          <Governor>
            <Word>pizza</Word>
            <Lemma>pizza</Lemma>
            <MolexLemmas>
              <string>pizza</string>
            </MolexLemmas>
            <POSTag>N</POSTag>
            <BasicStem>pizz</BasicStem>
            <MSDs>
              <string>N=fpg</string>
              <string>N=fsn</string>
            </MSDs>
            <Position>16</Position>
            <SentenceIndex>2</SentenceIndex>
          </Governor>
          <Dependent>
            <Word>hladna</Word>
            <Lemma>hladan</Lemma>
            <MolexLemmas>
              <string>hladan</string>
            </MolexLemmas>
            <POSTag>A</POSTag>
            <BasicStem>hladn</BasicStem>
            <MSDs>
              <string>Afpfsnn</string>
              <string>Afpfsny</string>
              <string>Afpfsvn</string>
              <string>Afpfsvy</string>
              <string>Afpmsgn</string>
              <string>Afpnpan</string>
              <string>Afpnpay</string>
              <string>Afpnpnn</string>
              <string>Afpnpny</string>
              <string>Afpnpvn</string>
              <string>Afpnpvy</string>
              <string>Afpnsgn</string>
            </MSDs>
            <Position>0</Position>
            <SentenceIndex>0</SentenceIndex>
          </Dependent>
          <Relation>Atr</Relation>
        </DependencyRelation>
        <DependencyRelation>
          <Governor>
            <Word>pizza</Word>
            <Lemma>pizza</Lemma>
            <MolexLemmas>
              <string>pizza</string>
            </MolexLemmas>
            <POSTag>N</POSTag>
            <BasicStem>pizz</BasicStem>
            <MSDs>
              <string>N=fpg</string>
              <string>N=fsn</string>
            </MSDs>
            <Position>16</Position>
            <SentenceIndex>2</SentenceIndex>
          </Governor>
          <Dependent>
            <Word>osrednja</Word>
            <Lemma>osrednji</Lemma>
            <MolexLemmas>
              <string>osrednji</string>
            </MolexLemmas>
            <POSTag>A</POSTag>
            <BasicStem>osrednj</BasicStem>
            <MSDs>
              <string>Afpfsny</string>
              <string>Afpfsvy</string>
              <string>Afpnpay</string>
              <string>Afpnpny</string>
              <string>Afpnpvy</string>
            </MSDs>
            <Position>7</Position>
            <SentenceIndex>1</SentenceIndex>
          </Dependent>
          <Relation>Atr</Relation>
        </DependencyRelation>
        <DependencyRelation>
          <Governor>
            <Word>.</Word>
            <Lemma>.</Lemma>
            <MolexLemmas>
              <string>.</string>
            </MolexLemmas>
            <POSTag>Z</POSTag>
            <BasicStem>.</BasicStem>
            <MSDs />
            <Position>22</Position>
            <SentenceIndex>3</SentenceIndex>
          </Governor>
          <Dependent>
            <Word>pizza</Word>
            <Lemma>pizza</Lemma>
            <MolexLemmas>
              <string>pizza</string>
            </MolexLemmas>
            <POSTag>N</POSTag>
            <BasicStem>pizz</BasicStem>
            <MSDs>
              <string>N=fpg</string>
              <string>N=fsn</string>
            </MSDs>
            <Position>16</Position>
            <SentenceIndex>2</SentenceIndex>
          </Dependent>
          <Relation>Punc</Relation>
        </DependencyRelation>
      </DependencyRelations>
    </SentenceInfo>
  </Sentences>
  <TaggedWords>
    <CROTaggedWord>
      <Word>hladna</Word>
      <Lemma>hladan</Lemma>
      <MolexLemmas>
        <string>hladan</string>
      </MolexLemmas>
      <POSTag>A</POSTag>
      <BasicStem>hladn</BasicStem>
      <MSDs>
        <string>Afpfsnn</string>
        <string>Afpfsny</string>
        <string>Afpfsvn</string>
        <string>Afpfsvy</string>
        <string>Afpmsgn</string>
        <string>Afpnpan</string>
        <string>Afpnpay</string>
        <string>Afpnpnn</string>
        <string>Afpnpny</string>
        <string>Afpnpvn</string>
        <string>Afpnpvy</string>
        <string>Afpnsgn</string>
      </MSDs>
      <Position>0</Position>
      <SentenceIndex>0</SentenceIndex>
    </CROTaggedWord>
    <CROTaggedWord>
      <Word>osrednja</Word>
      <Lemma>osrednji</Lemma>
      <MolexLemmas>
        <string>osrednji</string>
      </MolexLemmas>
      <POSTag>A</POSTag>
      <BasicStem>osrednj</BasicStem>
      <MSDs>
        <string>Afpfsny</string>
        <string>Afpfsvy</string>
        <string>Afpnpay</string>
        <string>Afpnpny</string>
        <string>Afpnpvy</string>
      </MSDs>
      <Position>7</Position>
      <SentenceIndex>1</SentenceIndex>
    </CROTaggedWord>
    <CROTaggedWord>
      <Word>pizza</Word>
      <Lemma>pizza</Lemma>
      <MolexLemmas>
        <string>pizza</string>
      </MolexLemmas>
      <POSTag>N</POSTag>
      <BasicStem>pizz</BasicStem>
      <MSDs>
        <string>N=fpg</string>
        <string>N=fsn</string>
      </MSDs>
      <Position>16</Position>
      <SentenceIndex>2</SentenceIndex>
    </CROTaggedWord>
    <CROTaggedWord>
      <Word>.</Word>
      <Lemma>.</Lemma>
      <MolexLemmas>
        <string>.</string>
      </MolexLemmas>
      <POSTag>Z</POSTag>
      <BasicStem>.</BasicStem>
      <MSDs />
      <Position>22</Position>
      <SentenceIndex>3</SentenceIndex>
    </CROTaggedWord>
  </TaggedWords>
</CrawlItem>

redpajama_set_name: RedPajamaGithub


text:
/*******************************************************************************
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
#ifndef IM2CHARRECTSCHARDATAREADER_HPP_
#define IM2CHARRECTSCHARDATAREADER_HPP_

#include "xml/xml.h"
#include "xml/Im2CharRectsXMLOutputHandler.hpp"

#include "CharDataReaderI.hpp"

namespace impact_tr4
{

class Im2CharRectsCharDataReader : public CharDataReaderI
{
public:
	Im2CharRectsCharDataReader() : CharDataReaderI() {};
	virtual ~Im2CharRectsCharDataReader() {}

//	void parseFiles(Document *pDocument, std::vector<ImageChar> &imageCharVec, std::vector<std::string> &charVec) const;
	void parseSingleImageFile(const std::string fileName, const std::string resultFileName, GrayImage<> *pImage, std::vector<ImageChar> &imageCharVec, int imageId = 0) const;
	const std::string fileExtension() const { return std::string("xml"); }
};

} // end of namespace impact_tr4

#endif /*IM2CHARRECTSCHARDATAREADER_HPP_*/


text:
layout:    page
title:     WEEKLIES
permalink: /weeklys/
---

In 2017/18, I  uploaded something to Soundcloud every week. This turned out to be the best way for me to get better at the music I make, because otherwise I'd never finish anything, ever. It forced me to waste less time and focus my workflow, and after a month I ended up with some cool samples that were created in the process as well.

I stopped to focus more on full releases, but the tracks are still on Soundcloud:


text:
<?php

namespace App\Fixture\Factory;

use App\Fixture\OptionsResolver\LazyOption;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Review\Factory\ReviewFactoryInterface;
use Sylius\Component\Review\Model\ReviewInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
 */
class ProductReviewExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
{
    /**
     * @var ReviewFactoryInterface
     */
    private $productReviewFactory;

    /**
     * @var RepositoryInterface
     */
    private $productRepository;

    /**
     * @var RepositoryInterface
     */
    private $customerRepository;

    /**
     * @var \Faker\Generator
     */
    private $faker;

    /**
     * @var OptionsResolver
     */
    private $optionsResolver;

    /**
     * @param ReviewFactoryInterface $productReviewFactory
     * @param RepositoryInterface    $productRepository
     * @param RepositoryInterface    $customerRepository
     */
    public function __construct(
        ReviewFactoryInterface $productReviewFactory,
        RepositoryInterface $productRepository,
        RepositoryInterface $customerRepository
    ) {
        $this->productReviewFactory = $productReviewFactory;
        $this->productRepository = $productRepository;
        $this->customerRepository = $customerRepository;

        $this->faker = \Faker\Factory::create();
        $this->optionsResolver = new OptionsResolver();

        $this->configureOptions($this->optionsResolver);
    }

    /**
     * {@inheritdoc}
     */
    public function create(array $options = [])
    {
        $options = $this->optionsResolver->resolve($options);

        /** @var ReviewInterface $productReview */
        $productReview = $this->productReviewFactory->createForSubjectWithReviewer(
            $options['product'],
            $options['author']
        );
        $productReview->setTitle($options['title']);
        $productReview->setComment($options['comment']);
        $productReview->setRating($options['rating']);
        $options['product']->addReview($productReview);

        return $productReview;
    }

    /**
     * {@inheritdoc}
     */
    protected function configureOptions(OptionsResolver $resolver)
    {
        $resolver
            ->setDefault('title', function (Options $options) {
                return $this->faker->words(3, true);
            })
            ->setDefault('rating', function (Options $options) {
                return $this->faker->numberBetween(1, 10);
            })
            ->setDefault('comment', function (Options $options) {
                return $this->faker->sentences(3, true);
            })
            ->setDefault('author', LazyOption::randomOne($this->customerRepository))
            ->setNormalizer('author', LazyOption::findOneBy($this->customerRepository, 'email'))
            ->setDefault('product', LazyOption::randomOne($this->productRepository))
            ->setNormalizer('product', LazyOption::findOneBy($this->productRepository, 'code'))
        ;
    }
}


text:
package com.ryg.dynamicload;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.WindowManager.LayoutParams;

import com.ryg.dynamicload.internal.DLAttachable;
import com.ryg.dynamicload.internal.DLPluginManager;
import com.ryg.dynamicload.internal.DLProxyImpl;

public class DLProxyActivity extends Activity implements DLAttachable {

    protected DLPlugin mRemoteActivity;
    private DLProxyImpl impl = new DLProxyImpl(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        impl.onCreate(getIntent());
    }

    @Override
    public void attach(DLPlugin remoteActivity, DLPluginManager pluginManager) {
        mRemoteActivity = remoteActivity;
    }

    @Override
    public AssetManager getAssets() {
        return impl.getAssets() == null ? super.getAssets() : impl.getAssets();
    }

    @Override
    public Resources getResources() {
        return impl.getResources() == null ? super.getResources() : impl.getResources();
    }

    @Override
    public Theme getTheme() {
        return impl.getTheme() == null ? super.getTheme() : impl.getTheme();
    }

    @Override
    public ClassLoader getClassLoader() {
        return impl.getClassLoader();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mRemoteActivity.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onStart() {
        mRemoteActivity.onStart();
        super.onStart();
    }

    @Override
    protected void onRestart() {
        mRemoteActivity.onRestart();
        super.onRestart();
    }

    @Override
    protected void onResume() {
        mRemoteActivity.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mRemoteActivity.onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        mRemoteActivity.onStop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mRemoteActivity.onDestroy();
        super.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        mRemoteActivity.onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        mRemoteActivity.onRestoreInstanceState(savedInstanceState);
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        mRemoteActivity.onNewIntent(intent);
        super.onNewIntent(intent);
    }

    @Override
    public void onBackPressed() {
        mRemoteActivity.onBackPressed();
        super.onBackPressed();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        return mRemoteActivity.onTouchEvent(event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        super.onKeyUp(keyCode, event);
        return mRemoteActivity.onKeyUp(keyCode, event);
    }

    @Override
    public void onWindowAttributesChanged(LayoutParams params) {
        mRemoteActivity.onWindowAttributesChanged(params);
        super.onWindowAttributesChanged(params);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        mRemoteActivity.onWindowFocusChanged(hasFocus);
        super.onWindowFocusChanged(hasFocus);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mRemoteActivity.onCreateOptionsMenu(menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        mRemoteActivity.onOptionsItemSelected(item);
        return super.onOptionsItemSelected(item);
    }

    @Override
    public ComponentName startService(Intent service) {
        return super.startService(service);
    }

}


text:
package org.apache.flink.kubernetes.kubeclient.factory;

import org.apache.flink.kubernetes.KubernetesTestUtils;
import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions;
import org.apache.flink.kubernetes.configuration.KubernetesConfigOptionsInternal;
import org.apache.flink.kubernetes.entrypoint.KubernetesSessionClusterEntrypoint;
import org.apache.flink.kubernetes.kubeclient.KubernetesJobManagerSpecification;
import org.apache.flink.kubernetes.kubeclient.KubernetesJobManagerTestBase;
import org.apache.flink.kubernetes.kubeclient.decorators.ExternalServiceDecorator;
import org.apache.flink.kubernetes.kubeclient.decorators.FlinkConfMountDecorator;
import org.apache.flink.kubernetes.kubeclient.decorators.HadoopConfMountDecorator;
import org.apache.flink.kubernetes.kubeclient.decorators.InternalServiceDecorator;
import org.apache.flink.kubernetes.kubeclient.parameters.KubernetesJobManagerParameters;
import org.apache.flink.kubernetes.utils.Constants;
import org.apache.flink.kubernetes.utils.KubernetesUtils;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.PodSpec;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.apache.flink.configuration.GlobalConfiguration.FLINK_CONF_FILENAME;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
 * General tests for the {@link KubernetesJobManagerFactory}.
 */
public class KubernetesJobManagerFactoryTest extends KubernetesJobManagerTestBase {

	private static final String SERVICE_ACCOUNT_NAME = "service-test";
	private static final String ENTRY_POINT_CLASS = KubernetesSessionClusterEntrypoint.class.getCanonicalName();

	private static final String EXISTING_HADOOP_CONF_CONFIG_MAP = "hadoop-conf";

	protected KubernetesJobManagerSpecification kubernetesJobManagerSpecification;

	@Override
	protected void setupFlinkConfig() {
		super.setupFlinkConfig();

		flinkConfig.set(KubernetesConfigOptionsInternal.ENTRY_POINT_CLASS, ENTRY_POINT_CLASS);
		flinkConfig.set(KubernetesConfigOptions.JOB_MANAGER_SERVICE_ACCOUNT, SERVICE_ACCOUNT_NAME);
	}

	@Override
	protected void onSetup() throws Exception {
		super.onSetup();

		KubernetesTestUtils.createTemporyFile("some data", flinkConfDir, "logback.xml");
		KubernetesTestUtils.createTemporyFile("some data", flinkConfDir, "log4j.properties");

		this.kubernetesJobManagerSpecification =
			KubernetesJobManagerFactory.buildKubernetesJobManagerSpecification(kubernetesJobManagerParameters);
	}

	@Test
	public void testDeploymentMetadata() {
		final Deployment resultDeployment = this.kubernetesJobManagerSpecification.getDeployment();
		assertEquals(Constants.APPS_API_VERSION, resultDeployment.getApiVersion());
		assertEquals(KubernetesUtils.getDeploymentName(CLUSTER_ID), resultDeployment.getMetadata().getName());
		final Map<String, String> expectedLabels = getCommonLabels();
		expectedLabels.put(Constants.LABEL_COMPONENT_KEY, Constants.LABEL_COMPONENT_JOB_MANAGER);
		expectedLabels.putAll(userLabels);
		assertEquals(expectedLabels, resultDeployment.getMetadata().getLabels());
	}

	@Test
	public void testDeploymentSpec() {
		final DeploymentSpec resultDeploymentSpec = this.kubernetesJobManagerSpecification.getDeployment().getSpec();
		assertEquals(1, resultDeploymentSpec.getReplicas().intValue());

		final Map<String, String> expectedLabels =  new HashMap<>(getCommonLabels());
		expectedLabels.put(Constants.LABEL_COMPONENT_KEY, Constants.LABEL_COMPONENT_JOB_MANAGER);
		expectedLabels.putAll(userLabels);

		assertEquals(expectedLabels, resultDeploymentSpec.getTemplate().getMetadata().getLabels());
		assertEquals(expectedLabels, resultDeploymentSpec.getSelector().getMatchLabels());

		assertNotNull(resultDeploymentSpec.getTemplate().getSpec());
	}

	@Test
	public void testPodSpec() {
		final PodSpec resultPodSpec =
			this.kubernetesJobManagerSpecification.getDeployment().getSpec().getTemplate().getSpec();

		assertEquals(1, resultPodSpec.getContainers().size());
		assertEquals(SERVICE_ACCOUNT_NAME, resultPodSpec.getServiceAccountName());
		assertEquals(1, resultPodSpec.getVolumes().size());

		final Container resultedMainContainer = resultPodSpec.getContainers().get(0);
		assertEquals(KubernetesJobManagerParameters.JOB_MANAGER_MAIN_CONTAINER_NAME, resultedMainContainer.getName());
		assertEquals(CONTAINER_IMAGE, resultedMainContainer.getImage());
		assertEquals(CONTAINER_IMAGE_PULL_POLICY.name(), resultedMainContainer.getImagePullPolicy());

		assertEquals(3, resultedMainContainer.getEnv().size());
		assertTrue(resultedMainContainer.getEnv()
				.stream()
				.anyMatch(envVar -> envVar.getName().equals("key1")));

		assertEquals(3, resultedMainContainer.getPorts().size());

		final Map<String, Quantity> requests = resultedMainContainer.getResources().getRequests();
		assertEquals(Double.toString(JOB_MANAGER_CPU), requests.get("cpu").getAmount());
		assertEquals(String.valueOf(JOB_MANAGER_MEMORY), requests.get("memory").getAmount());

		assertEquals(1, resultedMainContainer.getCommand().size());
		assertEquals(3, resultedMainContainer.getArgs().size());

		assertEquals(1, resultedMainContainer.getVolumeMounts().size());
	}

	@Test
	public void testAdditionalResourcesSize() {
		final List<HasMetadata> resultAdditionalResources = this.kubernetesJobManagerSpecification.getAccompanyingResources();
		assertEquals(3, resultAdditionalResources.size());

		final List<HasMetadata> resultServices = resultAdditionalResources
			.stream()
			.filter(x -> x instanceof Service)
			.collect(Collectors.toList());
		assertEquals(2, resultServices.size());

		final List<HasMetadata> resultConfigMaps = resultAdditionalResources
			.stream()
			.filter(x -> x instanceof ConfigMap)
			.collect(Collectors.toList());
		assertEquals(1, resultConfigMaps.size());
	}

	@Test
	public void testServices() {
		final List<Service> resultServices = this.kubernetesJobManagerSpecification.getAccompanyingResources()
			.stream()
			.filter(x -> x instanceof Service)
			.map(x -> (Service) x)
			.collect(Collectors.toList());

		assertEquals(2, resultServices.size());

		final List<Service> internalServiceCandidates = resultServices
				.stream()
				.filter(x -> x.getMetadata().getName().equals(InternalServiceDecorator.getInternalServiceName(CLUSTER_ID)))
				.collect(Collectors.toList());
		assertEquals(1, internalServiceCandidates.size());

		final List<Service> restServiceCandidates = resultServices
				.stream()
				.filter(x -> x.getMetadata().getName().equals(ExternalServiceDecorator.getExternalServiceName(CLUSTER_ID)))
				.collect(Collectors.toList());
		assertEquals(1, restServiceCandidates.size());

		final Service resultInternalService = internalServiceCandidates.get(0);
		assertEquals(2, resultInternalService.getMetadata().getLabels().size());

		assertNull(resultInternalService.getSpec().getType());
		assertEquals(Constants.HEADLESS_SERVICE_CLUSTER_IP, resultInternalService.getSpec().getClusterIP());
		assertEquals(2, resultInternalService.getSpec().getPorts().size());
		assertEquals(5, resultInternalService.getSpec().getSelector().size());

		final Service resultRestService = restServiceCandidates.get(0);
		assertEquals(2, resultRestService.getMetadata().getLabels().size());

		assertEquals(resultRestService.getSpec().getType(), "LoadBalancer");
		assertEquals(1, resultRestService.getSpec().getPorts().size());
		assertEquals(5, resultRestService.getSpec().getSelector().size());
	}

	@Test
	public void testFlinkConfConfigMap() {
		final ConfigMap resultConfigMap = (ConfigMap) this.kubernetesJobManagerSpecification.getAccompanyingResources()
			.stream()
			.filter(x -> x instanceof ConfigMap &&
				x.getMetadata().getName().equals(FlinkConfMountDecorator.getFlinkConfConfigMapName(CLUSTER_ID)))
			.collect(Collectors.toList())
			.get(0);

		assertEquals(2, resultConfigMap.getMetadata().getLabels().size());

		final Map<String, String> resultDatas = resultConfigMap.getData();
		assertEquals(3, resultDatas.size());
		assertEquals("some data", resultDatas.get("log4j.properties"));
		assertEquals("some data", resultDatas.get("logback.xml"));
		assertTrue(resultDatas.get(FLINK_CONF_FILENAME)
			.contains(KubernetesConfigOptionsInternal.ENTRY_POINT_CLASS.key() + ": " + ENTRY_POINT_CLASS));
	}

	@Test
	public void testExistingHadoopConfigMap() throws IOException {
		flinkConfig.set(KubernetesConfigOptions.HADOOP_CONF_CONFIG_MAP, EXISTING_HADOOP_CONF_CONFIG_MAP);
		kubernetesJobManagerSpecification = KubernetesJobManagerFactory.buildKubernetesJobManagerSpecification(kubernetesJobManagerParameters);

		assertFalse(kubernetesJobManagerSpecification.getAccompanyingResources().stream()
			.anyMatch(resource -> resource.getMetadata().getName().equals(HadoopConfMountDecorator.getHadoopConfConfigMapName(CLUSTER_ID))));

		final PodSpec podSpec = kubernetesJobManagerSpecification.getDeployment().getSpec().getTemplate().getSpec();
		assertTrue(podSpec.getVolumes().stream().anyMatch(volume -> volume.getConfigMap().getName().equals(EXISTING_HADOOP_CONF_CONFIG_MAP)));
	}

	@Test
	public void testHadoopConfConfigMap() throws IOException {
		setHadoopConfDirEnv();
		generateHadoopConfFileItems();
		kubernetesJobManagerSpecification = KubernetesJobManagerFactory.buildKubernetesJobManagerSpecification(kubernetesJobManagerParameters);

		final ConfigMap resultConfigMap = (ConfigMap) kubernetesJobManagerSpecification.getAccompanyingResources()
			.stream()
			.filter(x -> x instanceof ConfigMap &&
				x.getMetadata().getName().equals(HadoopConfMountDecorator.getHadoopConfConfigMapName(CLUSTER_ID)))
			.collect(Collectors.toList())
			.get(0);

		assertEquals(2, resultConfigMap.getMetadata().getLabels().size());

		final Map<String, String> resultDatas = resultConfigMap.getData();
		assertEquals(2, resultDatas.size());
		assertEquals("some data", resultDatas.get("core-site.xml"));
		assertEquals("some data", resultDatas.get("hdfs-site.xml"));
	}

	@Test
	public void testEmptyHadoopConfDirectory() throws IOException {
		setHadoopConfDirEnv();
		kubernetesJobManagerSpecification = KubernetesJobManagerFactory.buildKubernetesJobManagerSpecification(kubernetesJobManagerParameters);

		assertFalse(kubernetesJobManagerSpecification.getAccompanyingResources().stream()
			.anyMatch(resource -> resource.getMetadata().getName().equals(HadoopConfMountDecorator.getHadoopConfConfigMapName(CLUSTER_ID))));
	}
}


text:
import React from 'react';
import renderer from 'react/lib/ReactTestRenderer';

import Menu from '../../src/js/components/Menu';

// needed because this:
// https://github.com/facebook/jest/issues/1353
jest.mock('react-dom');

describe('Menu', () => {
  it('has correct default options', () => {
    const component = renderer.create(
      <Menu>

          First

          Second

          Third

      </Menu>
    );
    let tree = component.toJSON();
    expect(tree).toMatchSnapshot();
  });
});


text:
module Badmin
	class ApplicationForm < ActiveForm::Base
		def partial
			"#{self.class.name.demodulize.chomp('Form').underscore}_fields"
		end
	end
end


text:
<!DOCTYPE html>

    <meta http-equiv="refresh" content="0;URL=fn.SDL_GetWindowFromID.html">

Redirecting to fn.SDL_GetWindowFromID.html...


text:
module Service
  class ManageWorktime
    attr_reader :worktime_params, :comment_params

    def initialize(user, worktime_params, comment_params)
      @worktime_params = worktime_params.merge user: user
      @comment_params  = comment_params.merge  user: user
    end

    def create
      worktime = Worktime.new worktime_params
      ActiveRecord::Base.transaction do
        worktime.save!
        create_comment_for worktime
      end
      worktime.decorate
    end

    def update(worktime_id, comment_id)
      ActiveRecord::Base.transaction do
        worktime = Worktime.find worktime_id
        comment  = Comment.find_by id: comment_id
        worktime.update! worktime_params
        if comment
          update_comment comment
        else
          create_comment_for worktime
        end
        worktime.decorate
      end
    end

  private

    def create_comment_for(worktime)
      comment = worktime.build_comment comment_params
      comment.save! unless comment.message.empty?
    end

    def update_comment(comment)
      if comment_params[:message].empty?
        comment.destroy
      else
        comment.update! comment_params
      end
    end
  end
end


text:
package gov.hhs.fha.nhinc.patientdiscovery.passthru.proxy;

import gov.hhs.fha.nhinc.proxy.ComponentProxyObjectFactory;

/**
 * An object factory that uses the Spring Framework to create service implementation objects. The configuration file is
 * referenced in the common properties file location to assist in installation and configuration simplicity.
 *
 * The Spring configuration file is defined by the constant "SPRING_CONFIG_FILE". This file is loaded into an
 * application context in the static initializer and then the objects defined in the config file are available to the
 * framework for creation. This configuration file can be located anywhere in the classpath.
 *
 * To retrieve an object that is created by the framework, the "getBean(String beanId)" method is called on the
 * application context passing in the beanId that is specified in the config file. Considering the default correlation
 * definition in the config file for this component: <bean id="nhinpatientdiscovery"
 * class="gov.hhs.fha.nhinc.patientdiscovery.nhin.proxy.NhinPatientDiscoveryNoOpImpl"/> the bean id is
 * "nhinpatientdiscovery" and an object of this type can be retrieved from the application context by calling the
 * getBean method like: context.getBean("nhinpatientdiscovery");. This returns an object that can be casted to the
 * appropriate interface and then used in the application code. See the getNhinPatientDisocoveryProxy() method in this
 * class.
 *
 * @author Les Westberg
 */
public class PassthruPatientDiscoveryProxyObjectFactory extends ComponentProxyObjectFactory {

    private static final String CONFIG_FILE_NAME = "PatientDiscoveryProxyConfig.xml";
    private static final String BEAN_NAME_PATIENT_DISCOVERY = "nhincproxypatientdiscovery";

    /**
     * Returns the name of the config file.
     *
     * @return The name of the config file.
     */
    protected String getConfigFileName() {
        return CONFIG_FILE_NAME;
    }

    /**
     * Return an instance of the NhinPatientDiscoveryProxy class.
     *
     * @return An instance of the NhinPatientDiscoveryProxy class.
     */
    public PassthruPatientDiscoveryProxy getNhincPatientDiscoveryProxy() {

        return getBean(BEAN_NAME_PATIENT_DISCOVERY, PassthruPatientDiscoveryProxy.class);
    }
}


text:
namespace Stankins.Interfaces
{
    public interface ITable : IMetadataRow
    {

    }
}


text:
import { nativeTimeout, rafFrames } from '../../util/dom';
export var Activator = (function () {
    function Activator(app, config) {
        this.app = app;
        this._queue = [];
        this._active = [];
        this._css = config.get('activatedClass') || 'activated';
    }
    Activator.prototype.downAction = function (ev, activatableEle, startCoord) {
        var _this = this;
        if (this.disableActivated(ev)) {
            return;
        }
        this._queue.push(activatableEle);
        rafFrames(6, function () {
            var activatableEle;
            for (var i = 0; i < _this._queue.length; i++) {
                activatableEle = _this._queue[i];
                if (activatableEle && activatableEle.parentNode) {
                    _this._active.push(activatableEle);
                    activatableEle.classList.add(_this._css);
                }
            }
            _this._queue.length = 0;
        });
    };
    Activator.prototype.upAction = function (ev, activatableEle, startCoord) {
        var _this = this;
        rafFrames(CLEAR_STATE_DEFERS, function () {
            _this.clearState();
        });
    };
    Activator.prototype.clearState = function () {
        var _this = this;
        if (!this.app.isEnabled()) {
            nativeTimeout(function () {
                _this.clearState();
            }, 600);
        }
        else {
            this.deactivate();
        }
    };
    Activator.prototype.deactivate = function () {
        var _this = this;
        this._queue.length = 0;
        rafFrames(2, function () {
            for (var i = 0; i < _this._active.length; i++) {
                _this._active[i].classList.remove(_this._css);
            }
            _this._active = [];
        });
    };
    Activator.prototype.disableActivated = function (ev) {
        if (ev.defaultPrevented) {
            return true;
        }
        var targetEle = ev.target;
        for (var i = 0; i < 4; i++) {
            if (!targetEle) {
                break;
            }
            if (targetEle.hasAttribute('disable-activated')) {
                return true;
            }
            targetEle = targetEle.parentElement;
        }
        return false;
    };
    return Activator;
}());
var CLEAR_STATE_DEFERS = 5;
//# sourceMappingURL=activator.js.map


text:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="VcsDirectoryMappings">
    <mapping directory="$USER_HOME$/andoowhy.github.io" vcs="" />
  </component>
</project>


text:
This is a simple FirePHP package to allow a better AJAX development with debugging.

Fuel's default debugger is awesome, but still breaks the layout when errors are found. So why not display them on the Firebug's console?

More about *FirePHP*: **http://www.firephp.org/**

## Installing

Clone from Github. Put it on `'packages_dir/firephp'` dir in and add to your app/config/config.php.

	git clone git://github.com/kriansa/fuel-firephp.git

Works with Fuel 1.1

## Usage

This package simply replaces the original error and exception handler and the Debug class. It also adds a FirePHP handler to log things

If you want to log something, just do the following:

```php
Debug::dump('anything');
// Or a trace
Debug::backtrace();
// Or a FirePHP event
Debug::firephp()->info('this is a test');
```

## License

Fuel FirePHP package is released under the MIT License.


text:
# accessNav version 1.2
## by IBM Accessibility
Copyright (c) 2016, IBM Corporation. All rights reserved.

Copyright (c) 2014, eBay Software Foundation. All rights reserved.

The accessNav script is based on the SkipTo version 2.1 script and is a replacement for the classic *Skipnav* link. It creates a drop-down menu that lists the links to the important places on the current web page. Once accessNav is installed and configured, its menu makes it easier for keyboard and screen reader users to quickly jump to a location by choosing the location from the menu.

![Example Screen Shot](images/A11yAdvisorwSkipTo.png "Example Screen Shot")
## How it works
1.  By default the accessNav *Skip To* menu becomes visible the first time the user tabs into the page or may appear after the page is loaded if the visibility is set to onLoad.
2.  Once the keyboard focus is on the *Skip To* menu, selecting the **Enter** key or the **Spacebar** will display the list of high-level headings and landmarks on the current page.
3.  Use the arrow keys to select a choice; then select **Enter** to skip to it.
4.  To return to the *Skip To* menu, use the built-in access key (0 by default) which opens the menu and moves focus to it. Refer to the notes on [Access keys](#access-keys) for more information.

## Enabling the JavaScript on your web page
All you need is either `SkipTo.js` or `SkipTo.min.js` from the `compiled/js` directory. Please note that `SkipTo.min.js` is a minified (light-weight) version of the script. (NOTE: UPDATE ALL FILENAMES WHEN MOE CHANGES IT TO ACCESSNAV)  If you would like to be able to debug your production-ready script, include the provided `SkipTo.min.map` file as well.

To use the accessNav script, just include it at the bottom of your HTML page or template as follows:

```html
<script type="text/javascript" src="../compiled/js/skipto.js"></script>
```

Note: Make sure that `src` points to the place where you put the Javascript file in your web application. Otherwise, things will not work directly. For detailed instructions on how to deploy the script to an IBM Connections environment, refer to [Deploying to IBM Connections.](#connections)

## Deploying to IBM Connections

### Custom JavaScript
The following instructions outline how to deploy the accessNav script *Skip To* menu to an on-premise version of IBM Connections. These instructions were tested using IBM Connections Version 5.5.0. Full details of how to deploy custom JavaScript to IBM Connections are found in the IBM Knowledge Center topic, [Extending JavaScript in IBM Connections.](http://www.ibm.com/support/knowledgecenter/SSYGQH_5.5.0/admin/customize/t_customize_extend_js.html)

IBM Connections uses the shared resources WAR file, `connections.web.resources.war,` to aggregate and serve all JavaScript files. This WAR file is based on the OSGi extension model, which allows new capabilities to be added to a system via a plug-in mechanism. IBM Connections leverages this mechanism to provide the following capabilities:

*  Expose custom JavaScript
*  Ensure that custom JavaScript loads when another module is loaded

To extend the JavaScript used in IBM Connections, first you must put your JavaScript files into an OSGi bundle (a JAR file with a special `MANIFEST.MF` file and some directories), and deploy the bundle into IBM Connections. Then, you need to link your JavaScript to ensure that it is loaded at the same time as the rest of the JavaScript in IBM Connections.

### Adding `Skipto.js` to a sample app
The easiest way to extend the JavaScript used in IBM Connections is to start with a sample bundle, add files to it, and deploy it in your IBM Connections environment. Complete the following steps to extend the JavaScript used in IBM Connections using a sample bundle.

1.  Download the following sample bundle: [com.mycompany.example_1.0.0.jar](http://public.dhe.ibm.com/software/dw/lotus/connections/4/reference/com.mycompany.example_1.0.0.jar)
2.  Deploy and test the sample bundle.
	*  Copy the sample bundle to web resources provisioning directory for IBM Connections. The installer creates this directory at the following location: `CONNECTIONS_HOME/data/shared/provision/webresources`
	*  Stop and start the Common Websphere Enterprise Application (common.ear) from the Websphere Application Server interface.
	*  Test the deployment of the sample app by opening the `readme.txt` file included with the sample bundle by entering the following URL in your web browser: 	`http://server/connections/resources/web/com.mycompany.example/readme.txt`
3.  Add `SkipTo.js` to the sample bundle.
	*  Using your favorite archive editor, add the `Skipto.js` to the `resources/directory` of the sample bundle:  `com.mycompany.example_1.0.0.jar.`
4.  Edit the `plugin.xml` file in the sample app and add a new `<dojoModuleBinding>` element following `demonstration.js` to request that `SkipTo.js` is loaded after core Connections JavaScript is loaded.

	`<dojoModuleBinding bind="com.mycompany.example.demonstration" to="lconn.core.header" />`

	`<dojoModuleBinding bind="com.mycompany.example.skipto" to="lconn.core.header" />`
5.	Restart common.ear to pick up the changes in the `plugin.xml` file.
 	* Stop and start the Common Websphere Enterprise Application (common.ear) from the Websphere Application Server interface.
6.	Clear your browser cache and verify `skipto.js` is discoverable by viewing it on your server. For example, enter the following url into your browser:

	`http://server/connections/resources/web/com.mycompany.example/skipto.js`

7.	Optionally, change the name of the bundle. Refer to the IBM Knowledge Center topic, [Extending JavaScript in IBM Connections.](http://www.ibm.com/support/knowledgecenter/SSYGQH_5.5.0/admin/customize/t_customize_extend_js.html)
8.	Log in as a user to test the accessNav *Skip To* script. For example, open the Blogs application and type Alt + Shift + 0 keys to launch the accessNav *Skip To* menu when accessing the application in Firefox.

	Note: The access key is customizable and varies per browser. In Internet Explorer, Alt + 0 is the default access key.

## Configuring accessNav options
By default, the accessNav *Skip To* menu includes the following places on the page:

*  Heading (e.g., h1, h2, h3 and h4 elements).
*  ARIA landmarks (e.g., banner, navigation, main and search).
*  HTML5 Section Elements (e.g., main, `section[aria-label],` `section[aria-labelledby]`)
*  Any element with the id specified via the configuration file.
*  Any element with the custom class specified via the configuration file.

and options:

*  The "accesskey" is set to 0.
*  The menu is set not to wrap.
*  The menu is visible on keyboard focus only (can be changed to be always visible via the "onload" parameter).

You may have different requirements for your web site and can include other heading levels, other ARIA landmarks, and HTML5 sectioning elements. You will need to provide a JSON object containing the necessary configuration parameters. This may look like the following:

```html
<script>
var SkipToConfig =
{
	"settings": {
		"skipTo": {
			"headings"     : "h1, h2, h3, h4",
			"main"         : "main, [role=main]",
			"landmarks"    : "[role=navigation], [role=search]",
			"sections"     : "nav",
			"ids"          : "#SkipToA1, #SkipToA2",
			"customClass"  : "MyClass",
			"accesskey"    : "0",
			"wrap"         : "true",
			"visibility"   : "onfocus",
			"attachElement": ".MyCustomClass" // or "attachElement": "#MyCustomId"
		}
	}
};
</script>
```

The code above will need to be inserted before including the `SkipTo.js` or `SkipTo.min.js` into your page or template.

### Notes
*  Most parameters are optional.
*  accessNav can be attached to any element on the page (refer to the "attachElement" parameter). If no "attachElement" is found, the script will be attached as the first element after the body.
*  When the custom class is specified (refer to the "customClass" parameter), the user can override the style, as in the following `MyCustomClass` CSS example:

```CSS
.dropMenu .MyCustomClass {
	background:  red;
	left: 50px;
	top: 50px;
}
```

## Compiling CSS and JavaScript
You may feel slightly adventurous and decide to change some colors or even enhance the script with your changes. Once you do this, here is how you compile the accessNav script for production.

1.  Get [NodeJS](http://nodejs.org) and install it on the operating system of your choice.
2.  Get [the latest code from Github](https://github.com/paypal/skipto).
3.  After unzipping or downloading the source code into the directory of your choice, switch to that directory and type these commands:

  ```sh
   sudo npm install grunt-cli -g
   npm install
   grunt
  ```
4.  You should now have a directory called `**compiled**` with the necessary files in it.
5.  Refer to the instructions in [Enabling the JavaScript on your web page](#enabling_javascript) for information on which files you need to get the accessNav script running on your web page.

Note: On Windows, `build-win.bat` runs `npm install` and `grunt` modules (Step 3). To successfully run, you must launch a Windows command prompt as an Admin (Ctrl + Shift + Enter) and then run `build-win.bat` from this command prompt.

## Cleaning up
If you would like to revert your local code repository to its initial state, run:
```sh
grunt clean
```
from the root directory of your repository.

## Of course, we want feedback!
Please do not hesitate to [raise issues and comment on Github](https://github.com/moekraft/accessNav/issues) if something doesn't work or you have ideas on how to improve the script.

Happy skipping!

IBM Accessibility Team
[Twitter: @IBMAccess](http://www.twitter.com/IBMAccess)

## Authors
**Moe Kraft**
[https://github.com/moekraft](https://github.com/moekraft) || [@moekraft](https://twitter.com/moekraft)

**Jack Dam**
[https://github.com/githubdam](https://github.com/githubdam)

**Fran Hayden**
[https://github.com/fahayden](https://github.com/fahayden) || [@fhayden1112](https://twitter.com/fhayden1112)

## Access keys
Access keys work just like regular shortcut keys except that they need a browser-specific modifier key in order to work. For example, to use the *Skip To* access key, you would press the modifier key + the access key (0 in this particular case). here is a quick list for how this would work in most popular browsers.

*  Microsoft Internet Explorer -- Alt + 0.
*  Mozilla Firefox -- Alt +Shift + 0.
*  Google Chrome -- Ctrl + Alt + 0 (Windows) and Command + Option + 0 (Mac OS). (VERIFY ACCESS KEYS ON MAC)
*  Safari -- Command + 0. (VERIFY THIS ON MAC)

## Version History

### accessNav version 1.2
* Added support for Internet Explorer 8.0

#### accessNav version 1.1
* Initial fork of accessNav from SkipTo

### accessNav version 1.0
* Rebranding to IBM Accessibility accessNav

## Copyright and license
Copyright 2016, IBM Corporation under the [BSD license](LICENSE.md).

Copyright 2015, eBay Software Foundation under the [BSD license](LICENSE.md).


text:
package com.spotify.scio.elasticsearch

import java.lang.{Iterable => JIterable}

import com.spotify.scio.values.SCollection
import com.spotify.scio.ScioContext
import com.spotify.scio.io.{EmptyTap, EmptyTapOf, ScioIO, Tap}
import org.elasticsearch.action.DocWriteRequest
import org.apache.beam.sdk.io.{elasticsearch => beam}
import org.apache.beam.sdk.io.elasticsearch.ElasticsearchIO.Write.BulkExecutionException
import org.apache.beam.sdk.transforms.SerializableFunction
import org.joda.time.Duration

import scala.jdk.CollectionConverters._
import com.spotify.scio.io.TapT

final case class ElasticsearchIO[T](esOptions: ElasticsearchOptions) extends ScioIO[T] {
  override type ReadP = Nothing
  override type WriteP = ElasticsearchIO.WriteParam[T]
  override val tapT: TapT.Aux[T, Nothing] = EmptyTapOf[T]

  override protected def read(sc: ScioContext, params: ReadP): SCollection[T] =
    throw new UnsupportedOperationException("Can't read from Elacticsearch")

  /** Save this SCollection into Elasticsearch. */
  override protected def write(data: SCollection[T], params: WriteP): Tap[Nothing] = {
    val shards = if (params.numOfShards >= 0) {
      params.numOfShards
    } else {
      esOptions.servers.size.toLong
    }

    data.applyInternal(
      beam.ElasticsearchIO.Write
        .withClusterName(esOptions.clusterName)
        .withServers(esOptions.servers.toArray)
        .withFunction(new SerializableFunction[T, JIterable[DocWriteRequest[_]]]() {
          override def apply(t: T): JIterable[DocWriteRequest[_]] =
            params.f(t).asJava
        })
        .withFlushInterval(params.flushInterval)
        .withNumOfShard(shards)
        .withMaxBulkRequestSize(params.maxBulkRequestSize)
        .withMaxBulkRequestBytes(params.maxBulkRequestBytes)
        .withMaxRetries(params.retry.maxRetries)
        .withRetryPause(params.retry.retryPause)
        .withError(new beam.ThrowingConsumer[BulkExecutionException] {
          override def accept(t: BulkExecutionException): Unit =
            params.errorFn(t)
        })
    )
    EmptyTap
  }

  override def tap(params: ReadP): Tap[Nothing] =
    EmptyTap
}

object ElasticsearchIO {
  object WriteParam {
    private[elasticsearch] val DefaultErrorFn: BulkExecutionException => Unit = m => throw m
    private[elasticsearch] val DefaultFlushInterval = Duration.standardSeconds(1)
    private[elasticsearch] val DefaultNumShards = -1L
    private[elasticsearch] val DefaultMaxBulkRequestSize = 3000
    private[elasticsearch] val DefaultMaxBulkRequestBytes = 5L * 1024L * 1024L
    private[elasticsearch] val DefaultMaxRetries = 3
    private[elasticsearch] val DefaultRetryPause = Duration.millis(35000)
    private[elasticsearch] val DefaultRetryConfig =
      RetryConfig(
        maxRetries = WriteParam.DefaultMaxRetries,
        retryPause = WriteParam.DefaultRetryPause
      )
  }

  final case class WriteParam[T] private (
    f: T => Iterable[DocWriteRequest[_]],
    errorFn: BulkExecutionException => Unit = WriteParam.DefaultErrorFn,
    flushInterval: Duration = WriteParam.DefaultFlushInterval,
    numOfShards: Long = WriteParam.DefaultNumShards,
    maxBulkRequestSize: Int = WriteParam.DefaultMaxBulkRequestSize,
    maxBulkRequestBytes: Long = WriteParam.DefaultMaxBulkRequestBytes,
    retry: RetryConfig = WriteParam.DefaultRetryConfig
  )

  final case class RetryConfig(maxRetries: Int, retryPause: Duration)
}


text:
using System;
using System.Linq;
using System.Collections.Generic;

struct Coordinates
{
    public int Row { get; private set; }
    public int Col { get; private set; }

    public Coordinates(int row, int col)
        : this()
    {
        this.Row = row;
        this.Col = col;
    }

    public static int Distance(Coordinates a, Coordinates b)
    {
        return Math.Abs(a.Row - b.Row) + Math.Abs(a.Col - b.Col);
    }
}

public class Program
{
    static Dictionary<string, string> secretKeys = null;

    static Dictionary<string, Coordinates> coords = new Dictionary<string, Coordinates>();

    static string[][] matrix = null;

    static string Decode(string encoded)
    {
        return string.Join("-", encoded.Split('-').Select(part => secretKeys[part]).ToArray());
    }

    static void GenerateCoords()
    {
        for (int row = 0; row < matrix.Length; row++)
            for (int col = 0; col < matrix[row].Length; col++)
                coords[matrix[row][col]] = new Coordinates(row, col);
    }

    static void Main()
    {
#if DEBUG
        Console.SetIn(new System.IO.StreamReader("../../input.txt"));
#endif

        var maxDistance = int.Parse(Console.ReadLine());

        secretKeys = Enumerable.Range(0, int.Parse(Console.ReadLine()))
            .Select(i => Console.ReadLine().Split())
            .ToDictionary(parts => parts[0], parts => parts[1]);

        var rows = int.Parse(Console.ReadLine().Split()[0]);

        matrix = Enumerable.Range(0, rows)
            .Select(row => Console.ReadLine().Split())
            .ToArray();

        GenerateCoords();

        var targetArea = Console.ReadLine();

        var encodedTemplars = Enumerable.Range(0, int.Parse(Console.ReadLine()))
            .Select(i => Console.ReadLine())
            .ToArray();

        var decodedTemplars = encodedTemplars.Select(Decode);
        var coordinatesTemplars = decodedTemplars.Select(coord => coords[coord]).ToArray();

        var center = coords[Decode(targetArea)];

        var result = coordinatesTemplars.Where(coord =>
            Coordinates.Distance(center, coord) <= maxDistance
        );

        Console.WriteLine(result.Count());
    }
}


text:
package com.xjeffrose.log;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;

class LogFormatter extends Formatter implements Glog.Formatter<LogRecord> {
  public Map<Level, Glog.Level> levelLabel = new HashMap<Level, Glog.Level>();

  LogFormatter() {
    levelLabel.put(Level.FINEST, Glog.Level.DEBUG);
    levelLabel.put(Level.FINER, Glog.Level.DEBUG);
    levelLabel.put(Level.FINE, Glog.Level.DEBUG);
    levelLabel.put(Level.CONFIG, Glog.Level.INFO);
    levelLabel.put(Level.INFO, Glog.Level.INFO);
    levelLabel.put(Level.WARNING, Glog.Level.WARNING);
    levelLabel.put(Level.SEVERE, Glog.Level.ERROR);
  }

  public String format(final LogRecord record) {
    return Glog.formatRecord(this, record);
  }

  public String getMessage(LogRecord record) {
    return formatMessage(record);
  }

  public String getClassName(LogRecord record) {
    return record.getSourceClassName();
  }

  public String getMethodName(LogRecord record) {
    return record.getSourceMethodName();
  }

  public Glog.Level getLevel(LogRecord record) {
    return levelLabel.get(record.getLevel());
  }

  public long getTimeStamp(LogRecord record) {
    return record.getMillis();
  }

  public long getThreadId(LogRecord record) {
    return record.getThreadID();
  }

  public Throwable getThrowable(LogRecord record) {
    return record.getThrown();
  }
}


text:
from __future__ import unicode_literals

from django.db import migrations, models
import django.utils.timezone

class Migration(migrations.Migration):

    dependencies = [
        ('user', '0001_initial'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='user',
            options={},
        ),
        migrations.AddField(
            model_name='user',
            name='created',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='user',
            name='modified',
            field=models.DateTimeField(auto_now=True),
        ),
    ]