text:
/*
 * Contributors:
 * Adrian Jackson - iapetus@users.sourceforge.net
 * David Taylor - exodussystems@users.sourceforge.net
 * Lars Kristensen - llk@users.sourceforge.net
 * Ling Li - lonecatz@users.sourceforge.net
 * Martin Clough - mtclough@users.sourceforge.net
 */
package net.sf.jasperreports.engine.export;

import java.util.Stack;

import net.sf.jasperreports.crosstabs.JRCellContents;
import net.sf.jasperreports.engine.JRPrintElement;
import net.sf.jasperreports.engine.JRPrintFrame;
import net.sf.jasperreports.engine.JRPrintImage;

import com.lowagie.text.pdf.PdfArray;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfNumber;
import com.lowagie.text.pdf.PdfString;
import com.lowagie.text.pdf.PdfStructureElement;
import com.lowagie.text.pdf.PdfStructureTreeRoot;
import com.lowagie.text.pdf.PdfWriter;

/**
 * Provides support for tagged PDF documents.
 *

 * PDF files can contain hidden tags that describe the structure of the document. Some of
 * the tags are used by the automated reader tool that reads PDF documents aloud to people
 * with disabilities.
 *
Marking Headings

 * JasperReports currently supports specifying type 1, 2 and 3 level headings.
 * In order to mark a text field as a level 1 heading, the following custom element property
 * should be used in JRXML:
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.h1" value="full"/>
 *

 * Value full means that a full <H1> tag will be embedded in the PDF wrapping the
 * current text element.
 * If two or more text fields make up a single level 1 heading, there are two ways to mark
 * the heading:
 *

 *
In the first, the text elements making up the heading are placed inside a frame and
 * the frame is marked with the following custom property:
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.h1" value="full"/>

 *
In the second, the first element of the heading (respective to the Z-Order, or the
 * order in which the elements appear in JRXML) is tagged with:
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.h1" value="start"/>
 *

 * and the last element from the heading (respective to the same order) is marked with
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.h1" value="end"/>

 *

 *

 * Level 2 and level 3 headings are marked the same way, except that the properties are:
 *

 * net.sf.jasperreports.export.pdf.tag.h2
 *

 * and
 *

 * net.sf.jasperreports.export.pdf.tag.h3.
 *
Marking Tables

 * Tables are comprised of column headers, row headers, and a data section. Each table
 * section is made of cells. Marking table structures in PDF is similar to the way tables are
 * described in HTML and uses the same techniques as those for marking headings.
 *

 * When marking a table, the user has to indicate in the report template where the table
 * starts and where it ends.
 *

 * If the entire table is placed in a container, such as a frame element, marking the table
 * requires only marking the parent frame with the following custom element property:
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.table" value="full"/>
 *

 * However, most of the time, tables cannot be isolated in a frame unless they are
 * subreports, because they generally span multiple report sections and bands. In such
 * cases, marking a table requires marking in JRXML the first and last element making up
 * the table structure.
 *

 * The first element of the table (probably the first element in the table header) should be
 * marked with the following custom property:
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.table" value="start"/>
 *

 * The last element of the table should be marked with:
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.table" value="end"/>
 *

 * Tables are made of rows, and each row has to be precisely delimited within the table
 * structure. This includes the column header rows at the top of the table. Similar to the
 * headings and table marking, a table row can be identified in two ways:
 *

 *
If the entire content that makes up the row is isolated within a frame, the frame can be
 * marked with the following custom property:
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.tr" value="full"/>
 *

 *
If the content of the row is not grouped in a container frame, its first and last elements
 * (respective to the Z-order or the order in which they appear in JRXML) have to be
 * marked with the following custom properties:
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.tr" value="start"/>
 *

 * for the first element and
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.tr" value="start"/>
 *

 * for the last element.

 *

 *

 * Each table row can contain header cells or data cells. Regardless of their type, and
 * similar to headings, tables, and table rows, cells can be marked either by marking a
 * single element representing the cell content (this single cell element can actually be a
 * frame element), or by marking the first and last element from the cell content.
 *

 * Header cells made of a single element (this single element can actually be a frame) are
 * marked with
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.th" value="full"/>
 *

 * A header cell made of multiple elements is marked with
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.th" value="start"/>
 * on its first element and
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.th" value="end"/>
 * on its last element.
 *

 * Normal data cells made of a single element (that can be frame) are marked with
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.td" value="full"/>
 *

 * Normal data cells made of multiple elements are marked with
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.td" value="start"/>
 * on their first element and
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.td" value="end"/>
 * on their last element.
 *

 * Just as in HTML tables, cells can span multiple rows and/or columns. Column span and
 * row span values for the current table cell can be specified using the following custom
 * properties on the same element where the cell start was marked (the element with the
 * full or start property marking the cell):
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.colspan" value="<number>"/>
 *

 * <property name="net.sf.jasperreports.export.pdf.tag.rowspan" value="<number>"/>
 *
PDF Content Reading Order

 * JasperReports uses the Z-order of the elements as present in the report template
 * (JRXML) to control reading order in the resulting PDF files. This is usually the intended
 * way for the documents to be read, so no specific modifications were required in order to
 * achieve it.
 *
 * @author Teodor Danciu (teodord@users.sourceforge.net)
 * @version $Id: JRPdfExporterTagHelper.java 7199 2014-08-27 13:58:10Z teodord $
 */
public class JRPdfExporterTagHelper
{
	public static final String PROPERTY_TAG_TABLE = JRPdfExporter.PDF_EXPORTER_PROPERTIES_PREFIX + "tag.table";
	public static final String PROPERTY_TAG_TR = JRPdfExporter.PDF_EXPORTER_PROPERTIES_PREFIX + "tag.tr";
	public static final String PROPERTY_TAG_TH = JRPdfExporter.PDF_EXPORTER_PROPERTIES_PREFIX + "tag.th";
	public static final String PROPERTY_TAG_TD = JRPdfExporter.PDF_EXPORTER_PROPERTIES_PREFIX + "tag.td";
	public static final String PROPERTY_TAG_H1 = JRPdfExporter.PDF_EXPORTER_PROPERTIES_PREFIX + "tag.h1";
	public static final String PROPERTY_TAG_H2 = JRPdfExporter.PDF_EXPORTER_PROPERTIES_PREFIX + "tag.h2";
	public static final String PROPERTY_TAG_H3 = JRPdfExporter.PDF_EXPORTER_PROPERTIES_PREFIX + "tag.h3";
	public static final String PROPERTY_TAG_COLSPAN = JRPdfExporter.PDF_EXPORTER_PROPERTIES_PREFIX + "tag.colspan";
	public static final String PROPERTY_TAG_ROWSPAN = JRPdfExporter.PDF_EXPORTER_PROPERTIES_PREFIX + "tag.rowspan";
	public static final String TAG_START = "start";
	public static final String TAG_END = "end";
	public static final String TAG_FULL = "full";

	protected JRPdfExporter exporter;

	protected PdfContentByte pdfContentByte;
	protected PdfWriter pdfWriter;

	protected PdfStructureElement allTag;
	protected Stack<PdfStructureElement> tagStack;
	protected boolean isTagEmpty = true;
	protected int crtCrosstabRowY = -1;
	protected int crosstabFrameDepth;
	protected boolean isDataCellPrinted;

	protected boolean isTagged;
	protected String language;

	/**
	 *
	 */
	protected JRPdfExporterTagHelper(JRPdfExporter exporter)
	{
		this.exporter = exporter;
	}

	/**
	 *
	 */
	protected void setTagged(boolean isTagged)
	{
		this.isTagged = isTagged;
	}

	/**
	 *
	 */
	protected void setLanguage(String language)
	{
		this.language = language;
	}

	/**
	 *
	 */
	protected void setPdfWriter(PdfWriter pdfWriter)
	{
		this.pdfWriter = pdfWriter;

		if (isTagged)
		{
			pdfWriter.setTagged();
		}
	}

	/**
	 *
	 */
	protected void init(PdfContentByte pdfContentByte)
	{
		this.pdfContentByte = pdfContentByte;

		if (isTagged)
		{
			PdfStructureTreeRoot root = pdfWriter.getStructureTreeRoot();
			allTag = new PdfStructureElement(root, new PdfName("All"));
			root.mapRole(new PdfName("All"), new PdfName("Sect"));
			if(pdfWriter.getPDFXConformance() == PdfWriter.PDFA1A)
			{
				root.mapRole(new PdfName("Anchor"), PdfName.NONSTRUCT);
				root.mapRole(PdfName.TEXT, PdfName.SPAN);
				root.mapRole(PdfName.IMAGE, PdfName.FIGURE);
			}
			else
			{
				root.mapRole(new PdfName("Anchor"), PdfName.TEXT);
			}

			if (language != null)
			{
				allTag.put(PdfName.LANG, new PdfString(language));
			}
			tagStack = new Stack<PdfStructureElement>();
			tagStack.push(allTag);
		}
	}

	protected void startPageAnchor()
	{
		if (isTagged)
		{
			PdfStructureElement textTag = new PdfStructureElement(allTag, new PdfName("Anchor"));
			pdfContentByte.beginMarkedContentSequence(textTag);
		}
	}

	protected void endPageAnchor()
	{
		if (isTagged)
		{
			pdfContentByte.endMarkedContentSequence();
		}
	}

	protected void startPage()
	{
		crtCrosstabRowY = -1;
		crosstabFrameDepth = 0;
		isDataCellPrinted = false;

	}

	protected void endPage()
	{
		if (isTagged)
		{
			if (crtCrosstabRowY >= 0) //crosstab still open
			{
				//end the current row
				pdfContentByte.endMarkedContentSequence();
				tagStack.pop();

				//end the table
				pdfContentByte.endMarkedContentSequence();
				tagStack.pop();
			}
		}
	}

	protected void startElement(JRPrintElement element)
	{
		if (isTagged)
		{
			if (element instanceof JRPrintFrame)
			{
				JRPrintFrame frame = (JRPrintFrame) element;

				boolean isCellContentsFrame =
					frame.getPropertiesMap().hasProperties()
					&& frame.getPropertiesMap().getProperty(JRCellContents.PROPERTY_TYPE) != null;

				if (crtCrosstabRowY >= 0) //crosstab already started
				{
					//frame depth must be incremented for all frame, when inside a crosstab
					crosstabFrameDepth++;

					if (isCellContentsFrame)
					{
						if (JRCellContents.TYPE_DATA.equals(frame.getPropertiesMap().getProperty(JRCellContents.PROPERTY_TYPE)))
						{
							isDataCellPrinted = true;
						}

						if (crtCrosstabRowY != frame.getY())
						{
							//this is the first cell on a new row

							//end the current row
							pdfContentByte.endMarkedContentSequence();
							tagStack.pop();

							if (
								isDataCellPrinted
								&& (JRCellContents.TYPE_CROSSTAB_HEADER.equals(frame.getPropertiesMap().getProperty(JRCellContents.PROPERTY_TYPE))
									|| JRCellContents.TYPE_COLUMN_HEADER.equals(frame.getPropertiesMap().getProperty(JRCellContents.PROPERTY_TYPE)))
								)
							{
								//end the table
								pdfContentByte.endMarkedContentSequence();
								tagStack.pop();

								//start table
								createTableStartTag();
								//start crosstab
								isDataCellPrinted = false;
							}

							//start the new row
							createTrStartTag();

							//keep crosstab open, but mark new row position and frame depth
							crtCrosstabRowY = frame.getY();
						}
					}
					else
					{
						if (crosstabFrameDepth == 1)
						{
							//normal frame outside crosstab

							//end the current row
							pdfContentByte.endMarkedContentSequence();
							tagStack.pop();

							//end the table
							pdfContentByte.endMarkedContentSequence();
							tagStack.pop();

							//end crosstab
							crtCrosstabRowY = -1;
							//make depth zero because it will not be decremented after frame export
							// due to crosstab being ended here
							crosstabFrameDepth = 0;
						}
					}
				}
				else
				{
					if (isCellContentsFrame)
					{
						//start table and firts row
						createTableStartTag();
						createTrStartTag();
						//start crosstab
						crtCrosstabRowY = frame.getY();
						crosstabFrameDepth++;
						isDataCellPrinted = false;
					}
//					else
//					{
//						//normal frame outside crosstab
//					}
				}
			}
			else
			{
				if (crtCrosstabRowY >= 0) //crosstab already started
				{
					if (crosstabFrameDepth == 0)
					{
						//normal element outside crosstab

						//end the current row
						pdfContentByte.endMarkedContentSequence();
						tagStack.pop();

						//end the table
						pdfContentByte.endMarkedContentSequence();
						tagStack.pop();

						//end crosstab
						crtCrosstabRowY = -1;
						crosstabFrameDepth = 0;
					}
				}
			}

			createStartTags(element);
		}
	}

	protected void endElement(JRPrintElement element)
	{
		if (isTagged)
		{
			if (element instanceof JRPrintFrame)
			{
				if (crtCrosstabRowY >= 0) // crosstab started
				{
					crosstabFrameDepth--;
				}
			}

			createEndTags(element);
		}
	}

	protected void startImage(JRPrintImage printImage)
	{
		if (isTagged)
		{
			PdfStructureElement imageTag = new PdfStructureElement(allTag, PdfName.IMAGE);
			pdfContentByte.beginMarkedContentSequence(imageTag);
			if (printImage.getHyperlinkTooltip() != null)
			{
				imageTag.put(PdfName.ALT, new PdfString(printImage.getHyperlinkTooltip()));
			}
		}
	}

	protected void endImage()
	{
		if (isTagged)
		{
			pdfContentByte.endMarkedContentSequence();
		}
	}

	protected void startText()
	{
		if (isTagged)
		{
//			PdfStructureElement parentTag = tableCellTag == null ? (tableHeaderTag == null ? allTag : tableHeaderTag): tableCellTag;
//			PdfStructureElement textTag = new PdfStructureElement(parentTag, PdfName.TEXT);
			PdfStructureElement textTag = new PdfStructureElement(tagStack.peek(), PdfName.TEXT);
			pdfContentByte.beginMarkedContentSequence(textTag);
		}
	}

	protected void endText()
	{
		if (isTagged)
		{
			pdfContentByte.endMarkedContentSequence();
			isTagEmpty = false;
		}
	}

	protected void createStartTags(JRPrintElement element)
	{
		if (element.hasProperties())
		{
			String prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_TABLE);
			if (prop != null && (TAG_START.equals(prop) || TAG_FULL.equals(prop)))
			{
				createTableStartTag();
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_TR);
			if (prop != null && (TAG_START.equals(prop) || TAG_FULL.equals(prop)))
			{
				createTrStartTag();
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_TH);
			if (prop != null && (TAG_START.equals(prop) || TAG_FULL.equals(prop)))
			{
				createThStartTag(element);
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_TD);
			if (prop != null && (TAG_START.equals(prop) || TAG_FULL.equals(prop)))
			{
				createTdStartTag(element);
			}

			prop = element.getPropertiesMap().getProperty(JRCellContents.PROPERTY_TYPE);
			if (
				prop != null
				&& (JRCellContents.TYPE_CROSSTAB_HEADER.equals(prop)
					|| JRCellContents.TYPE_COLUMN_HEADER.equals(prop)
					|| JRCellContents.TYPE_ROW_HEADER.equals(prop)))
			{
				createThStartTag(element);
			}
			if (prop != null && (JRCellContents.TYPE_DATA.equals(prop)))
			{
				createTdStartTag(element);
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_H1);
			if (prop != null && (TAG_START.equals(prop) || TAG_FULL.equals(prop)))
			{
				PdfStructureElement headingTag = new PdfStructureElement(tagStack.peek(), new PdfName("H1"));
				pdfContentByte.beginMarkedContentSequence(headingTag);
				headingTag.put(PdfName.K, new PdfArray());
				tagStack.push(headingTag);
				isTagEmpty = true;
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_H2);
			if (prop != null && (TAG_START.equals(prop) || TAG_FULL.equals(prop)))
			{
				PdfStructureElement headingTag = new PdfStructureElement(tagStack.peek(), new PdfName("H2"));
				pdfContentByte.beginMarkedContentSequence(headingTag);
				headingTag.put(PdfName.K, new PdfArray());
				tagStack.push(headingTag);
				isTagEmpty = true;
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_H3);
			if (prop != null && (TAG_START.equals(prop) || TAG_FULL.equals(prop)))
			{
				PdfStructureElement headingTag = new PdfStructureElement(tagStack.peek(), new PdfName("H3"));
				pdfContentByte.beginMarkedContentSequence(headingTag);
				headingTag.put(PdfName.K, new PdfArray());
				tagStack.push(headingTag);
				isTagEmpty = true;
			}
		}
	}

	protected void createTableStartTag()
	{
		PdfStructureElement tableTag = new PdfStructureElement(allTag, new PdfName("Table"));
		pdfContentByte.beginMarkedContentSequence(tableTag);
		tableTag.put(PdfName.K, new PdfArray());
		tagStack.push(tableTag);
	}

	protected void createTrStartTag()
	{
		PdfStructureElement tableRowTag = new PdfStructureElement(tagStack.peek(), new PdfName("TR"));
		pdfContentByte.beginMarkedContentSequence(tableRowTag);
		tableRowTag.put(PdfName.K, new PdfArray());
		tagStack.push(tableRowTag);
	}

	protected void createThStartTag(JRPrintElement element)
	{
		PdfStructureElement tableHeaderTag = new PdfStructureElement(tagStack.peek(), new PdfName("TH"));
		pdfContentByte.beginMarkedContentSequence(tableHeaderTag);
		tableHeaderTag.put(PdfName.K, new PdfArray());
		tagStack.push(tableHeaderTag);
		isTagEmpty = true;

		createSpanTags(element, tableHeaderTag);
	}

	protected void createTdStartTag(JRPrintElement element)
	{
		PdfStructureElement tableCellTag = new PdfStructureElement(tagStack.peek(), new PdfName("TD"));
		pdfContentByte.beginMarkedContentSequence(tableCellTag);
		tableCellTag.put(PdfName.K, new PdfArray());
		tagStack.push(tableCellTag);
		isTagEmpty = true;

		createSpanTags(element, tableCellTag);
	}

	protected void createSpanTags(JRPrintElement element, PdfStructureElement parentTag)
	{
		int colSpan = 0;
		int rowSpan = 0;
		try	{
			colSpan = Integer.valueOf(element.getPropertiesMap().getProperty(PROPERTY_TAG_COLSPAN)).intValue();
		} catch (NumberFormatException e) {
			try	{
				colSpan = Integer.valueOf(element.getPropertiesMap().getProperty(JRCellContents.PROPERTY_COLUMN_SPAN)).intValue();
			} catch (NumberFormatException ex) {}
		}
		try {
			rowSpan = Integer.valueOf(element.getPropertiesMap().getProperty(PROPERTY_TAG_ROWSPAN)).intValue();
		} catch (NumberFormatException e) {
			try {
				rowSpan = Integer.valueOf(element.getPropertiesMap().getProperty(JRCellContents.PROPERTY_ROW_SPAN)).intValue();
			} catch (NumberFormatException ex) {}
		}
		if (colSpan > 1 || rowSpan > 1)
		{
			PdfArray a = new PdfArray();
			PdfDictionary dict = new PdfDictionary();
			if (colSpan > 1)
			{
				dict.put(new PdfName("ColSpan"), new PdfNumber(colSpan));
			}
			if (rowSpan > 1)
			{
				dict.put(new PdfName("RowSpan"), new PdfNumber(rowSpan));
			}
			dict.put(PdfName.O, new PdfName("Table"));
			a.add(dict);
			parentTag.put(PdfName.A, a);
		}
	}

	protected void createEndTags(JRPrintElement element)// throws DocumentException, IOException, JRException
	{
		if (element.hasProperties())
		{
			String prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_TABLE);
			if (prop != null && (TAG_END.equals(prop) || TAG_FULL.equals(prop)))
			{
				pdfContentByte.endMarkedContentSequence();
				tagStack.pop();
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_TR);
			if (prop != null && (TAG_END.equals(prop) || TAG_FULL.equals(prop)))
			{
				pdfContentByte.endMarkedContentSequence();
				tagStack.pop();
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_TH);
			if (prop != null && (TAG_END.equals(prop) || TAG_FULL.equals(prop)))
			{
				pdfContentByte.endMarkedContentSequence();

				if (isTagEmpty)
				{
					pdfContentByte.beginMarkedContentSequence(new PdfStructureElement(tagStack.peek(), PdfName.SPAN));
					pdfContentByte.endMarkedContentSequence();
				}

				tagStack.pop();
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_TD);
			if (prop != null && (TAG_END.equals(prop) || TAG_FULL.equals(prop)))
			{
				pdfContentByte.endMarkedContentSequence();

				if (isTagEmpty)
				{
					pdfContentByte.beginMarkedContentSequence(new PdfStructureElement(tagStack.peek(), PdfName.SPAN));
					pdfContentByte.endMarkedContentSequence();
				}

				tagStack.pop();
			}

			prop = element.getPropertiesMap().getProperty(JRCellContents.PROPERTY_TYPE);
			if (
				prop != null
				&& (JRCellContents.TYPE_CROSSTAB_HEADER.equals(prop)
					|| JRCellContents.TYPE_COLUMN_HEADER.equals(prop)
					|| JRCellContents.TYPE_ROW_HEADER.equals(prop)
					|| JRCellContents.TYPE_DATA.equals(prop)))
			{
				pdfContentByte.endMarkedContentSequence();

				if (isTagEmpty)
				{
					pdfContentByte.beginMarkedContentSequence(new PdfStructureElement(tagStack.peek(), PdfName.SPAN));
					pdfContentByte.endMarkedContentSequence();
				}

				tagStack.pop();
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_H1);
			if (prop != null && (TAG_END.equals(prop) || TAG_FULL.equals(prop)))
			{
				pdfContentByte.endMarkedContentSequence();

				if (isTagEmpty)
				{
					pdfContentByte.beginMarkedContentSequence(new PdfStructureElement(tagStack.peek(), PdfName.SPAN));
					pdfContentByte.endMarkedContentSequence();
				}

				tagStack.pop();
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_H2);
			if (prop != null && (TAG_END.equals(prop) || TAG_FULL.equals(prop)))
			{
				pdfContentByte.endMarkedContentSequence();

				if (isTagEmpty)
				{
					pdfContentByte.beginMarkedContentSequence(new PdfStructureElement(tagStack.peek(), PdfName.SPAN));
					pdfContentByte.endMarkedContentSequence();
				}

				tagStack.pop();
			}

			prop = element.getPropertiesMap().getProperty(PROPERTY_TAG_H3);
			if (prop != null && (TAG_END.equals(prop) || TAG_FULL.equals(prop)))
			{
				pdfContentByte.endMarkedContentSequence();

				if (isTagEmpty)
				{
					pdfContentByte.beginMarkedContentSequence(new PdfStructureElement(tagStack.peek(), PdfName.SPAN));
					pdfContentByte.endMarkedContentSequence();
				}

				tagStack.pop();
			}
		}
	}
}


text:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Shell;
using MVVM_Tools.Code.Commands;
using TranslatorApk.Logic.Interfaces;
using TranslatorApk.Logic.Utils;

namespace TranslatorApk.Windows
{
    public partial class LoadingWindow : IRaisePropertyChanged
    {
        public Visibility CancelVisibility
        {
            get => _cancelVisibility;
            set => this.SetProperty(ref _cancelVisibility, value);
        }
        private Visibility _cancelVisibility = Visibility.Visible;

        public ICommand CancelCommand { get; }

        public bool DoFinishActions { get; set; }

        private bool _canClose;

        private static CancellationTokenSource _cancellationToken;

        private LoadingWindow()
        {
            InitializeComponent();
            TaskbarItemInfo = new TaskbarItemInfo();

            CancelCommand = new ActionCommand(_cancellationToken.Cancel);
        }

        public static void ShowWindow(
            Action beforeStarting,
            Action<CancellationToken> threadActions,
            Action finishActions,
            Visibility cancelVisibility = Visibility.Visible,
            Window ownerWindow = null)
        {
            _cancellationToken = new CancellationTokenSource();
            Application.Current.Dispatcher.Invoke(beforeStarting);

            var window = new LoadingWindow
            {
                CancelVisibility = cancelVisibility,
                DoFinishActions = true,
                Owner = ownerWindow
            };

            window.SetTaskBarState(TaskbarItemProgressState.Indeterminate);
            window.Show();

            var th = new Thread(() =>
            {
                try
                {
                    threadActions(_cancellationToken.Token);
                }
                catch (OperationCanceledException)
                { }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString(), "Error");
                    Debug.WriteLine(Environment.StackTrace, "Current Stack Trace");

                    Application.Current.Dispatcher.InvokeAction(() => throw new Exception(nameof(LoadingWindow), ex));
                }
            })
            {
                CurrentCulture = Thread.CurrentThread.CurrentCulture,
                CurrentUICulture = Thread.CurrentThread.CurrentUICulture
            };
            th.SetApartmentState(ApartmentState.STA);
            th.Start();

            Task.Factory.StartNew(() =>
            {
                try
                {
                    th.Join();

                    Application.Current.Dispatcher.InvokeAction(() =>
                    {
                        window.SetTaskBarState(TaskbarItemProgressState.None);
                        window._canClose = true;
                        window.Close();

                        if (window.DoFinishActions)
                        {
                            finishActions();
                        }
                    });
                }
                catch (Exception ex)
                {
                    Application.Current.Dispatcher.InvokeAction(() => throw new Exception(nameof(LoadingWindow), ex));
                }
            });
        }

        private void SetTaskBarState(TaskbarItemProgressState state)
        {
            TaskbarItemInfo.ProgressState = state;
        }

        private void LoadingWindow_OnClosing(object sender, CancelEventArgs e)
        {
            if (!_canClose)
            {
                e.Cancel = true;
            }
        }

        #region PropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}

redpajama_set_name: RedPajamaGithub


text:
# Frontend Dev
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Commodi odio quas soluta error perspiciatis quisquam asperiores laboriosam nihil, quo illum suscipit consectetur vel, saepe ad. Dolore excepturi dolores perferendis, assumenda.

## General Guidelines

### Avoid Multiple Files
A lot of web development seems to be about splitting things up into manageable chunks or "components". For each discrete bit of javascript functionality or each HTML partial I might make a dedicated file, then organize related files into folders named javascript and html or controllers and templates. Whatever you like. This way, you can easily dip into the file system and focus on just the file containing the code you want to edit at the time.

This doesn't work so well for CSS. Javascript function definitions can go either before or after where they are called and HTML modules can be inserted wherever you see fit in the flow of the document. CSS, on the other hand, is a chronology. It matters a great deal in what order you declare styles. Heeding the language features of inheritance and specificity, you should start with generic styles (like a font-family set on body) and progress to more specific definitions.

CSS is an ordered, exception based language and there is no easy way for a file listing (which is typically organised alphabetically) to represent it coherently. It gives you the impression that each CSS file is autonomous, which it is not.

```
- buttons.css
- reset.css
```

So you have two choices: You can either live in denial, grumbling that "specificity shouldn't be part of CSS" as you try to force a square peg into a round hole, or you can work in one, well-commented file that clearly represents the arc of your inheritance-harnessing cascade. Specificity should never be a problem because the most specific selectors should be the last ones you write.

### Nesting
If it possible, always avoid writing too much selectors for referencing on CSS. 3 depths selector is fine. But always keep on mind to not make it too general. It will cause problems with other selectors.

Good:
```css
#header .navbar .navbar-menu {
  ...
}
```

Bad (too specific):
```css
#header .top .navbar .navbar-left .navbar-menu {
  ...
}

```

Bad (too general):
``````css
.navbar-menu {
  ...
}
```

### Commenting
The most important thing when it comes to code etiquette is commenting. Comment everything, even if it seems obvious to you it needs to be commented. This is especially true for the main javascript file.

Code is written and maintained by people. Ensure your code is descriptive, well commented, and approachable by others. Great code comments convey context or purpose. Do not simply reiterate a component or class name.

Be sure to write in complete sentences for larger comments and succinct phrases for general notes.

Good:
```css
/* Modal header */
.modal-header {
  ...
}
```

Bad:
```css
/* Wrapping element for .modal-title and .modal-close */
.modal-header {
  ...
}
```

### Reduce image file size

Reduce image file size is by using the "Save for Web" command in Adobe Photoshop. When using this command, you want to adjust the image to the lowest file size acceptable while keeping an eye out for image quality.

A good rule of thumb for ecommerce images is to try to keep your image file size below 70kb. That can be difficult sometimes, especially for larger images, so you can use larger file size no more than 200kb.

## HTML

### Readjust HTML
You FrontEnds are allowed to readjust the HTML whenever you think the mockup of the new site has a different layout with the template. Believe me, the good structure will save your time on styling, moreover while facing IE.

### Reducing Markup
Whenever possible, avoid superfluous parent elements when writing HTML. Many times this requires iteration and refactoring, but produces less HTML.

Good:
```html

```

Bad:
```html

```
### JS Generated Markup
Writing markup in a JavaScript file makes the content harder to find, harder to edit, and less performant. Avoid it whenever possible.

### Setting the Viewport

HTML5 introduced a method to let web designers take control over the viewport, through the <meta> tag.

You should include the following <meta> viewport element in all your web pages:

```html
<meta name="viewport" content="width=device-width, initial-scale=1.0">
```

A <meta> viewport element gives the browser instructions on how to control the page's dimensions and scaling.

The width=device-width part sets the width of the page to follow the screen-width of the device (which will vary depending on the device).

The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by the browser.

Users are used to scroll websites vertically on both desktop and mobile devices - but not horizontally!

So, if the user is forced to scroll horizontally, or zoom out, to see the whole web page it results in a poor user experience.

Some additional rules to follow:

1. Do NOT use large fixed width elements - For example, if an image is displayed at a width wider than the viewport it can cause the viewport to scroll horizontally. Remember to adjust this content to fit within the width of the viewport.

2. Do NOT let the content rely on a particular viewport width to render well - Since screen dimensions and width in CSS pixels vary widely between devices, content should not rely on a particular viewport width to render well.

3. Use CSS media queries to apply different styling for small and large screens - Setting large absolute CSS widths for page elements, will cause the element to be too wide for the viewport on a smaller device. Instead, consider using relative width values, such as width: 100%. Also, be careful of using large absolute positioning values. It may cause the element to fall outside the viewport on small devices.

## Naming
Always use a semantic approach to CSS or JS up to a certain point. The idea is for you to be able to read them and get some idea of what the HTML would look like.

### CSS Selectors
Class names should all be lower case, with hyphens as spaces. So use work-grid, not WorkGrid or work_grid.

Good:
```css
  .block-slideshow {
    font-size: 22px;
    margin-top: 33px;
  }
```

Bad:
```css
  .blockSlideshow {
    font-size: 22px;
    margin-top: 33px;
  }

  .block_footer {
    background-color: #000;
  }
```

### JS Variable
Variables should be camel cased like initMenu or initVideoDetail.

Good:
```javascript
  var headerHTML = jQuery('#header').html();
```

Bad:
```javascript
  var header-HTML = jQuery('#header').html();
  var footer_visibility = false;
```

### Image Icon Naming
Icons and image assets should be named describing what they are, not what they represent. Use hyphens as spaces, never use underscore or camel cased naming. A logo name should starts with "logo" and and icon name starts with "icon".

Good:
```
logo-small.svg
icon-envelope.svg
icon-arrow-left.svg
```

Bad:
```
icon-email.svg
icon-previous.svg
icon_arrow.svg
logoLarge.svg
```

## CSS

### Prefixing
Don't forget to always prefixing the stylesheet to avoid the cross browser issues on the future. There are numbers of CSS auto prefixing tools app or online services that can do that, like:
- Prepos (App, available for windows, linux and OSX)
- http://autoprefixer.github.io

### Indenting
Use soft tabs with four spaces. It will make the code easy to read.

Good:
```css
footer {
    text-align: center;
    padding: 20px;
    font-size: 14px;
    color: #8E8E8E;
}

footer a {
    color: #2e7e83;
}
```

Bad:
```css
footer {
text-align: center;
padding: 20px;
font-size: 14px;
color: #8E8E8E;
}

footer a {
color: #2e7e83;
}
```

### Unit sizes

CSS has four different unit sizes you can use for your margins, borders, fonts, etc. These are pixels (px), points (pt), ems (em) and percentages (%).

We can separate these units in two different groups, fixed and relative. Pixels (px) and points (pt) are fixed, whereas em and percents(%) are relative unit sizes. Relative unit sizes are really powerful when creating scalable layouts. Fixed size units are good to use when you always want the same element size no matter where you place it.

CSS3 introduces a few new units, including the rem unit, which stands for "root em".  The rem unit is relative to the root—or the html—element.  It fixes the problem of sizing relative to the context encountered when using ems.

Point values are only for print! A point is a unit of measurement used for real-life ink-on-paper typography. 72pts = one inch. One inch = one real-life inch like-on-a-ruler. Not an inch on a screen, which is totally arbitrary based on resolution. Point (pt) is not recommended to use in web pages since cross-browser results can be very different.

### Preprocessors
Use your preferred CSS preprocessor. You can use SASS or LESS, but keep in mind that always be consistent. If the last developer was using SASS, please keep using it when you continue the project.

Keep attention to this list when working with CSS preprocessor:
- Don't use too much nesting. It will make your compiled CSS become too big.
- Don't minify the compiled CSS unless the project is on production stage.
- Always put variable if possible. Variable could be color code, background color, font name.

### CSS Reset
CSS reset is a must! It will prevent issues with cross browser and make things easier to style. We prefer to use normalize.css to resetting the css.

Don't forget to reset box-sizing to border-box for every element This allows us to more easily assign widths to elements that also have padding and borders

```css
* {
    box-sizing: border-box;
}
```

## Javascript

### Setup
Put all the main JavaScript for the site in one file.

To avoid any namespace issues and keep things simple, all the main logic of the site gets wrapped in one large object. This allows you to turn off entire sections of the code, or change the run order, which makes debugging easier. It has the added benefit of providing a clear way to safely set global values. Look at this example:
```javascript
var app = {
    init: function() {
        // Size things
        app.setWinSize();
    },

    setWinSize: function({
        // Set window size
        app.winHeight = jQuery(window).height();
        app.winWidth = jQuery(window).width();
    }
};
jQuery(document).ready(function($){
    app.init();
    jQuery(window).resize(function(){
        app.onResize();
    });
});
```
## Responsive Web Design

### Responsive width

Setting the width of a block-level element will prevent it from stretching out to the edges of its container to the left and right. Then, you can set the left and right margins to auto to horizontally center that element within its container. The element will take up the width you specify, then the remaining space will be split evenly between the two margins.

The only problem occurs when the browser window is narrower than the width of your element. The browser resolves this by creating a horizontal scrollbar on the page.

Using max-width instead of width in this situation will improve the browser's handling of small windows. This is important when making a site usable on mobile. Resize this page to check it out!

By the way, max-width is supported by all major browsers including IE7+ so you shouldn't be afraid of using it.

Use percentage as width and give maximum width in pixel

Good:
```css
.wrapper {
   width:100%;
   max-width: 1024px;
   margin: 0 auto;
}
```

Bad:
```css
.wrapper {
   width: 1024px;
   margin: 0 auto;
}

@media (max-width: 1024px) {
  .wrapper {
    width: 100%;
  }
}
```

### Responsive image

Make sure when you crop ALL images across the sites you DO NOT play with the ratio of the images. NEVER stretch images. You reduce the width of an image so it is the same size as the container and then the height is reduced in the same ratio. If the height is then bigger than the container you hide the top and bottom of the image through the css.

example:

html:
```html

```

css:
```css
.image {
  height: 0;
  overflow: hidden;
  padding-bottom: 70%;
  position: relative;
  width: 100%;
}

span {
  height: 200%;
  left: -50%;
  position: absolute;
  top: -50%;
  width: 200%;
}

img {
  bottom: 0;
  left: 0;
  margin: auto;
  min-height: 50%;
  min-width: 50%;
  position: absolute;
  right: 0;
  top: 0;
}
```

### Box sizing

Over the generations, people realized that math is not fun, so a new CSS property called box-sizing was created. When you set box-sizing: border-box; on an element, the padding and border of that element no longer increase its width. Here is the same example as the previous page, but with box-sizing: border-box; on both elements:

sample:
```css
.border-box {
  width: 500px;
  padding: 50px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
```

Since this is so much better, some authors want all elements on all their pages to always work this way. Such authors put the following CSS on their pages:
```css
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
```

This ensures that all elements are always sized in this more intuitive way.

Since box-sizing is pretty new, you should use the -webkit- and -moz- prefixes for now, as I have in these examples. This technique enables experimental features in specific browsers. Also, keep in mind that this one is IE8+.

### Breakpoint

Adding a breakpoint when the content is no longer easy to consume.

With this fundamental shift in thinking we no longer have to fear a new device — a new iPhone width — because we've already fixed our sites to work everywhere.

### PX Unit
Managing sizes using pixels in responsive design is an absolute hog. The lack of relativity between separate elements' sizes means you have to treat each setting as a separate concern, for each separate breakpoint. In fact, using pixels you even have to manage the font size and margin of single, isolated elements separately. You really don't want to do that.

Take the following example:
```css
@media (min-width: 400px) {
  h1 {
    font-size: 22px;
    margin-top: 33px;
  }
}
```

How would we write that using relative units? We wouldn't. In fact, we wouldn't have to address font sizes or margins for any individual elements in any of our @media queries. We'd simply let the browser or user determine the font-size on the root element (<html>) and set all my other dimensions in ems or rems.

```css
h1, h2 { margin-top: 1.5rem; }
h1 { font-size: 2.5em; }
h2 { font-size: 2em; }
/* all your other element styles */
```

Then, when we wanted to scale things up at a min-width breakpoint, we'd just adjust the root font size, upon which everything else is proportionately based. I use a percentage value because this is relative to the user's preference, if set:

```css
@media (min-width: 400px) { /* "Should be ems too!" - lots of folks :-) */
  html {
    font-size: 120%;
  }
}
```

That's it. That's 90% of your responsive design strategy right there. For bonus points, you can set a common margin between all your sibling flow elements using a selector.

```css
body * + * {
  margin-top: 1.5rem;
}
```

## Performance
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cumque atque ipsum omnis explicabo modi iste autem saepe alias natus odit. Perspiciatis aliquam numquam corporis minima fugiat consequatur a sit. Incidunt.

## Testing
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur autem voluptatum blanditiis in rem. Sapiente id perspiciatis earum mollitia quibusdam, fugit a officia! Eum optio est sed, quaerat quis dolor.

### Basic Checklist
Use your responsive media view in your browser and resize it. If all look good when you resize it, then it would not cause much trouble when QA check it on real devices.

### Code Checklist
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur autem voluptatum blanditiis in rem. Sapiente id perspiciatis earum mollitia quibusdam, fugit a officia! Eum optio est sed, quaerat quis dolor.

### To Do List
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur autem voluptatum blanditiis in rem. Sapiente id perspiciatis earum mollitia quibusdam, fugit a officia! Eum optio est sed, quaerat quis dolor.


text:
"""Raykar et al. (2010) EM crowd learning algorithm.

Matthew Alger
The Australian National University
2016
"""

import logging
import time

import numpy
import scipy.optimize
import sklearn.linear_model

from crowdastro.crowd.util import majority_vote, logistic_regression

EPS = 1E-8

class RaykarClassifier(object):
    """Classifier based on the Raykar et al. (2010) EM algorithm.

    Jointly learns an annotator model and a classification model.
    """

    def __init__(self, n_restarts=5, epsilon=1e-5, lr_init=True):
        """
        n_restarts: Number of times to run the algorithm. Higher numbers improve
            chances of finding a global maximum likelihood solution.
        epsilon: Convergence threshold.
        lr_init: Whether to initialise w using logistic regression.
        """
        self.n_restarts = n_restarts
        self.epsilon = epsilon
        self.lr_init = lr_init

    def fit(self, X, Y):
        """
        X: (n_samples, n_features) NumPy array of data.
        Y: (n_labellers, n_samples) NumPy masked array of crowd labels.
        """
        if X.shape[0] != Y.shape[1]:
            raise ValueError('X and Y have different numbers of samples.')

        results = []
        for trial in range(self.n_restarts):
            logging.debug('Trial {}/{}'.format(trial + 1, self.n_restarts))
            a, b, w = self._fit_params(X, Y)
            self.a_, self.b_, self.w_ = a, b, w
            lh = self.score(X, Y)
            results.append((lh, (a, b, w)))

        a, b, w = max(results, key=lambda z: z[0])[1]
        self.a_ = a
        self.b_ = b
        self.w_ = w

    def _fit_params(self, x, y):
        """
        x: (n_samples, n_features) NumPy array of data (with no bias term).
        y: (n_labellers, n_samples) NumPy masked array of crowd labels.
        """
        # Add a bias feature.
        x = numpy.hstack([x, numpy.ones((x.shape[0], 1))])

        n_samples, n_dim = x.shape
        n_labellers, n_samples_ = y.shape
        if n_samples_ != n_samples:
            raise ValueError('x and y have different numbers of samples.')

        self.n_samples_ = n_samples
        self.n_labellers_ = n_labellers
        self.n_dim_ = n_dim

        # Compute majority vote labels for initialisation.
        mv = majority_vote(y)
        m = mv.copy()
        # Add a small random factor for variety.
        m[m == 1] -= numpy.abs(numpy.random.normal(scale=1e-2,
                                                   size=m[m == 1].shape[0]))
        m[m == 0] += numpy.abs(numpy.random.normal(scale=1e-2,
                                                   size=m[m == 0].shape[0]))

        # Convert y into a dense array and a mask. Then we can ignore the mask
        # when we don't need it and get nice fast code (numpy.ma is quite slow).
        y_mask = y.mask
        y_1 = y.filled(1)
        y = y_0 = y.filled(0)

        w = None
        a = self._max_alpha_step(m, y, y_mask)
        b = self._max_beta_step(m, y, y_mask)

        while True:
            then = time.time()
            # Maximisation step.
            # w first, so the optimisation uses the old parameters.
            w = self._max_w_step(a, b, m, x, y_0, y_1, mv, init_w=w)
            a = self._max_alpha_step(m, y, y_mask)
            b = self._max_beta_step(m, y, y_mask)

            # Expectation step.
            m_ = self._exp_m_step(a, b, w, x, y, y_mask)

            logging.debug('Current value of delta mu: %f',
                          numpy.linalg.norm(m_ - m))

            dm = numpy.linalg.norm(m_ - m)
            if dm < self.epsilon:
                logging.debug('a: {}'.format(a))
                logging.debug('b: {}'.format(b))
                return a, b, w

            m = m_

            # Estimate time remaining.
            now = time.time()
            dt = now - then
            logging.debug('Raykar iteration took {} s.'.format(dt))

    def _exp_m_step(self, a, b, w, x, y, y_mask):
        """Computes expectation value of μ."""
        lr = logistic_regression(w, x)
        exp_a = numpy.ones((x.shape[0],))
        exp_b = numpy.ones((x.shape[0],))
        for t in range(a.shape[0]):
            for i in range(x.shape[0]):
                if y_mask[t, i]:
                    continue

                exp_a[i] *= a[t] ** y[t, i] * (1 - a[t]) ** (1 - y[t, i])
                exp_b[i] *= b[t] ** (1 - y[t, i]) * (1 - b[t]) ** y[t, i]

        logging.debug('Average a_i: {:.02}'.format(exp_a.mean()))
        logging.debug('Average alpha_t: {:.02}'.format(a.mean()))
        logging.debug('Max alpha_t: {}'.format(a.max()))
        logging.debug('Min alpha_t: {}'.format(a.min()))

        return exp_a * lr / (exp_a * lr + exp_b * (1 - lr) + EPS)

    def _hessian_inverse_multiply(self, x, H, g):
        return numpy.linalg.norm(H.dot(x) - g)

    def _max_w_step(self, a, b, m, x, y_0, y_1, mv, init_w=None):
        """Computes w based on μ.

        m: μ
        x: (n_samples, n_features) NumPy array of examples.
        y_0, y_1: Array of crowd labels filled with 0 or 1.
        mv: Majority vote of labels.
        init_w: Initial value of w.
        -> w
        """
        n_samples, n_features = x.shape

        if init_w is None and not self.lr_init:
            w = numpy.random.normal(size=(x.shape[1],))
        elif init_w is None:
            lr = sklearn.linear_model.LogisticRegression(
                    class_weight='balanced', fit_intercept=False)
            lr.fit(x, mv)
            w = lr.coef_.ravel()
        else:
            w = init_w

        w = scipy.optimize.fmin_bfgs(self._log_likelihood, w,
                                     args=(a.reshape((-1, 1)),
                                           b.reshape((-1, 1)), x, y_0, y_1),
                                     disp=False)
        return w

    def _max_alpha_step(self, m, y, y_mask):
        """Computes α based on μ.

        m: μ
        y: Array of crowd labels.
        y_mask: Mask of unobserved crowd labels.
        -> α
        """
        a = numpy.zeros((y.shape[0],))
        divisor = numpy.zeros((y.shape[0],))
        for t in range(y.shape[0]):
            for i in range(y.shape[1]):
                if y_mask[t, i]:
                    continue

                a[t] += m[i] * y[t, i]
                divisor[t] += m[i]

        divisor[divisor == 0] = EPS
        return a / divisor

    def _max_beta_step(self, m, y, y_mask):
        """Computes β based on μ.

        m: μ
        y: Array of crowd labels.
        y_mask: Mask of unobserved crowd labels.
        -> β
        """
        b = numpy.zeros((y.shape[0],))
        divisor = numpy.zeros((y.shape[0],))
        for t in range(y.shape[0]):
            for i in range(y.shape[1]):
                if y_mask[t, i]:
                    continue

                b[t] += (1 - m[i]) * (1 - y[t, i])
                divisor[t] += (1 - m[i])

        divisor[divisor == 0] = EPS
        return b / divisor

    def predict(self, X):
        return self.predict_proba(X).round()

    def predict_proba(self, X):
        X = numpy.hstack([X, numpy.ones((X.shape[0], 1))])
        return logistic_regression(self.w_, X)

    def score(self, X, Y):
        """Computes the likelihood of labels and data under the model.

        X: (n_samples, n_features) NumPy array of data.
        Y: (n_labellers, n_samples) NumPy masked array of crowd labels.
        """
        X = numpy.hstack([X, numpy.ones((X.shape[0], 1))])
        return self._likelihood(self.w_, self.a_.reshape((-1, 1)),
                                self.b_.reshape((-1, 1)), X, Y.filled(0),
                                Y.filled(1))

    def _log_likelihood(self, *args, **kwargs):
        return numpy.log(self._likelihood(*args, **kwargs) + EPS)

    def _likelihood(self, w, a, b, X, Y_0, Y_1):
        """Computes the likelihood of labels and data under a model.

        X: (n_samples, n_features) NumPy array of data.
        Y: (n_labellers, n_samples) NumPy masked array of crowd labels.
        """
        n_examples = X.shape[0]
        exp_p = logistic_regression(w, X)
        exp_a = numpy.ones((n_examples,))
        exp_b = numpy.ones((n_examples,))
        exp_a = numpy.power(a, Y_0).prod(axis=0)
        exp_a *= numpy.power(1 - a, 1 - Y_1).prod(axis=0)
        exp_b *= numpy.power(b, 1 - Y_1).prod(axis=0)
        exp_b *= numpy.power(1 - b, Y_0).prod(axis=0)

        return (exp_a * exp_p.T + exp_b * (1 - exp_p).T).prod()

    def get_params(self, deep=True):
        return {
            'n_restarts': self.n_restarts,
            'epsilon': self.epsilon,
            'lr_init': self.lr_init,
        }

    def set_params(self, **parameters):
        for parameter, value in parameters.items():
            setattr(self, parameter, value)
        return self

    def serialise(self):
        """Returns a NumPy array representing the optimised parameters."""
        return numpy.concatenate([
                [self.n_labellers_],
                self.a_.ravel(),
                self.b_.ravel(),
                self.w_.ravel(),
        ])

    @classmethod
    def unserialise(cls, array):
        """Converts a NumPy array into a RaykarClassifier."""
        rc = cls()
        n_annotators = int(array[0])
        array = array[1:]
        rc.a_ = array[:n_annotators]
        rc.b_ = array[n_annotators:n_annotators * 2]
        rc.w_ = array[n_annotators * 2:]
        return rc


text:
set -e

echo $DRUID_INTEGRATION_TEST_OVERRIDE_CONFIG_PATH

export DIR=$(cd $(dirname $0) && pwd)
export HADOOP_DOCKER_DIR=$DIR/../examples/quickstart/tutorial/hadoop/docker

if [ -n "${HADOOP_VERSION}" ] && [ "${HADOOP_VERSION:0:1}" == "3" ]; then
   export HADOOP_DOCKER_DIR=$DIR/../examples/quickstart/tutorial/hadoop3/docker
fi

export DOCKERDIR=$DIR/docker
export SHARED_DIR=${HOME}/shared

# so docker IP addr will be known during docker build
echo ${DOCKER_IP:=127.0.0.1} > $DOCKERDIR/docker_ip

if !($DRUID_INTEGRATION_TEST_SKIP_BUILD_DOCKER); then
  bash ./gen-scripts/copy_resources.sh
  bash ./script/docker_build_containers.sh
fi

if !($DRUID_INTEGRATION_TEST_SKIP_RUN_DOCKER); then
  bash ./stop_cluster.sh
  bash ./script/docker_run_cluster.sh
fi

if ($DRUID_INTEGRATION_TEST_START_HADOOP_DOCKER); then
  bash ./script/copy_hadoop_resources.sh
fi


text:
IMAGE=gcr.io/google-containers/kube-addon-manager
VERSION=v1
KUBECTL_VERSION=v1.2.3

.PHONY: build push container

build: kubectl
	docker build -t "$(IMAGE):$(VERSION)" .

kubectl:
	curl "https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl" \
		-o kubectl
	chmod +x kubectl

push: build
	gcloud docker push "$(IMAGE):$(VERSION)"

clean:
	rm kubectl
	docker rmi -f "$(IMAGE):$(VERSION)"


text:
package com.vaadin.tests.themes.valo;

import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.shared.ui.colorpicker.Color;
import com.vaadin.ui.AbstractColorPicker.PopupStyle;
import com.vaadin.ui.ColorPicker;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;

public class ColorPickers extends VerticalLayout implements View {
    public ColorPickers() {
        setMargin(true);

        Label h1 = new Label("Color Pickers");
        h1.addStyleName(ValoTheme.LABEL_H1);
        addComponent(h1);

        HorizontalLayout row = new HorizontalLayout();
        row.addStyleName(ValoTheme.LAYOUT_HORIZONTAL_WRAPPING);
        row.setSpacing(true);
        addComponent(row);

        TestIcon testIcon = new TestIcon(40);

        ColorPicker cp = new ColorPicker();
        cp.setDefaultCaptionEnabled(true);
        cp.setIcon(testIcon.get());
        cp.setColor(new Color(138, 73, 115));
        row.addComponent(cp);

        cp = new ColorPicker();
        cp.setPopupStyle(PopupStyle.POPUP_SIMPLE);
        cp.setTextfieldVisibility(true);
        row.addComponent(cp);
    }

    @Override
    public void enter(ViewChangeEvent event) {
        // TODO Auto-generated method stub

    }

}


text:
@interface ITShapes : NSObject

+ (UIBezierPath *) bezierPathForBackButtonInRect:(CGRect)rect withRoundingRadius:(CGFloat)radius andArrowWidth:(CGFloat)arrowWidth;
+ (UIBezierPath *) bezierPathForInnerGlowBackButtonInRect:(CGRect)rect withRoundingRadius:(CGFloat)radius andArrowWidth:(CGFloat)arrowWidth;

@end


text:
import os

import pytest
from funcy import first, get_in

from dvc import api
from dvc.exceptions import FileMissingError, OutputNotFoundError
from dvc.utils.fs import remove
from tests.unit.fs.test_repo import make_subrepo

cloud_names = [
    "s3",
    "gs",
    "azure",
    "gdrive",
    "oss",
    "ssh",
    "http",
    "hdfs",
    "webdav",
    "webhdfs",
]
clouds = [pytest.lazy_fixture(cloud) for cloud in cloud_names]
all_clouds = [pytest.lazy_fixture("local_cloud")] + clouds

# `lazy_fixture` is confusing pylint, pylint: disable=unused-argument

@pytest.mark.needs_internet
@pytest.mark.parametrize("remote", clouds, indirect=True)
def test_get_url(tmp_dir, dvc, remote):
    tmp_dir.dvc_gen("foo", "foo")

    expected_url = (remote / "ac/bd18db4cc2f85cedef654fccc4a4d8").url
    assert api.get_url("foo") == expected_url

@pytest.mark.needs_internet
@pytest.mark.parametrize("cloud", clouds)
def test_get_url_external(tmp_dir, erepo_dir, cloud):
    erepo_dir.add_remote(config=cloud.config)
    with erepo_dir.chdir():
        erepo_dir.dvc_gen("foo", "foo", commit="add foo")

    # Using file url to force clone to tmp repo
    repo_url = f"file://{erepo_dir}"
    expected_url = (cloud / "ac/bd18db4cc2f85cedef654fccc4a4d8").url
    assert api.get_url("foo", repo=repo_url) == expected_url

def test_get_url_requires_dvc(tmp_dir, scm):
    tmp_dir.scm_gen({"foo": "foo"}, commit="initial")

    with pytest.raises(OutputNotFoundError, match="output 'foo'"):
        api.get_url("foo", repo=os.fspath(tmp_dir))

    with pytest.raises(OutputNotFoundError, match="output 'foo'"):
        api.get_url("foo", repo=f"file://{tmp_dir}")

@pytest.mark.needs_internet
@pytest.mark.parametrize("remote", all_clouds, indirect=True)
def test_open(tmp_dir, dvc, remote):
    tmp_dir.dvc_gen("foo", "foo-text")
    dvc.push()

    # Remove cache to force download
    remove(dvc.odb.local.cache_dir)

    with api.open("foo") as fd:
        assert fd.read() == "foo-text"

@pytest.mark.needs_internet
@pytest.mark.parametrize(
    "cloud",
    [
        pytest.lazy_fixture(cloud)
        for cloud in [
            "s3",
            "gs",
            "azure",
            "gdrive",
            "oss",
            "http",
            "hdfs",
            "ssh",
            "webdav",
        ]
    ],
)
def test_open_external(tmp_dir, erepo_dir, cloud):
    erepo_dir.add_remote(config=cloud.config)

    with erepo_dir.chdir():
        erepo_dir.dvc_gen("version", "master", commit="add version")

        with erepo_dir.branch("branch", new="True"):
            # NOTE: need file to be other size for Mac
            erepo_dir.dvc_gen("version", "branchver", commit="add version")

    erepo_dir.dvc.push(all_branches=True)

    # Remove cache to force download
    remove(erepo_dir.dvc.odb.local.cache_dir)

    # Using file url to force clone to tmp repo
    repo_url = f"file://{erepo_dir}"
    with api.open("version", repo=repo_url) as fd:
        assert fd.read() == "master"

    assert api.read("version", repo=repo_url, rev="branch") == "branchver"

@pytest.mark.needs_internet
@pytest.mark.parametrize("remote", all_clouds, indirect=True)
def test_open_granular(tmp_dir, dvc, remote):
    tmp_dir.dvc_gen({"dir": {"foo": "foo-text"}})
    dvc.push()

    # Remove cache to force download
    remove(dvc.odb.local.cache_dir)

    with api.open("dir/foo") as fd:
        assert fd.read() == "foo-text"

@pytest.mark.needs_internet
@pytest.mark.parametrize(
    "remote",
    [
        pytest.lazy_fixture(cloud)
        for cloud in [
            "s3",
            "gs",
            "azure",
            "gdrive",
            "oss",
            "ssh",
            "http",
            "hdfs",
            "webdav",
        ]
    ],
    indirect=True,
)
def test_missing(tmp_dir, dvc, remote):
    tmp_dir.dvc_gen("foo", "foo")

    # Remove cache to make foo missing
    remove(dvc.odb.local.cache_dir)

    api.read("foo")

    remove("foo")

    with pytest.raises(FileMissingError):
        api.read("foo")

def test_open_scm_controlled(tmp_dir, erepo_dir):
    erepo_dir.scm_gen({"scm_controlled": "file content"}, commit="create file")

    with api.open("scm_controlled", repo=os.fspath(erepo_dir)) as fd:
        assert fd.read() == "file content"

def test_open_not_cached(dvc):
    metric_file = "metric.txt"
    metric_content = "0.6"
    metric_code = "open('{}', 'w').write('{}')".format(
        metric_file, metric_content
    )
    dvc.run(
        single_stage=True,
        metrics_no_cache=[metric_file],
        cmd=(f'python -c "{metric_code}"'),
    )

    with api.open(metric_file) as fd:
        assert fd.read() == metric_content

    os.remove(metric_file)
    with pytest.raises(FileMissingError):
        api.read(metric_file)

def test_open_rev(tmp_dir, scm, dvc):
    tmp_dir.scm_gen("foo", "foo", commit="foo")

    (tmp_dir / "foo").write_text("bar")

    with api.open("foo", rev="master") as fobj:
        assert fobj.read() == "foo"

@pytest.mark.parametrize("as_external", [True, False])
@pytest.mark.parametrize("remote", [pytest.lazy_fixture("ssh")], indirect=True)
@pytest.mark.parametrize(
    "files, to_read",
    [
        ({"foo": "foo"}, "foo"),
        ({"dir": {"foo": "foo", "bar": "bar"}}, os.path.join("dir", "foo")),
    ],
    ids=["file", "inside-dir"],
)
def test_api_missing_local_cache_exists_on_remote(
    tmp_dir, scm, dvc, as_external, remote, files, to_read
):
    tmp_dir.dvc_gen(files, commit="DVC track files")
    dvc.push()

    # Remove cache to make foo missing
    remove(dvc.odb.local.cache_dir)
    remove(first(files))

    repo_url = f"file://{tmp_dir}" if as_external else None
    file_content = get_in(files, to_read.split(os.sep))
    assert api.read(to_read, repo=repo_url) == file_content

@pytest.mark.parametrize("local_repo", [False, True])
def test_read_with_subrepos(tmp_dir, scm, local_cloud, local_repo):
    tmp_dir.scm_gen("foo.txt", "foo.txt", commit="add foo.txt")
    subrepo = tmp_dir / "dir" / "subrepo"
    make_subrepo(subrepo, scm, config=local_cloud.config)
    with subrepo.chdir():
        subrepo.scm_gen({"lorem": "lorem"}, commit="add lorem")
        subrepo.dvc_gen({"dir": {"file.txt": "file.txt"}}, commit="add dir")
        subrepo.dvc_gen("dvc-file", "dvc-file", commit="add dir")
        subrepo.dvc.push()

    repo_path = None if local_repo else f"file:///{tmp_dir}"
    subrepo_path = os.path.join("dir", "subrepo")

    assert api.read("foo.txt", repo=repo_path) == "foo.txt"
    assert (
        api.read(os.path.join(subrepo_path, "lorem"), repo=repo_path)
        == "lorem"
    )
    assert (
        api.read(os.path.join(subrepo_path, "dvc-file"), repo=repo_path)
        == "dvc-file"
    )
    assert (
        api.read(os.path.join(subrepo_path, "dir", "file.txt"), repo=repo_path)
        == "file.txt"
    )

def test_get_url_granular(tmp_dir, dvc, s3):
    tmp_dir.add_remote(config=s3.config)
    tmp_dir.dvc_gen(
        {"dir": {"foo": "foo", "bar": "bar", "nested": {"file": "file"}}}
    )

    expected_url = (s3 / "5f/c28ea78987408341668eba6525ebd1.dir").url
    assert api.get_url("dir") == expected_url

    expected_url = (s3 / "ac/bd18db4cc2f85cedef654fccc4a4d8").url
    assert api.get_url("dir/foo") == expected_url

    expected_url = (s3 / "37/b51d194a7513e45b56f6524f2d51f2").url
    assert api.get_url("dir/bar") == expected_url

    expected_url = (s3 / "8c/7dd922ad47494fc02c388e12c00eac").url
    assert api.get_url(os.path.join("dir", "nested", "file")) == expected_url

def test_get_url_subrepos(tmp_dir, scm, local_cloud):
    subrepo = tmp_dir / "subrepo"
    make_subrepo(subrepo, scm, config=local_cloud.config)
    with subrepo.chdir():
        subrepo.dvc_gen(
            {"dir": {"foo": "foo"}, "bar": "bar"}, commit="add files"
        )
        subrepo.dvc.push()

    expected_url = os.fspath(
        local_cloud / "ac" / "bd18db4cc2f85cedef654fccc4a4d8"
    )
    assert api.get_url(os.path.join("subrepo", "dir", "foo")) == expected_url

    expected_url = os.fspath(
        local_cloud / "37" / "b51d194a7513e45b56f6524f2d51f2"
    )
    assert api.get_url("subrepo/bar") == expected_url


text:
package ar.com.circuloodontochaco.co.dto;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import ar.com.circuloodontochaco.co.model.Factura;

/**
 * @author Jorge E. Villaverde
 *
 */
public class FacturaDto extends MonetaryDto {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	// Properties -------------------------------------------------------------

	private final List<LineaFacturaDto> lineas;

	// Constructor ------------------------------------------------------------

	public FacturaDto(String numero,
			Date fecha,
			String domicilio,
			String razonSocial,
			String formaPago,
			String iva,
			String cuit,
			String telefono,
			String localidad,
			String provincia,
			String codigoPostal,
			String matricula,
			BigDecimal total,
			String totalTexto,
			List<LineaFacturaDto> lineas) {
		super(numero,
			  fecha,
			  domicilio,
			  razonSocial,
			  formaPago,
			  iva,
			  cuit,
			  telefono,
			  localidad,
			  provincia,
			  codigoPostal,
			  matricula,
			  total,
			  totalTexto);
		this.lineas = lineas;
	}

	// Getters/Setters --------------------------------------------------------

	public List<LineaFacturaDto> getLineas() {
		return lineas;
	}

	// Static Methods ---------------------------------------------------------

	public static FacturaDto fromFactura(Factura factura, String totalTexto) {

		String matricula = "";
		if(factura.getPersona() != null && factura.getPersona().getOdontologo() != null) {
			matricula = String.valueOf(factura.getPersona().getOdontologo().getMatricula());
		}

		return new FacturaDto(
				String.valueOf(factura.getId()),
				factura.getFecha(),
				factura.getDireccion(),
				factura.getRazonSocial(),
				factura.getFormaPago().getDescripcion(),
				factura.getIva().getName(),
				formatCUIT(factura.getCuit()),
				factura.getTelefono(),
				factura.getLocalidad(),
				factura.getProvincia(),
				factura.getCodigoPostal(),
				matricula,
				factura.getImporte(),
				totalTexto,
				LineaFacturaDto.fromFactura(factura));

	}
}


text:
'use strict';

const animateOptions = {
  duration: 100,
  fill: 'forwards',
  easing: 'ease'
};

customElements.define('view-source', class ViewSource extends HTMLElement {
  constructor() {
    super();
    this._built = false;

    const shadowRoot = this.attachShadow({mode: 'open'});

    shadowRoot.innerHTML = `
      <link rel=stylesheet href="resources/highlight-default.css">

      <button>
        ▶
        View source
      </button>

    `;

    this._spinnyArrow = shadowRoot.querySelector('#spinny-arrow');
    this._label = shadowRoot.querySelector('#label');
    this._source = shadowRoot.querySelector('#source');

    const button = shadowRoot.querySelector('button');
    let shown = false;
    button.onclick = () => {
      if (shown) {
        this.hide();
        shown = false;
        return;
      }

      this.show();
      shown = true;
    };
  }

  hide() {
    this._spinnyArrow.animate({
      transform: ['rotate(90deg)', 'rotate(0deg)']
    }, animateOptions);

    const currentHeight = this._source.clientHeight;
    this._source.animate({
      height: [`${currentHeight}px`, '0px'],
      paddingTop: ['0.5em', '0'],
      paddingBottom: ['0.5em', '0']
    }, animateOptions);
  }

  show() {
    this._spinnyArrow.animate({
      transform: ['rotate(0deg)', 'rotate(90deg)']
    }, animateOptions);

    const currentHeight = this._source.clientHeight;
    this._source.style.overflow = 'hidden';
    this._source.animate({
      height: ['0', 'auto'],
      paddingTop: ['0', '0.5em'],
      paddingBottom: ['0', '0.5em']
    }, animateOptions);
  }

  async connectedCallback() {
    if (this._built) {
      return;
    }

    const res = await fetch(location.href);
    const text = await res.text();

    const [,snippedText] = /\/\/ BEGIN SOURCE TO VIEW([\s\S]+)\/\/ END SOURCE TO VIEW/.exec(text);

    this._source.textContent = snippedText.trim();
    hljs.highlightBlock(this._source);
    this._built = true;
  }
});


text:
class Api::ConfigController < ApplicationController
  def index
    render json: {
      recaptcha_site_key: ENV['RECAPTCHA_SITE_KEY'],
      image_max_size: MAX_IMAGE_KB_SIZE.kilobytes,
      message_max_wordcount: MAX_POST_MESSAGE_WORDCOUNT
    }
  end
end


text:
from sympy.concrete.products import Product
from sympy.concrete.summations import Sum
from sympy.core.function import Derivative
from sympy.core.numbers import Integer, Rational, Float
from sympy.core.relational import Rel
from sympy.core.symbol import symbols
from sympy.functions import sin
from sympy.integrals.integrals import Integral
from sympy.series.order import Order

from sympy.printing.precedence import precedence, PRECEDENCE

x, y = symbols("x,y")

def test_Add():
    assert precedence(x+y) == PRECEDENCE["Add"]
    assert precedence(x*y+1) == PRECEDENCE["Add"]

def test_Function():
    assert precedence(sin(x)) == PRECEDENCE["Atom"]
    assert precedence(Derivative(x,y)) == PRECEDENCE["Atom"]

def test_Integral():
    assert precedence(Integral(x,y)) == PRECEDENCE["Atom"]

def test_Mul():
    assert precedence(x*y) == PRECEDENCE["Mul"]
    assert precedence(-x*y) == PRECEDENCE["Add"]

def test_Number():
    assert precedence(Integer(0)) == PRECEDENCE["Atom"]
    assert precedence(Integer(1)) == PRECEDENCE["Atom"]
    assert precedence(Integer(-1)) == PRECEDENCE["Add"]
    assert precedence(Integer(10)) == PRECEDENCE["Atom"]
    assert precedence(Rational(5,2)) == PRECEDENCE["Mul"]
    assert precedence(Rational(-5,2)) == PRECEDENCE["Add"]
    assert precedence(Float(5)) == PRECEDENCE["Atom"]
    assert precedence(Float(-5)) == PRECEDENCE["Add"]

def test_Order():
    assert precedence(Order(x)) == PRECEDENCE["Atom"]

def test_Pow():
    assert precedence(x**y) == PRECEDENCE["Pow"]
    assert precedence(-x**y) == PRECEDENCE["Add"]
    assert precedence(x**-y) == PRECEDENCE["Pow"]

def test_Product():
    assert precedence(Product(x,(x,y,y+1))) == PRECEDENCE["Atom"]

def test_Relational():
    assert precedence(Rel(x+y,y,"<")) == PRECEDENCE["Relational"]

def test_Sum():
    assert precedence(Sum(x,(x,y,y+1))) == PRECEDENCE["Atom"]

def test_Symbol():
    assert precedence(x) == PRECEDENCE["Atom"]

def test_And_Or():
    # precendence relations between logical operators, ...
    assert precedence(x&y) > precedence(x|y)
    assert precedence(~y) > precedence(x&y)
    # ... and with other operators (cfr. other programming languages)
    assert precedence(x+y) > precedence(x|y)
    assert precedence(x+y) > precedence(x&y)
    assert precedence(x*y) > precedence(x|y)
    assert precedence(x*y) > precedence(x&y)
    assert precedence(~y) > precedence(x*y)
    assert precedence(~y) > precedence(x-y)
    # double checks
    assert precedence(x&y) == PRECEDENCE["And"]
    assert precedence(x|y) == PRECEDENCE["Or"]
    assert precedence(~y) == PRECEDENCE["Not"]


text:
[![Coverage](http://gocover.io/_badge/github.com/go-fsnotify/fsnotify)](http://gocover.io/github.com/go-fsnotify/fsnotify) [![GoDoc](https://godoc.org/gopkg.in/fsnotify.v1?status.svg)](https://godoc.org/gopkg.in/fsnotify.v1)

Cross platform: Windows, Linux, BSD and OS X.

|Adapter   |OS        |Status    |
|----------|----------|----------|
|inotify   |Linux, Android\*|Supported|
|kqueue    |BSD, OS X, iOS\*|Supported|
|ReadDirectoryChangesW|Windows|Supported|
|FSEvents  |OS X          |[Planned](https://github.com/go-fsnotify/fsnotify/issues/11)|
|FEN       |Solaris 11    |[Planned](https://github.com/go-fsnotify/fsnotify/issues/12)|
|fanotify  |Linux 2.6.37+ | |
|Polling   |*All*         |[Maybe](https://github.com/go-fsnotify/fsnotify/issues/9)|
|          |Plan 9        | |

\* Android and iOS are untested.

Please see [the documentation](https://godoc.org/gopkg.in/fsnotify.v1) for usage. Consult the [Wiki](https://github.com/go-fsnotify/fsnotify/wiki) for the FAQ and further information.

## API stability

Two major versions of fsnotify exist.

**[fsnotify.v1](https://gopkg.in/fsnotify.v1)** provides [a new API](https://godoc.org/gopkg.in/fsnotify.v1) based on [this design document](http://goo.gl/MrYxyA). You can import v1 with:

```go
import "gopkg.in/fsnotify.v1"
```

\* Refer to the package as fsnotify (without the .v1 suffix).

**[fsnotify.v0](https://gopkg.in/fsnotify.v0)** is API-compatible with [howeyc/fsnotify](https://godoc.org/github.com/howeyc/fsnotify). Bugfixes *may* be backported, but I recommend upgrading to v1.

```go
import "gopkg.in/fsnotify.v0"
```

Further API changes are [planned](https://github.com/go-fsnotify/fsnotify/milestones), but a new major revision will be tagged, so you can depend on the v1 API.

## Contributing

* Send questions to [golang-dev@googlegroups.com](mailto:golang-dev@googlegroups.com).
* Request features and report bugs using the [GitHub Issue Tracker](https://github.com/go-fsnotify/fsnotify/issues).

A future version of Go will have [fsnotify in the standard library](https://code.google.com/p/go/issues/detail?id=4068), therefore fsnotify carries the same [LICENSE](https://github.com/go-fsnotify/fsnotify/blob/master/LICENSE) as Go. Contributors retain their copyright, so we need you to fill out a short form before we can accept your contribution: [Google Individual Contributor License Agreement](https://developers.google.com/open-source/cla/individual).

Please read [CONTRIBUTING](https://github.com/go-fsnotify/fsnotify/blob/master/CONTRIBUTING.md) before opening a pull request.

## Example

See [example_test.go](https://github.com/go-fsnotify/fsnotify/blob/master/example_test.go).


text:
package io.requery.proxy;

import io.requery.meta.Attribute;
import io.requery.query.Expression;
import io.requery.query.MutableTuple;

import java.util.Map;

/**
 * Represents a composite key of multiple values.
 *
 * @param <T> entity type
 *
 * @author Nikhil Purushe
 */
public class CompositeKey<T> extends MutableTuple {

    /**
     * Creates a new composite key instance.
     *
     * @param values a map of key {@link Attribute} to their corresponding values.
     */
    public CompositeKey(Map<? extends Attribute<T, ?>, ?> values) {
        super(values.size());
        if (values.isEmpty()) {
            throw new IllegalArgumentException();
        }
        int index = 0;
        for (Map.Entry<? extends Attribute<T, ?>, ?> entry : values.entrySet()) {
            set(index++, (Expression<?>) entry.getKey(), entry.getValue());
        }
    }
}


text:
require 'rubygems'
require 'highline'
require 'pivotal-tracker'
require 'yaml'

module Limer
  class Gitpt
    attr_reader :token, :highline, :initials, :name, :memberships, :applicable_stories, :setting_file

    def initialize
      @highline = HighLine.new
      @setting_file = File.join(ENV['HOME'], '.gitpt')

      load_setting
      hello unless settings_valid?
      request_settings while not settings_valid?
      stored if not settings_valid?
      PivotalTracker::Client.use_ssl = true
      PivotalTracker::Client.token = token
    end

    def highlight string
      bold HighLine::BLUE + string
    end

    def missing string
      bold HighLine::RED + string
    end

    def bold string
      HighLine::BOLD + string + HighLine::RESET
    end

    def hello
      @highline.say HighLine::RESET
      @highline.say "Welcome to #{bold 'Limer'}.\n\n"
    end

    def settings_valid?
      token and token.size > 0
    end

    def stored
      @highline.say "
        Thank you. Your settings have been stored at #{highlight @settings_file}
        You may remove that file for the wizard to reappear.

        ----------------------------------------------------"
    end

    def load_setting
      if File.exists? setting_file
        settings = YAML.load(File.read setting_file)
        @initials = settings[:initials]
        @token = settings[:token]
      else
      end
    end

    def load_project
      project_id_file = '.pt_project_id'
      if File.exists? project_id_file
        project_ids = File.read(project_id_file).split(/[\s]+/).map(&:to_i)
      end
      unless project_ids and project_ids.size > 0
        @highline.say "No project config provided. Add project id(s) to .pt_project_id file"
        exit 1
      end
      loading 'Connecting to Pivotal Tracker: ... ' do
        projects = project_ids.collect do |project_id|
          PivotalTracker::Project.find(project_id)
        end

        @memberships = projects.collect(&:memberships).map(&:all).flatten

        @applicable_stories = projects.collect do |project|
          project.stories.all(:state => 'started,finished,rejected')
        end.flatten
      end
    end

    def loading(message, &block)
      print message
      STDOUT.flush
      yield
      print "\r" + ' ' * message.size + "\r" # Remove loading message
      STDOUT.flush
    end

    def request_settings
      @highline.say highlight('Your settings are missing or invalid.')
      @highline.say "Please configure your Pivotal Tracker access.\n\n"
      token = @highline.ask bold("Your API key:") + " "
      initials = @highline.ask bold("Your PT initials") + " (optional, used for highlighting your stories): "
      @highline.say "\n"

      settings = { :token => token, :initials => initials }

      File.open setting_file, 'w' do |file|
        file.write settings.to_yaml
      end

      load_setting
    end

    def choose_story
      selected_story = nil

      @highline.choose do |menu|
        menu.header = "Choose a story"
        applicable_stories.each do |story|
          owner_name = story.owned_by
          owner = if owner_name
            owners = memberships.select{|member| member.name == owner_name}
            owners.first ? owners.first.initials : '?'
          else
            '?'
          end

          state = story.current_state
          if state == 'started'
            state = HighLine::GREEN + state + HighLine::RESET
          elsif state != 'finished'
            state = HighLine::RED + state + HighLine::RESET
          end
          state += HighLine::BOLD if owner == initials

          label = "(#{owner}, #{state}) #{story.name}"
          label = bold(label) if owner == initials
          menu.choice(label) { selected_story = story }
        end
        menu.hidden ''
      end

      if selected_story
        message = @highline.ask("\nAdd an optional message")
        @highline.say message

        commit_message = "[##{selected_story.id}] #{selected_story.name}"
        if message.strip != ''
          commit_message << ' - '<< message.strip
        end

        exec('git', 'commit', '-m', commit_message)
      end
    end
    def run
      load_project
      choose_story
    end
  end
end


text:
using System.Collections.Generic;
using LamarCodeGeneration;
using LamarCodeGeneration.Frames;
using LamarCodeGeneration.Model;

namespace Jasper.Persistence.Sagas;

internal class SagaStoreOrDeleteFrame : Frame
{
    private readonly Variable _saga;
    private readonly Frame _update;
    private readonly Frame _delete;

    public SagaStoreOrDeleteFrame(Variable saga, Frame update, Frame delete) : base(update.IsAsync || delete.IsAsync)
    {
        uses.Add(saga);
        _saga = saga;
        _update = update;
        _delete = delete;
    }

    public override IEnumerable<Variable> FindVariables(IMethodVariables chain)
    {
        foreach (var variable in _update.FindVariables(chain))
        {
            yield return variable;
        }

        foreach (var variable in _delete.FindVariables(chain))
        {
            yield return variable;
        }
    }

    public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
    {
        writer.Write($"BLOCK:if ({_saga.Usage}.{nameof(Saga.IsCompleted)}())");
        _delete.GenerateCode(method, writer);
        writer.FinishBlock();
        writer.Write("BLOCK:else");
        _update.GenerateCode(method, writer);
        writer.FinishBlock();

        Next?.GenerateCode(method, writer);
    }
}


text:
<?php

namespace phpDocumentor\Application\Commands;

/**
 * @coversDefaultClass phpDocumentor\Application\Commands\LoadProjectFromCache
 */
class LoadProjectFromCacheTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @covers ::__construct
     * @covers ::getSource
     */
    public function testCanRegisterAbsoluteTargetLocation()
    {
        $fixture = new LoadProjectFromCache(__DIR__);

        $this->assertSame(__DIR__, $fixture->getSource());
    }

    /**
     * @covers ::__construct
     * @expectedException \InvalidArgumentException
     */
    public function testThrowErrorIfLocationDoesNotExist()
    {
        new LoadProjectFromCache('/khfkjhdskjhfdks');
    }

    /**
     * @covers ::__construct
     * @expectedException \InvalidArgumentException
     */
    public function testThrowErrorIfLocationIsNotAFolder()
    {
        new LoadProjectFromCache(__FILE__);
    }
}


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.7"/>
<title>App Engine Python SDK: google.appengine._internal.antlr3.recognizers.TokenSource Class Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>

<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="common.css" rel="stylesheet" type="text/css"/>

App Engine Python SDK
    v1.6.9 rev.445

The Python runtime is available as an experimental Preview feature.

google

appengine

_internal

antlr3

recognizers

TokenSource

Public Member Functions |
List of all members

google.appengine._internal.antlr3.recognizers.TokenSource Class Reference

if self._state.failed: sys.stdout.write(" failed=%s" % self._state.failed)
 More...

Inheritance diagram for google.appengine._internal.antlr3.recognizers.TokenSource:

  <map id="google.appengine._internal.antlr3.recognizers.TokenSource_map" name="google.appengine._internal.antlr3.recognizers.TokenSource_map">
<area href="classgoogle_1_1appengine_1_1__internal_1_1antlr3_1_1recognizers_1_1_lexer.html" alt="google.appengine._internal.antlr3.recognizers.Lexer" shape="rect" coords="227,112,671,136"/>
<area href="classgoogle_1_1appengine_1_1api_1_1search_1_1_expression_lexer_1_1_expression_lexer.html" alt="google.appengine.api.search.ExpressionLexer.ExpressionLexer" shape="rect" coords="0,168,444,192"/>
<area href="classgoogle_1_1appengine_1_1api_1_1search_1_1_query_lexer_1_1_query_lexer.html" alt="google.appengine.api.search.QueryLexer.QueryLexer" shape="rect" coords="454,168,898,192"/>
<area href="classgoogle_1_1appengine_1_1api_1_1search_1_1expression__parser_1_1_expression_lexer_with_errors.html" alt="google.appengine.api.search.expression_parser.ExpressionLexerWithErrors" shape="rect" coords="0,224,444,248"/>
<area href="classgoogle_1_1appengine_1_1api_1_1search_1_1query__parser_1_1_query_lexer_with_errors.html" alt="google.appengine.api.search.query_parser.QueryLexerWithErrors" shape="rect" coords="454,224,898,248"/>
</map>

Public Member Functions

def 	nextToken

def 	__iter__

def 	next

Detailed Description

if self._state.failed: sys.stdout.write(" failed=%s" % self._state.failed)

@brief Abstract baseclass for token producers.

A source of tokens must provide a sequence of tokens via nextToken()
and also must reveal it's source of characters; CommonToken's text is
computed from a CharStream; it only store indices into the char stream.

Errors from the lexer are never passed to the parser.  Either you want
to keep going or you do not upon token recognition error.  If you do not
want to continue lexing then you do not want to continue parsing.  Just
throw an exception not under RecognitionException and Java will naturally
toss you all the way out of the recognizers.  If you want to continue
lexing then you should not throw an exception to the parser--it has already
requested a token.  Keep lexing until you get a valid one.  Just report
errors and keep going, looking for a valid token.

Member Function Documentation

          def google.appengine._internal.antlr3.recognizers.TokenSource.__iter__
          (

          self	)

The TokenSource is an interator.

The iteration will not include the final EOF token, see also the note
for the next() method.

          def google.appengine._internal.antlr3.recognizers.TokenSource.next
          (

          self	)

Return next token or raise StopIteration.

Note that this will raise StopIteration when hitting the EOF token,
so EOF will not be part of the iteration.

          def google.appengine._internal.antlr3.recognizers.TokenSource.nextToken
          (

          self	)

Return a Token object from your input stream (usually a CharStream).

Do not fail/return upon lexing error; keep chewing on the characters
until you get a good one; errors are not passed through to the parser.

The documentation for this class was generated from the following file:

code/googleappengine-read-only/python/google/appengine/_internal/antlr3/recognizers.py

<address class="footer">
    Maintained by tzmartin
</address>


text:
#ifndef XSC_GLSL_KEYWORDS_H
#define XSC_GLSL_KEYWORDS_H

#include "Token.h"
#include "ASTEnums.h"
#include <string>
#include <memory>
#include <set>

namespace Xsc
{

/* ----- GLSL Keywords ----- */

// Returns the Keyword-to-Token map for GLSL.
const KeywordMapType& GLSLKeywords();

/* ----- DataType Mapping ----- */

// Returns the GLSL keyword for the specified data type or null on failure.
const std::string* DataTypeToGLSLKeyword(const DataType t);

// Returns the data type for the specified GLSL keyword or throws an std::runtime_error on failure.
DataType GLSLKeywordToDataType(const std::string& keyword);

/* ----- StorageClass Mapping ----- */

// Returns the GLSL keyword for the specified storage class or null on failure.
const std::string* StorageClassToGLSLKeyword(const StorageClass t);

// Returns the storage class for the specified GLSL keyword or throws an std::runtime_error on failure.
StorageClass GLSLKeywordToStorageClass(const std::string& keyword);

/* ----- InterpModifier Mapping ----- */

// Returns the GLSL keyword for the specified interpolation modifier or null on failure.
const std::string* InterpModifierToGLSLKeyword(const InterpModifier t);

// Returns the interpolation modifier for the specified GLSL keyword or throws an std::runtime_error on failure.
InterpModifier GLSLKeywordToInterpModifier(const std::string& keyword);

/* ----- BufferType Mapping ----- */

// Returns the GLSL keyword for the specified buffer type or null on failure.
const std::string* BufferTypeToGLSLKeyword(const BufferType t, bool useVulkanGLSL = false, bool separateSamplers = true);

/* ----- SamplerType Mapping ----- */

// Returns the GLSL keyword for the specified sampler type or null on failure.
const std::string* SamplerTypeToGLSLKeyword(const SamplerType t);

// Returns the sampler type for the specified GLSL keyword or throws an std::runtime_error on failure.
SamplerType GLSLKeywordToSamplerType(const std::string& keyword);

/* ----- AttributeType Mapping ----- */

// Returns the GLSL keyword for the specified attribut type or null on failure.
const std::string* AttributeTypeToGLSLKeyword(const AttributeType t);

// Returns the attribute type for the specified GLSL keyword or returns AttributeValue::Undefined.
AttributeType GLSLKeywordToAttributeType(const std::string& keyword);

/* ----- AttributeValue Mapping ----- */

// Returns the GLSL keyword for the specified attribut value or null on failure.
const std::string* AttributeValueToGLSLKeyword(const AttributeValue t);

// Returns the attribute value for the specified GLSL keyword or returns AttributeValue::Undefined.
AttributeValue GLSLKeywordToAttributeValue(const std::string& keyword);

/* ----- PrimitiveType Mapping ----- */

// Returns the GLSL keyword for the specified geometry primtive type or null on failure.
const std::string* PrimitiveTypeToGLSLKeyword(const PrimitiveType t);

// Returns the primitive type for the specified GLSL keyword or throws an std::runtime_error on failure.
PrimitiveType GLSLKeywordToPrimitiveType(const std::string& keyword);

/* ----- ImageLayoutFormat Mapping ----- */

// Returns the GLSL keyword for the specified image layout format or null on failure.
const std::string* ImageLayoutFormatToGLSLKeyword(const ImageLayoutFormat t);

/* ----- Semantic Mapping ----- */

// Returns the GLSL keyword for the specified semantic (special cases if 'useVulkanGLSL' is true).
// see https://www.khronos.org/registry/vulkan/specs/misc/GL_KHR_vulkan_glsl.txt
std::unique_ptr<std::string> SemanticToGLSLKeyword(const IndexedSemantic& semantic, bool useVulkanGLSL = false);

// Returns the GLSL data type for specified semantic.
DataType SemanticToGLSLDataType(const Semantic t);

/* ----- Reserved GLSL Keywords ----- */

// Returns the set of all reserved GLSL keywords (functions, intrinsics, types etc.).
const std::set<std::string>& ReservedGLSLKeywords();

} // /namespace Xsc

#endif

// ================================================================================


text:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.sakaiproject.component.app.messageforums.dao.hibernate">

<class
      name="org.sakaiproject.component.app.messageforums.dao.hibernate.AttachmentImpl"
      table="MFR_ATTACHMENT_T"
      optimistic-lock="version">

    <!-- Inherited fields from MutableEntity -->
    <id name="id">
      <column name="ID" not-null="true" length="19" />
      <generator class="native">
        <param name="sequence">MFR_ATTACHMENT_S</param>
      </generator>
    </id>

	<!--  Version number used for optimistic locking -->
    <version name="version" column="VERSION" />

    <property name="uuid" column="UUID" length="36" not-null="true" />
    <property name="created" column="CREATED" not-null="true" />
    <property name="createdBy" column="CREATED_BY" length="255" not-null="true" />
    <property name="modified" column="MODIFIED" not-null="true" />
    <property name="modifiedBy" column="MODIFIED_BY" length="255" not-null="true" />

    <!-- Attachment fields -->
    <property name="attachmentId">
      <column name="ATTACHMENT_ID" length="255" not-null="true" />
    </property>
    <property name="attachmentUrl">
      <column name="ATTACHMENT_URL" length="255" not-null="true" />
    </property>
    <property name="attachmentName">
      <column name="ATTACHMENT_NAME" length="255" not-null="true" />
    </property>
    <property name="attachmentSize">
      <column name="ATTACHMENT_SIZE" length="255" not-null="true" />
    </property>
    <property name="attachmentType">
      <column name="ATTACHMENT_TYPE" length="255" not-null="true" />
    </property>

    <many-to-one name="message" column="m_surrogateKey" lazy="proxy"
      class="org.sakaiproject.component.app.messageforums.dao.hibernate.MessageImpl"
      not-null="false" index="MFR_ATTACHMENT_PARENT_I" cascade="none" />
    <!-- <property name="mesindex" column="mes_index_col" type="int" update="true" insert="true"/> -->

    <many-to-one name="openForum" column="of_surrogateKey" lazy="proxy"
      class="org.sakaiproject.component.app.messageforums.dao.hibernate.OpenForumImpl"
      not-null="false" index="MFR_ATTACHMENT_PARENT_I2" cascade="none" />
    <!-- <property name="ofindex" column="of_index_col" type="int" update="true" insert="true"/> -->

    <many-to-one name="privateForum" column="pf_surrogateKey" lazy="proxy"
      class="org.sakaiproject.component.app.messageforums.dao.hibernate.PrivateForumImpl"
      not-null="false" index="MFR_ATTACHMENT_PARENT_I3" cascade="none" />
    <!-- <property name="pfindex" column="pf_index_col" type="int" update="true" insert="true"/> -->

    <many-to-one name="topic" column="t_surrogateKey"  lazy="proxy"
      class="org.sakaiproject.component.app.messageforums.dao.hibernate.TopicImpl"
      not-null="false" index="MFR_ATTACHMENT_PARENT_I4" cascade="none" />
    <!-- <property name="tindex" column="t_index_col" type="int" update="true" insert="true"/> -->

  </class>

  <query name="findAttachmentById">
    <![CDATA[from org.sakaiproject.component.app.messageforums.dao.hibernate.AttachmentImpl as a where a.id = :id]]>
  </query>

  <query name="findAttachmentByUuid">
    <![CDATA[from org.sakaiproject.component.app.messageforums.dao.hibernate.AttachmentImpl as a where a.uuid = :uuid]]>
  </query>

  <query name="findTopicAttachments">
  	<![CDATA[from org.sakaiproject.component.app.messageforums.dao.hibernate.AttachmentImpl as a where a.topic = :topic]]>
  </query>

</hibernate-mapping>


text:
extern __attribute__((visibility("default"))) uptr __asan_mapping_scale;
extern __attribute__((visibility("default"))) uptr __asan_mapping_offset;
# define SHADOW_SCALE (__asan_mapping_scale)
# define SHADOW_OFFSET (__asan_mapping_offset)
#else
# if ASAN_ANDROID
#  define SHADOW_SCALE (3)
#  define SHADOW_OFFSET (0)
# else
#  define SHADOW_SCALE (3)
#  if __WORDSIZE == 32
#   define SHADOW_OFFSET (1 << 29)
#  else
#   define SHADOW_OFFSET (1ULL << 44)
#  endif
# endif
#endif  // ASAN_FLEXIBLE_MAPPING_AND_OFFSET

#define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
#define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) | (SHADOW_OFFSET))
#define SHADOW_TO_MEM(shadow) (((shadow) - SHADOW_OFFSET) << SHADOW_SCALE)

#if __WORDSIZE == 64
  static const uptr kHighMemEnd = 0x00007fffffffffffUL;
#else  // __WORDSIZE == 32
  static const uptr kHighMemEnd = 0xffffffff;
#endif  // __WORDSIZE

#define kLowMemBeg      0
#define kLowMemEnd      (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)

#define kLowShadowBeg   SHADOW_OFFSET
#define kLowShadowEnd   MEM_TO_SHADOW(kLowMemEnd)

#define kHighMemBeg     (MEM_TO_SHADOW(kHighMemEnd) + 1)

#define kHighShadowBeg  MEM_TO_SHADOW(kHighMemBeg)
#define kHighShadowEnd  MEM_TO_SHADOW(kHighMemEnd)

#define kShadowGapBeg   (kLowShadowEnd ? kLowShadowEnd + 1 : 16 * kPageSize)
#define kShadowGapEnd   (kHighShadowBeg - 1)

#define kGlobalAndStackRedzone \
      (SHADOW_GRANULARITY < 32 ? 32 : SHADOW_GRANULARITY)

namespace __asan {

static inline bool AddrIsInLowMem(uptr a) {
  return a < kLowMemEnd;
}

static inline bool AddrIsInLowShadow(uptr a) {
  return a >= kLowShadowBeg && a <= kLowShadowEnd;
}

static inline bool AddrIsInHighMem(uptr a) {
  return a >= kHighMemBeg && a <= kHighMemEnd;
}

static inline bool AddrIsInMem(uptr a) {
  return AddrIsInLowMem(a) || AddrIsInHighMem(a);
}

static inline uptr MemToShadow(uptr p) {
  CHECK(AddrIsInMem(p));
  return MEM_TO_SHADOW(p);
}

static inline bool AddrIsInHighShadow(uptr a) {
  return a >= kHighShadowBeg && a <=  kHighMemEnd;
}

static inline bool AddrIsInShadow(uptr a) {
  return AddrIsInLowShadow(a) || AddrIsInHighShadow(a);
}

static inline bool AddrIsInShadowGap(uptr a) {
  return a >= kShadowGapBeg && a <= kShadowGapEnd;
}

static inline bool AddrIsAlignedByGranularity(uptr a) {
  return (a & (SHADOW_GRANULARITY - 1)) == 0;
}

static inline bool AddressIsPoisoned(uptr a) {
  const uptr kAccessSize = 1;
  u8 *shadow_address = (u8*)MemToShadow(a);
  s8 shadow_value = *shadow_address;
  if (shadow_value) {
    u8 last_accessed_byte = (a & (SHADOW_GRANULARITY - 1))
                                 + kAccessSize - 1;
    return (last_accessed_byte >= shadow_value);
  }
  return false;
}

}  // namespace __asan

#endif  // ASAN_MAPPING_H