text:
""" A module to contain utility ISO-19115 metadata parsing helpers """

from _collections import OrderedDict
from copy import deepcopy
from frozendict import frozendict as FrozenOrderedDict

from parserutils.collections import filter_empty, reduce_value, wrap_value
from parserutils.elements import get_element_name, get_element_text, get_elements_text
from parserutils.elements import get_elements, get_remote_element, insert_element, remove_element
from parserutils.elements import XPATH_DELIM

from gis_metadata.exceptions import InvalidContent
from gis_metadata.metadata_parser import MetadataParser
from gis_metadata.utils import DATE_TYPE, DATE_TYPE_SINGLE, DATE_TYPE_MULTIPLE
from gis_metadata.utils import DATE_TYPE_RANGE, DATE_TYPE_RANGE_BEGIN, DATE_TYPE_RANGE_END
from gis_metadata.utils import ATTRIBUTES
from gis_metadata.utils import CONTACTS
from gis_metadata.utils import BOUNDING_BOX
from gis_metadata.utils import DATES
from gis_metadata.utils import DIGITAL_FORMS
from gis_metadata.utils import KEYWORDS_PLACE, KEYWORDS_STRATUM, KEYWORDS_TEMPORAL, KEYWORDS_THEME
from gis_metadata.utils import LARGER_WORKS
from gis_metadata.utils import PROCESS_STEPS
from gis_metadata.utils import RASTER_DIMS, RASTER_INFO
from gis_metadata.utils import COMPLEX_DEFINITIONS, ParserProperty
from gis_metadata.utils import format_xpaths, get_default_for_complex, get_default_for_complex_sub
from gis_metadata.utils import parse_complex_list, parse_property, update_complex_list, update_property

ISO_ROOTS = ('MD_Metadata', 'MI_Metadata')

KEYWORD_PROPS = (KEYWORDS_PLACE, KEYWORDS_STRATUM, KEYWORDS_TEMPORAL, KEYWORDS_THEME)
KEYWORD_TYPES = FrozenOrderedDict({
    KEYWORDS_PLACE: 'place',
    KEYWORDS_STRATUM: 'stratum',
    KEYWORDS_TEMPORAL: 'temporal',
    KEYWORDS_THEME: 'theme'
})

# For appending digital form content to ISO distribution format specs
ISO_DIGITAL_FORMS_DELIM = '@------------------------------@'

# Define backup locations for attribute sub-properties and dimension type property
ISO_DEFINITIONS = dict({k: dict(v) for k, v in dict(COMPLEX_DEFINITIONS).items()})
ISO_DEFINITIONS[ATTRIBUTES].update({
    '_definition_source': '{_definition_src}',
    '__definition_source': '{__definition_src}',
    '___definition_source': '{___definition_src}'
})
ISO_DEFINITIONS[RASTER_DIMS]['_type'] = '{_type}'
ISO_DEFINITIONS = FrozenOrderedDict({k: FrozenOrderedDict(v) for k, v in ISO_DEFINITIONS.items()})

ISO_TAG_ROOTS = OrderedDict((
    # First process private dependency tags (order enforced by key sorting)
    ('_content_coverage', 'contentInfo/MD_CoverageDescription'),
    ('_dataqual', 'dataQualityInfo/DQ_DataQuality'),
    ('_dataqual_lineage', '{_dataqual}/lineage/LI_Lineage'),
    ('_dataqual_report', '{_dataqual}/report'),
    ('_distinfo', 'distributionInfo/MD_Distribution'),
    ('_distinfo_dist', '{_distinfo}/distributor/MD_Distributor'),
    ('_distinfo_proc', '{_distinfo_dist}/distributionOrderProcess/MD_StandardOrderProcess'),
    ('_distinfo_resp', '{_distinfo_dist}/distributorContact/CI_ResponsibleParty'),
    ('_distinfo_resp_contact', '{_distinfo_resp}/contactInfo/CI_Contact'),
    ('_distinfo_rsrc', '{_distinfo}/transferOptions/MD_DigitalTransferOptions/onLine/CI_OnlineResource'),
    ('_idinfo', 'identificationInfo/MD_DataIdentification'),
    ('_idinfo_aggregate', '{_idinfo}/aggregationInfo/MD_AggregateInformation'),
    ('_idinfo_aggregate_citation', '{_idinfo_aggregate}/aggregateDataSetName/CI_Citation'),
    ('_idinfo_aggregate_contact', '{_idinfo_aggregate_citation}/citedResponsibleParty/CI_ResponsibleParty'),
    ('_idinfo_citation', '{_idinfo}/citation/CI_Citation'),
    ('_idinfo_citresp', '{_idinfo_citation}/citedResponsibleParty/CI_ResponsibleParty'),
    ('_idinfo_extent', '{_idinfo}/extent/EX_Extent'),
    ('_idinfo_keywords', '{_idinfo}/descriptiveKeywords/MD_Keywords'),
    ('_idinfo_resp', '{_idinfo}/pointOfContact/CI_ResponsibleParty'),
    ('_idinfo_resp_contact', '{_idinfo_resp}/contactInfo/CI_Contact'),

    ('_srinfo_grid_rep', 'spatialRepresentationInfo/MD_GridSpatialRepresentation'),
    ('_srinfo_grid_dim', '{_srinfo_grid_rep}/axisDimensionProperties/MD_Dimension'),

    # Supported in separate file ISO-19110: FC_FeatureCatalog
    ('_attr_root', 'FC_FeatureCatalogue'),
    ('_attr_base', 'featureType/FC_FeatureType/carrierOfCharacteristics/FC_FeatureAttribute'),
    ('_attr_def', '{_attr_base}/definitionReference/FC_DefinitionReference/definitionSource/FC_DefinitionSource'),
    ('_attr_src', '{_attr_def}/source/CI_Citation/citedResponsibleParty/CI_ResponsibleParty'),

    # References to separate file ISO-19110 from: MD_Metadata
    ('_attr_citation', 'contentInfo/MD_FeatureCatalogueDescription/featureCatalogueCitation'),
    ('_attr_contact', '{_attr_citation}/CI_Citation/citedResponsibleParty/CI_ResponsibleParty/contactInfo/CI_Contact'),
    ('_attr_contact_url', '{_attr_contact}/onlineResource/CI_OnlineResource/linkage/URL')
))

# Two passes required because of self references within roots dict
ISO_TAG_ROOTS.update(format_xpaths(ISO_TAG_ROOTS, **ISO_TAG_ROOTS))
ISO_TAG_ROOTS.update(format_xpaths(ISO_TAG_ROOTS, **ISO_TAG_ROOTS))
ISO_TAG_ROOTS = FrozenOrderedDict(ISO_TAG_ROOTS)

ISO_TAG_FORMATS = {
    # Property-specific xpath roots: the base from which each element repeats
    '_attribute_accuracy_root': '{_dataqual_report}',
    '_attributes_root': 'featureType/FC_FeatureType/carrierOfCharacteristics',
    '_bounding_box_root': '{_idinfo_extent}/geographicElement',
    '_contacts_root': '{_idinfo}/pointOfContact',
    '_dataset_completeness_root': '{_dataqual_report}',
    '_dates_root': '{_idinfo_extent}/temporalElement',
    '_digital_forms_root': '{_distinfo}/distributionFormat',
    '_dist_liability_root': '{_idinfo}/resourceConstraints',
    '_transfer_options_root': '{_distinfo}/transferOptions/MD_DigitalTransferOptions/onLine',
    '_keywords_root': '{_idinfo}/descriptiveKeywords',
    '_larger_works_root': '{_idinfo_aggregate_citation}',
    '_process_steps_root': '{_dataqual_lineage}/processStep',
    '_raster_info_root': '{_srinfo_grid_rep}/axisDimensionProperties',
    '_use_constraints_root': '{_idinfo}/resourceConstraints',

    # Then process public dependent tags
    'title': '{_idinfo_citation}/title/CharacterString',
    'abstract': '{_idinfo}/abstract/CharacterString',
    'purpose': '{_idinfo}/purpose/CharacterString',
    'supplementary_info': '{_idinfo}/supplementalInformation/CharacterString',
    'online_linkages': '{_idinfo_citresp}/contactInfo/CI_Contact/onlineResource/CI_OnlineResource/linkage/URL',
    'originators': '{_idinfo_citresp}/organisationName/CharacterString',
    'publish_date': '{_idinfo_citation}/date/CI_Date/date/Date',
    'publish_date_type': '{_idinfo_citation}/date/CI_Date/dateType/CI_DateTypeCode',
    'data_credits': '{_idinfo}/credit/CharacterString',
    CONTACTS: '{_idinfo_resp}/{{ct_path}}',
    'dist_contact_org': '{_distinfo_resp}/organisationName/CharacterString',
    'dist_contact_person': '{_distinfo_resp}/individualName/CharacterString',
    'dist_address_type': '{_distinfo_resp_contact}/address/@type',
    'dist_address': '{_distinfo_resp_contact}/address/CI_Address/deliveryPoint/CharacterString',
    'dist_city': '{_distinfo_resp_contact}/address/CI_Address/city/CharacterString',
    'dist_state': '{_distinfo_resp_contact}/address/CI_Address/administrativeArea/CharacterString',
    'dist_postal': '{_distinfo_resp_contact}/address/CI_Address/postalCode/CharacterString',
    'dist_country': '{_distinfo_resp_contact}/address/CI_Address/country/CharacterString',
    '_dist_country': '{_distinfo_resp_contact}/address/CI_Address/country/Country',  # If not in CharacterString
    'dist_phone': '{_distinfo_resp_contact}/phone/CI_Telephone/voice/CharacterString',
    'dist_email': '{_distinfo_resp_contact}/address/CI_Address/electronicMailAddress/CharacterString',
    'dist_liability': '{_idinfo}/resourceConstraints/MD_LegalConstraints/otherConstraints/CharacterString',
    'processing_fees': '{_distinfo_proc}/fees/CharacterString',
    'processing_instrs': '{_distinfo_proc}/orderingInstructions/CharacterString',
    'resource_desc': '{_idinfo}/resourceSpecificUsage/MD_Usage/specificUsage/CharacterString',
    'tech_prerequisites': '{_idinfo}/environmentDescription/CharacterString',
    ATTRIBUTES: '{_attr_base}/{{ad_path}}',
    '_attributes_file': '{_attr_citation}/@href',
    '__attributes_file': '{_attr_contact_url}',  # If not in above: "_attr_citation/@href"
    'attribute_accuracy': '{_dataqual_report}/DQ_QuantitativeAttributeAccuracy/measureDescription/CharacterString',
    BOUNDING_BOX: '{_idinfo_extent}/geographicElement/EX_GeographicBoundingBox/{{bbox_path}}',
    'dataset_completeness': '{_dataqual_report}/DQ_CompletenessOmission/measureDescription/CharacterString',
    DIGITAL_FORMS: '{_distinfo}/distributionFormat/MD_Format/{{df_path}}',
    '_access_desc': '{_distinfo_rsrc}/description/CharacterString',
    '_access_instrs': '{_distinfo_rsrc}/protocol/CharacterString',
    '_network_resource': '{_distinfo_rsrc}/linkage/URL',
    PROCESS_STEPS: '{_dataqual_lineage}/processStep/LI_ProcessStep/{{ps_path}}',
    LARGER_WORKS: '{_idinfo_aggregate_citation}/{{lw_path}}',
    '_lw_citation': '{_idinfo_aggregate_contact}/{{lw_path}}',
    '_lw_collective': '{_idinfo_aggregate_citation}/collectiveTitle/CharacterString',
    '_lw_contact': '{_idinfo_aggregate_contact}/contactInfo/CI_Contact/{{lw_path}}',
    '_lw_linkage': '{_idinfo_aggregate_contact}/contactInfo/CI_Contact/onlineResource/CI_OnlineResource/{{lw_path}}',
    RASTER_INFO: '{_srinfo_grid_dim}/{{ri_path}}',
    '_ri_num_dims': '{_srinfo_grid_rep}/numberOfDimensions/Integer',
    'other_citation_info': '{_idinfo_citation}/otherCitationDetails/CharacterString',
    'use_constraints': '{_idinfo}/resourceConstraints/MD_Constraints/useLimitation/CharacterString',
    DATES: '{_idinfo_extent}/temporalElement/EX_TemporalExtent/extent/{{type_path}}',
    KEYWORDS_PLACE: '{_idinfo_keywords}/keyword/CharacterString',
    KEYWORDS_STRATUM: '{_idinfo_keywords}/keyword/CharacterString',
    KEYWORDS_TEMPORAL: '{_idinfo_keywords}/keyword/CharacterString',
    KEYWORDS_THEME: '{_idinfo_keywords}/keyword/CharacterString'
}

# Apply XPATH root formats to the basic data map formats
ISO_TAG_FORMATS.update(ISO_TAG_ROOTS)
ISO_TAG_FORMATS.update(format_xpaths(ISO_TAG_FORMATS, **ISO_TAG_ROOTS))
ISO_TAG_FORMATS = FrozenOrderedDict(ISO_TAG_FORMATS)

ISO_TAG_PRIMITIVES = frozenset({
    'Binary', 'Boolean', 'CharacterString',
    'Date', 'DateTime', 'timePosition',
    'Decimal', 'Integer', 'Real', 'RecordType',
    'CI_DateTypeCode', 'MD_KeywordTypeCode', 'URL'
})

class IsoParser(MetadataParser):
    """ A class to parse metadata files conforming to the ISO-19115 standard """

    def _init_data_map(self):
        """ OVERRIDDEN: Initialize required ISO-19115 data map with XPATHS and specialized functions """

        if self._data_map is not None:
            return  # Initiation happens once

        # Parse and validate the ISO metadata root

        if self._xml_tree is None:
            iso_root = ISO_ROOTS[0]
        else:
            iso_root = get_element_name(self._xml_tree)

        if iso_root not in ISO_ROOTS:
            raise InvalidContent('Invalid XML root for ISO-19115 standard: {root}', root=iso_root)

        iso_data_map = {'_root': iso_root}
        iso_data_map.update(ISO_TAG_ROOTS)
        iso_data_map.update(ISO_TAG_FORMATS)

        iso_data_structures = {}

        # Capture and format complex XPATHs

        ad_format = iso_data_map[ATTRIBUTES]
        ft_source = iso_data_map['_attr_src'].replace('/carrierOfCharacteristics/FC_FeatureAttribute', '')

        iso_data_structures[ATTRIBUTES] = format_xpaths(
            ISO_DEFINITIONS[ATTRIBUTES],

            label=ad_format.format(ad_path='memberName/LocalName'),
            aliases=ad_format.format(ad_path='aliases/LocalName'),  # Not in spec
            definition=ad_format.format(ad_path='definition/CharacterString'),

            # First try to populate attribute definition source from FC_FeatureAttribute
            definition_src=iso_data_map['_attr_src'] + '/organisationName/CharacterString',
            _definition_src=iso_data_map['_attr_src'] + '/individualName/CharacterString',

            # Then assume feature type source is the same as attribute: populate from FC_FeatureType
            __definition_src=ft_source + '/organisationName/CharacterString',
            ___definition_src=ft_source + '/individualName/CharacterString'
        )

        bb_format = iso_data_map[BOUNDING_BOX]
        iso_data_structures[BOUNDING_BOX] = format_xpaths(
            ISO_DEFINITIONS[BOUNDING_BOX],
            east=bb_format.format(bbox_path='eastBoundLongitude/Decimal'),
            south=bb_format.format(bbox_path='southBoundLatitude/Decimal'),
            west=bb_format.format(bbox_path='westBoundLongitude/Decimal'),
            north=bb_format.format(bbox_path='northBoundLatitude/Decimal')
        )

        ct_format = iso_data_map[CONTACTS]
        iso_data_structures[CONTACTS] = format_xpaths(
            ISO_DEFINITIONS[CONTACTS],
            name=ct_format.format(ct_path='individualName/CharacterString'),
            organization=ct_format.format(ct_path='organisationName/CharacterString'),
            position=ct_format.format(ct_path='positionName/CharacterString'),
            email=ct_format.format(
                ct_path='contactInfo/CI_Contact/address/CI_Address/electronicMailAddress/CharacterString'
            )
        )

        dt_format = iso_data_map[DATES]
        iso_data_structures[DATES] = {
            DATE_TYPE_MULTIPLE: dt_format.format(type_path='TimeInstant/timePosition'),
            DATE_TYPE_RANGE_BEGIN: dt_format.format(type_path='TimePeriod/begin/TimeInstant/timePosition'),
            DATE_TYPE_RANGE_END: dt_format.format(type_path='TimePeriod/end/TimeInstant/timePosition'),
            DATE_TYPE_SINGLE: dt_format.format(type_path='TimeInstant/timePosition')  # Same as multiple
        }
        iso_data_structures[DATES][DATE_TYPE_RANGE] = [
            iso_data_structures[DATES][DATE_TYPE_RANGE_BEGIN],
            iso_data_structures[DATES][DATE_TYPE_RANGE_END]
        ]

        df_format = iso_data_map[DIGITAL_FORMS]
        iso_data_structures[DIGITAL_FORMS] = format_xpaths(
            ISO_DEFINITIONS[DIGITAL_FORMS],
            name=df_format.format(df_path='name/CharacterString'),
            content='',  # Not supported in ISO-19115 (appending to spec)
            decompression=df_format.format(df_path='fileDecompressionTechnique/CharacterString'),
            version=df_format.format(df_path='version/CharacterString'),
            specification=df_format.format(df_path='specification/CharacterString'),
            access_desc=iso_data_map['_access_desc'],
            access_instrs=iso_data_map['_access_instrs'],
            network_resource=iso_data_map['_network_resource']
        )

        keywords_structure = {
            'keyword_root': 'MD_Keywords/keyword',
            'keyword_type': 'MD_Keywords/type/MD_KeywordTypeCode',
            'keyword': 'MD_Keywords/keyword/CharacterString'
        }
        for keyword_prop in KEYWORD_PROPS:
            iso_data_structures[keyword_prop] = deepcopy(keywords_structure)

        lw_format = iso_data_map[LARGER_WORKS]
        iso_data_structures[LARGER_WORKS] = format_xpaths(
            ISO_DEFINITIONS[LARGER_WORKS],
            title=lw_format.format(lw_path='title/CharacterString'),
            edition=lw_format.format(lw_path='edition/CharacterString'),
            origin=iso_data_map['_lw_citation'].format(lw_path='individualName/CharacterString'),
            online_linkage=iso_data_map['_lw_linkage'].format(lw_path='linkage/URL'),
            other_citation=lw_format.format(lw_path='otherCitationDetails/CharacterString'),
            date=lw_format.format(lw_path='editionDate/Date'),
            place=iso_data_map['_lw_contact'].format(lw_path='address/CI_Address/city/CharacterString'),
            info=iso_data_map['_lw_citation'].format(lw_path='organisationName/CharacterString')
        )

        ps_format = iso_data_map[PROCESS_STEPS]
        iso_data_structures[PROCESS_STEPS] = format_xpaths(
            ISO_DEFINITIONS[PROCESS_STEPS],
            description=ps_format.format(ps_path='description/CharacterString'),
            date=ps_format.format(ps_path='dateTime/DateTime'),
            sources=ps_format.format(
                ps_path='source/LI_Source/sourceCitation/CI_Citation/alternateTitle/CharacterString'
            )
        )

        ri_format = iso_data_map[RASTER_INFO]
        iso_data_structures[RASTER_INFO] = format_xpaths(
            ISO_DEFINITIONS[RASTER_DIMS],
            type=ri_format.format(ri_path='dimensionName/MD_DimensionNameTypeCode'),
            _type=ri_format.format(ri_path='dimensionName/MD_DimensionNameTypeCode/@codeListValue'),
            size=ri_format.format(ri_path='dimensionSize/Integer'),
            value=ri_format.format(ri_path='resolution/Measure'),
            units=ri_format.format(ri_path='resolution/Measure/@uom')
        )

        # Assign XPATHS and gis_metadata.utils.ParserProperties to data map

        for prop, xpath in dict(iso_data_map).items():
            if prop == ATTRIBUTES:
                iso_data_map[prop] = ParserProperty(self._parse_attribute_details, self._update_attribute_details)

            elif prop in (CONTACTS, PROCESS_STEPS):
                iso_data_map[prop] = ParserProperty(self._parse_complex_list, self._update_complex_list)

            elif prop in (BOUNDING_BOX, LARGER_WORKS):
                iso_data_map[prop] = ParserProperty(self._parse_complex, self._update_complex)

            elif prop == DATES:
                iso_data_map[prop] = ParserProperty(self._parse_dates, self._update_dates)

            elif prop == DIGITAL_FORMS:
                iso_data_map[prop] = ParserProperty(self._parse_digital_forms, self._update_digital_forms)

            elif prop in KEYWORD_PROPS:
                iso_data_map[prop] = ParserProperty(self._parse_keywords, self._update_keywords)

            elif prop == RASTER_INFO:
                iso_data_map[prop] = ParserProperty(self._parse_raster_info, self._update_raster_info)

            else:
                iso_data_map[prop] = xpath

        self._data_map = iso_data_map
        self._data_structures = iso_data_structures

    def _parse_attribute_details(self, prop=ATTRIBUTES):
        """ Concatenates a list of Attribute Details data structures parsed from a remote file """

        parsed_attributes = self._parse_attribute_details_file(prop)
        if parsed_attributes is None:
            # If not in the (official) remote location, try the tree itself
            parsed_attributes = self._parse_complex_list(prop)

        for attribute in (a for a in parsed_attributes if not a['aliases']):
            # Aliases are not in ISO standard: default to label
            attribute['aliases'] = attribute['label']

        return get_default_for_complex(prop, parsed_attributes)

    def _parse_attribute_details_file(self, prop=ATTRIBUTES):
        """ Concatenates a list of Attribute Details data structures parsed from a remote file """

        # Parse content from remote file URL, which may be stored in one of two places:
        #    Starting at: contentInfo/MD_FeatureCatalogueDescription/featureCatalogueCitation
        #    ATTRIBUTE: href
        #    ELEMENT TEXT: CI_Citation/.../CI_Contact/onlineResource/CI_OnlineResource/linkage

        self._attr_details_file_url = parse_property(
            self._xml_tree, None, self._data_map, '_attributes_file'
        )
        if not self._attr_details_file_url:
            self._attr_details_file_url = None
            return None

        try:
            tree_to_parse = get_remote_element(self._attr_details_file_url)
        except Exception:
            self._attr_details_file_url = None
            return None

        xpath_map = self._data_structures[ATTRIBUTES]
        xpath_root = self._get_xroot_for(prop)

        return parse_complex_list(tree_to_parse, xpath_root, xpath_map, prop)

    def _parse_digital_forms(self, prop=DIGITAL_FORMS):
        """ Concatenates a list of Digital Form data structures parsed from the metadata """

        xpath_map = self._data_structures[prop]

        # Parse base digital form fields: 'name', 'content', 'decompression', 'version', 'specification'
        xpath_root = self._data_map['_digital_forms_root']
        digital_forms = parse_complex_list(self._xml_tree, xpath_root, xpath_map, prop)

        # Parse digital form transfer option fields: 'access_desc', 'access_instrs', 'network_resource'
        xpath_root = self._data_map['_transfer_options_root']
        transfer_opts = parse_complex_list(self._xml_tree, xpath_root, xpath_map, prop)

        # Split out digital form content that has been appended to specifications

        content_delim = ISO_DIGITAL_FORMS_DELIM

        for digital_form in digital_forms:
            specs = reduce_value(digital_form['specification'])
            specs = specs.splitlines() if isinstance(specs, str) else specs

            specifications = wrap_value(s.strip() for s in specs)

            digital_form['content'] = []
            digital_form['specification'] = []
            has_content = False

            # For each specification, insert delim before appending content
            for spec in specifications:
                has_content = has_content or spec == content_delim

                if not has_content:
                    digital_form['specification'].append(spec)
                elif spec != content_delim:
                    digital_form['content'].append(spec)

            # Reduce spec and content to single string values if possible
            for form_prop in ('content', 'specification'):
                digital_form[form_prop] = reduce_value(filter_empty(digital_form[form_prop], u''))

        # Combine digital forms and transfer options into a single complex struct

        df_len = len(digital_forms)
        to_len = len(transfer_opts)
        parsed_forms = []

        for idx in range(0, max(df_len, to_len)):
            digital_form = {}.fromkeys(ISO_DEFINITIONS[prop], u'')

            if idx < df_len:
                digital_form.update(i for i in digital_forms[idx].items() if i[1])
            if idx < to_len:
                digital_form.update(i for i in transfer_opts[idx].items() if i[1])

            if any(digital_form.values()):
                parsed_forms.append(digital_form)

        return get_default_for_complex(prop, parsed_forms)

    def _parse_keywords(self, prop):
        """ Parse type-specific keywords from the metadata: Theme or Place """

        keywords = []

        if prop in KEYWORD_PROPS:
            xpath_root = self._data_map['_keywords_root']
            xpath_map = self._data_structures[prop]

            xtype = xpath_map['keyword_type']
            xpath = xpath_map['keyword']
            ktype = KEYWORD_TYPES[prop]

            for element in get_elements(self._xml_tree, xpath_root):
                if get_element_text(element, xtype).lower() == ktype.lower():
                    keywords.extend(get_elements_text(element, xpath))

        return keywords

    def _parse_raster_info(self, prop=RASTER_INFO):
        """ Collapses multiple dimensions into a single raster_info complex struct """

        raster_info = {}.fromkeys(ISO_DEFINITIONS[prop], u'')

        # Ensure conversion of lists to newlines is in place
        raster_info['dimensions'] = get_default_for_complex_sub(
            prop=prop,
            subprop='dimensions',
            value=parse_property(self._xml_tree, None, self._data_map, '_ri_num_dims'),
            xpath=self._data_map['_ri_num_dims']
        )

        xpath_root = self._get_xroot_for(prop)
        xpath_map = self._data_structures[prop]

        for dimension in parse_complex_list(self._xml_tree, xpath_root, xpath_map, RASTER_DIMS):
            dimension_type = dimension['type'].lower()

            if dimension_type == 'vertical':
                raster_info['vertical_count'] = dimension['size']

            elif dimension_type == 'column':
                raster_info['column_count'] = dimension['size']
                raster_info['x_resolution'] = u' '.join(dimension[k] for k in ['value', 'units']).strip()

            elif dimension_type == 'row':
                raster_info['row_count'] = dimension['size']
                raster_info['y_resolution'] = u' '.join(dimension[k] for k in ['value', 'units']).strip()

        return raster_info if any(raster_info[k] for k in raster_info) else {}

    def _update_attribute_details(self, **update_props):
        """ Update operation for ISO Attribute Details metadata: write to "MD_Metadata/featureType" """

        tree_to_update = update_props['tree_to_update']
        xpath = self._data_map['_attr_citation']

        # Cannot write to remote file: remove the featureCatalogueCitation element

        self._attr_details_file_url = None
        remove_element(tree_to_update, xpath, True)

        return self._update_complex_list(**update_props)

    def _update_dates(self, **update_props):
        """
        Update operation for ISO Dates metadata
        :see: gis_metadata.utils.COMPLEX_DEFINITIONS[DATES]
        """

        tree_to_update = update_props['tree_to_update']
        xpath_root = self._data_map['_dates_root']

        if self.dates:
            date_type = self.dates[DATE_TYPE]

            # First remove all date info from common root
            remove_element(tree_to_update, xpath_root)

            if date_type == DATE_TYPE_MULTIPLE:
                xpath_root += '/TimeInstant'
            elif date_type == DATE_TYPE_RANGE:
                xpath_root += '/TimePeriod'

        return super(IsoParser, self)._update_dates(xpath_root, **update_props)

    def _update_digital_forms(self, **update_props):
        """
        Update operation for ISO Digital Forms metadata
        :see: gis_metadata.utils.COMPLEX_DEFINITIONS[DIGITAL_FORMS]
        """

        digital_forms = wrap_value(update_props['values'])

        # Update all Digital Form properties: distributionFormat*

        xpath_map = self._data_structures[update_props['prop']]

        dist_format_props = ('name', 'decompression', 'version', 'specification')
        dist_format_xroot = self._data_map['_digital_forms_root']
        dist_format_xmap = {prop: xpath_map[prop] for prop in dist_format_props}
        dist_formats = []

        for digital_form in digital_forms:
            dist_format = {prop: digital_form[prop] for prop in dist_format_props}

            if digital_form.get('content'):
                dist_spec = wrap_value(digital_form.get('specification'))
                dist_spec.append(ISO_DIGITAL_FORMS_DELIM)
                dist_spec.extend(wrap_value(digital_form['content']))
                dist_format['specification'] = dist_spec

            dist_formats.append(dist_format)

        update_props['values'] = dist_formats
        dist_formats = update_complex_list(
            xpath_root=dist_format_xroot, xpath_map=dist_format_xmap, **update_props
        )

        # Update all Network Resources: transferOptions+

        trans_option_props = ('access_desc', 'access_instrs', 'network_resource')
        trans_option_xroot = self._data_map['_transfer_options_root']
        trans_option_xmap = {prop: self._data_map['_' + prop] for prop in trans_option_props}

        trans_options = []
        for digital_form in digital_forms:
            trans_options.append({prop: digital_form[prop] for prop in trans_option_props})

        update_props['values'] = trans_options
        trans_options = update_complex_list(
            xpath_root=trans_option_xroot, xpath_map=trans_option_xmap, **update_props
        )

        return {
            'distribution_formats': dist_formats,
            'transfer_options': trans_options
        }

    def _update_keywords(self, **update_props):
        """ Update operation for ISO type-specific Keywords metadata: Theme or Place """

        tree_to_update = update_props['tree_to_update']
        prop = update_props['prop']
        values = update_props['values']

        keywords = []

        if prop in KEYWORD_PROPS:
            xpath_root = self._data_map['_keywords_root']
            xpath_map = self._data_structures[prop]

            xtype = xpath_map['keyword_type']
            xroot = xpath_map['keyword_root']
            xpath = xpath_map['keyword']
            ktype = KEYWORD_TYPES[prop]

            # Remove descriptiveKeyword nodes according to type
            for element in get_elements(tree_to_update, xpath_root):
                if get_element_text(element, xtype).lower() == ktype.lower():
                    remove_element(tree_to_update, xpath_root)

            element = insert_element(tree_to_update, 0, xpath_root)
            insert_element(element, 0, xtype, ktype)  # Add the type node

            keywords.extend(update_property(element, xroot, xpath, prop, values))

        return keywords

    def _update_raster_info(self, **update_props):
        """ Derives multiple dimensions from a single raster_info complex struct """

        tree_to_update = update_props['tree_to_update']
        prop = update_props['prop']
        values = update_props.pop('values')

        # Update number of dimensions at raster_info root (applies to all dimensions below)

        xroot, xpath = None, self._data_map['_ri_num_dims']
        raster_info = [update_property(tree_to_update, xroot, xpath, prop, values.get('dimensions', u''))]

        # Derive vertical, longitude, and latitude dimensions from raster_info

        xpath_root = self._get_xroot_for(prop)
        xpath_map = self._data_structures[prop]

        v_dimension = {}
        if values.get('vertical_count'):
            v_dimension = v_dimension.fromkeys(xpath_map, u'')
            v_dimension['type'] = 'vertical'
            v_dimension['size'] = values.get('vertical_count', u'')

        x_dimension = {}
        if values.get('column_count') or values.get('x_resolution'):
            x_dimension = x_dimension.fromkeys(xpath_map, u'')
            x_dimension['type'] = 'column'
            x_dimension['size'] = values.get('column_count', u'')
            x_dimension['value'] = values.get('x_resolution', u'')

        y_dimension = {}
        if values.get('row_count') or values.get('y_resolution'):
            y_dimension = y_dimension.fromkeys(xpath_map, u'')
            y_dimension['type'] = 'row'
            y_dimension['size'] = values.get('row_count', u'')
            y_dimension['value'] = values.get('y_resolution', u'')

        # Update derived dimensions as complex list, and append affected elements for return

        update_props['prop'] = RASTER_DIMS
        update_props['values'] = [v_dimension, x_dimension, y_dimension]

        raster_info += update_complex_list(xpath_root=xpath_root, xpath_map=xpath_map, **update_props)

        return raster_info

    def update(self, use_template=False, **metadata_defaults):
        """ OVERRIDDEN: Prevents writing multiple CharacterStrings per XPATH property """

        self.validate()

        tree_to_update = self._xml_tree if not use_template else self._get_template(**metadata_defaults)
        supported_props = self._metadata_props

        # Iterate over keys, and extract non-primitive root for all XPATHs
        #    xroot = identificationInfo/MD_DataIdentification/abstract/
        #    xpath = identificationInfo/MD_DataIdentification/abstract/CharacterString
        #
        # This prevents multiple primitive tags from being inserted under an element

        for prop, xpath in self._data_map.items():
            if not prop.startswith('_') or prop.strip('_') in supported_props:
                # Send only public or alternate properties
                xroot = self._trim_xpath(xpath, prop)
                values = getattr(self, prop, u'')
                update_property(tree_to_update, xroot, xpath, prop, values, supported_props)

        return tree_to_update

    def _trim_xpath(self, xpath, prop):
        """ Removes primitive type tags from an XPATH """

        xroot = self._get_xroot_for(prop)

        if xroot is None and isinstance(xpath, str):
            xtags = xpath.split(XPATH_DELIM)

            if xtags[-1] in ISO_TAG_PRIMITIVES:
                xroot = XPATH_DELIM.join(xtags[:-1])

        return xroot


text:
package com.github.zugaldia.noaa.models.elements;

import org.simpleframework.xml.*;

import java.util.List;

/**
 * Created by antonio on 6/26/16.
 */
@Root(name = "weather-conditions", strict = false)
public class WeatherConditionsData {

    @ElementList(entry = "value", required = false, inline = true)
    private List<WeatherConditionsValueData> values;

    public WeatherConditionsData() {
    }

    public List<WeatherConditionsValueData> getValues() {
        return values;
    }

}

redpajama_set_name: RedPajamaGithub


text:
<?php
function testUnaryExpressionSuppressWarning(\SplObjectStorage $storage)
{
    return @$storage->offsetGet('Sindelfingen');
}


text:
layout: bidder
title: ResultsMedia
description: Prebid ResultsMedia Bidder Adaptor
pbjs: true
biddercode: resultsmedia
media_types: video, banner
gdpr_supported: true
schain_supported: true
enable_download: false
pbjs_version_notes: not ported to 5.x
---

### Bid Params

{: .table .table-bordered .table-striped }
| Name          | Scope    | Description | Example | Type     |
|---------------|----------|-------------|---------|----------|
| zoneId | required | The zone ID from ResultsMedia. | 12345 | integer |


text:
package at.ac.tuwien.kr.alpha.api.programs;

import at.ac.tuwien.kr.alpha.api.rules.NormalRule;

public interface NormalProgram extends Program<NormalRule> {

}


text:
package controllers.api;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import controllers.UserApp;
import controllers.annotation.IsAllowed;
import controllers.annotation.IsCreatable;
import models.*;
import models.enumeration.Operation;
import models.enumeration.ProjectScope;
import models.enumeration.ResourceType;
import models.enumeration.RoleType;
import org.apache.commons.lang3.StringUtils;
import play.db.ebean.Model;
import play.db.ebean.Transactional;
import play.i18n.Messages;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import playRepository.RepositoryService;
import utils.AccessControl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static models.AbstractPosting.findByProject;
import static play.libs.Json.toJson;
import static utils.CacheStore.getProjectCacheKey;
import static utils.CacheStore.projectMap;

public class ProjectApi extends Controller {
    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");

    @IsAllowed(Operation.DELETE)
    public static Result exports(String owner, String projectName) {
        Project project = Project.findByOwnerAndProjectName(owner, projectName);

        ObjectNode json = Json.newObject();
        json.put("owner", project.owner);
        json.put("projectName", project.name);
        json.put("projectDescription", project.overview);
        json.put("projectCreatedDate", getDateString(project.createdDate));
        json.put("projectVcs", project.vcs);
        json.put("projectScope", getProjectScope(project));
        json.put("assignees", toJson(getAssginees(project).toArray()));
        json.put("authors", toJson(getAuthors(project).toArray()));
        json.put("memberCount", project.members().size());
        json.put("members", project.members().size());
        Optional.ofNullable(project.members())
                .ifPresent(members -> json.put("members", composeMembersJson(project)));
        json.put("issueCount", project.issues.size());
        json.put("postCount", project.posts.size());
        json.put("milestoneCount", project.milestones.size());
        json.put("labels", getAllLabels(project.issueLabels));
        json.put("issues", composePosts(project, Issue.finder));
        json.put("posts", composePosts(project, Posting.finder));
        json.put("milestones", toJson(project.milestones.stream()
                .map(ProjectApi::getMilestoneNode).collect(Collectors.toList())));
        return ok(json);
    }

    private static String getProjectScope(Project project) {
        switch (project.projectScope) {
            case PRIVATE:
                return "PRIVATE";
            case PROTECTED:
                return "PROTECTED";
            case PUBLIC:
                return "PUBLIC";
            default:
                return "PRIVATE";
        }
    }

    public static List<ObjectNode> getAssginees(Project project) {
        List<ObjectNode> members = new ArrayList<>();
        for (Assignee assignee : project.assignees) {
            ObjectNode member = Json.newObject();
            member.put("loginId", assignee.user.loginId);
            member.put("name", assignee.user.name);
            member.put("email", assignee.user.email);
            members.add(member);
        }
        return members;
    }

    public static List<ObjectNode> getAuthors(Project project) {
        List<ObjectNode> authors = new ArrayList<>();
        for (User user : project.findAuthors()) {
            ObjectNode member = Json.newObject();
            member.put("loginId", user.loginId);
            member.put("name", user.name);
            member.put("email", user.email);
            authors.add(member);
        }
        return authors;
    }

    @Transactional
    public static Result newProject(String owner) throws Exception {
        ObjectNode result = Json.newObject();
        JsonNode json = request().body().asJson();
        if (json == null) {
            return badRequest(result.put("message", "Expecting Json data"));
        }

        User currentUser = UserApp.currentUser();
        if (!currentUser.isSiteManager()) {
            return badRequest(result.put("message", "User creation with api is allowed by Site admin only."));
        }

        Project existed = Project.findByOwnerAndProjectName(owner, json.findValue("projectName").asText());
        if (existed != null) {
            result.put("status", 409);
            result.put("reason", "Conflict");
            result.put("project", createdProjectNode(existed));
            return badRequest(result);
        }

        Organization organization = Organization.findByName(owner);

        if ((!AccessControl.isGlobalResourceCreatable(currentUser))
                || (Organization.isNameExist(owner) && !OrganizationUser.isAdmin(organization.id, currentUser.id))) {
            return forbidden(result.put("message", Messages.get("'" + currentUser.name + "' has no permission")));
        }

        Project project = new Project();
        project.owner = owner;
        project.name = json.findValue("projectName").asText();
        project.overview = getProjectDescription(json);
        project.vcs = getProjectVcs(json);
        project.createdDate = IssueApi.parseDateString(json.findValue("projectCreatedDate"));
        project.projectScope = parseProjectScope(json);

        if (Organization.isNameExist(owner)) {
            project.organization = organization;
        }

        ProjectUser.assignRole(User.SITE_MANAGER_ID, Project.create(project), RoleType.SITEMANAGER);
        RepositoryService.createRepository(project);

        // TODO project settings 도 export를 하는 것이 좋을 것 같다
        saveMenuSettingsToDefault(project);
        addProjectMembers(json, project);

        projectMap.put(getProjectCacheKey(project.owner, project.name), project.id);

        return created(createdProjectNode(project));
    }

    private static ProjectScope parseProjectScope(JsonNode json) {
        JsonNode scopeNode = json.findValue("projectScope");
        if (scopeNode == null || StringUtils.isEmpty(scopeNode.asText())) {
            return ProjectScope.PRIVATE;
        }

        switch (scopeNode.asText()) {
            case "PRIVATE":
                return ProjectScope.PRIVATE;
            case "PUBLIC":
                return ProjectScope.PUBLIC;
            case "PROTECTED":
                return ProjectScope.PROTECTED;
            default:
                return ProjectScope.PRIVATE;
        }
    }

    private static String getProjectDescription(JsonNode json) {
        JsonNode projectDescription = json.findValue("projectDescription");
        if (projectDescription == null) {
            return "";
        }
        return projectDescription.asText();
    }

    private static void addProjectMembers(JsonNode json, Project project) {
        JsonNode membersNode = json.findValue("members");

        if (membersNode != null && membersNode.isArray()) {
            // find members and add members
            for (JsonNode memberNode : membersNode) {
                String mail = memberNode.findValue("email").asText();
                User member = User.findByEmail(mail);
                if (!member.isAnonymous()) {
                    String role = memberNode.findValue("role").asText();
                    if ("member".equalsIgnoreCase(role)) {
                        ProjectUser.assignRole(member.id, project.id, RoleType.MEMBER);
                    } else if ("manager".equalsIgnoreCase(role)) {
                        ProjectUser.assignRole(member.id, project.id, RoleType.MANAGER);
                    } else {
                        play.Logger.warn("Unknown role type: " + member);
                    }
                }
            }
            project.cleanEnrolledUsers();
        }
    }

    private static String getProjectVcs(JsonNode json) {
        JsonNode projectVcs = json.findValue("projectVcs");
        if (projectVcs == null) {
            return "GIT";
        }
        return projectVcs.asText();
    }

    private static JsonNode createdProjectNode(Project project) {
        ObjectNode created = Json.newObject();
        created.put("id", project.id);
        created.put("owner", project.owner);
        created.put("name", project.name);
        created.put("overview", project.overview);
        created.put("vcs", project.vcs);
        return created;
    }

    private static void saveMenuSettingsToDefault(Project project) {
        ProjectMenuSetting projectMenuSetting = new ProjectMenuSetting();
        projectMenuSetting.code = true;
        projectMenuSetting.issue = true;
        projectMenuSetting.pullRequest = true;
        projectMenuSetting.review = true;
        projectMenuSetting.milestone = true;
        projectMenuSetting.board = true;
        projectMenuSetting.project = project;

        projectMenuSetting.save();
        project.menuSetting = projectMenuSetting;
        project.update();
    }

    @IsAllowed(Operation.READ)
    public static JsonNode projectLabels(Project project) {

        Map<Long, Map<String, String>> labels = new HashMap<>();
        for (Label label : project.labels) {
            labels.put(label.id, convertToMap(label));
        }

        return toJson(labels);
    }

    /**
     * convert from some part of {@link models.Label} to {@link java.util.Map}
     * @param label {@link models.Label} object
     * @return label's map data
     */
    private static Map<String, String> convertToMap(Label label) {
        Map<String, String> tagMap = new HashMap<>();
        tagMap.put("category", label.category);
        tagMap.put("name", label.name);
        return tagMap;
    }

    private static <T> JsonNode composePosts(Project project, Model.Finder<Long, T> finder) {
        List<ObjectNode> result = findByProject(finder, project).stream()
                .map(posting -> getResult((AbstractPosting) posting))
                .collect(Collectors.toList());

        return toJson(result);
    }

    public static ObjectNode getResult(AbstractPosting posting) {
        ObjectNode json = Json.newObject();
        json.put("number", posting.getNumber());
        json.put("id", posting.id);
        json.put("title", posting.title);
        json.put("type", posting.asResource().getType().toString());
        json.put("author", composeAuthorJson(posting.getAuthor()));
        json.put("createdAt", getDateString(posting.createdDate));
        json.put("updatedAt", getDateString(posting.updatedDate));
        json.put("body", posting.body);

        if (posting.asResource().getType() == ResourceType.ISSUE_POST) {
            Issue issue = ((Issue) posting);
            Optional.ofNullable(issue.assignee)
                    .ifPresent(assignee -> json.put("assignees", composeAssigneeJson(issue)));
            json.put("state", issue.state.toString());
            Optional.ofNullable(issue.getLabels()).ifPresent(labels -> {
                if (labels.size() > 0) {
                    json.put("labels", composeLabelJson(labels));
                }
            });
            Optional.ofNullable(issue.milestone).
                    ifPresent(milestone -> json.put("milestoneId", milestone.id));
            Optional.ofNullable(issue.milestone)
                    .ifPresent(milestone -> json.put("milestoneTitle", milestone.title));
            Optional.ofNullable(issue.dueDate).ifPresent(dueDate ->
                    json.put("dueDate", getDateString(dueDate)));

        }
        List<Attachment> attachments = Attachment.findByContainer(posting.asResource());
        if (attachments.size() > 0) {
            json.put("attachments", toJson(attachments));
        }

        List<ObjectNode> comments = composePlainCommentsJson(posting);
        if (comments.size() > 0) {
            json.put("comments", toJson(comments));
        }
        return json;
    }

    private static String getDateString(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd a hh:mm:ss Z", Locale.ENGLISH);
        return df.format(date);
    }

    private static JsonNode composeAuthorJson(User user) {
        ObjectNode authorNode = Json.newObject();
        authorNode.put("loginId", user.loginId);
        authorNode.put("name", user.name);
        authorNode.put("email", user.email);
        return authorNode;
    }

    // It may be looks like weired. But it is intended for future
    // which may introduce multiple assignees feature
    public static JsonNode composeAssigneeJson(Issue issue) {
        List<ObjectNode> assignees = new ArrayList<>();
        Assignee assignee = issue.assignee;

        ObjectNode assigneelNode = Json.newObject();
        assigneelNode.put("loginId", assignee.user.loginId);
        assigneelNode.put("name", assignee.user.name);
        assigneelNode.put("email", assignee.user.email);
        assignees.add(assigneelNode);

        return toJson(assignees);
    }

    private static JsonNode composeMembersJson(Project project) {
        List<ObjectNode> members = new ArrayList<>();

        for (ProjectUser projectUser : project.members()) {
            User user = projectUser.user;
            ObjectNode memberNode = Json.newObject();
            memberNode.put("loginId", user.loginId);
            memberNode.put("name", user.name);
            memberNode.put("role", projectUser.role.name);
            memberNode.put("email", user.email);
            members.add(memberNode);
        }

        return toJson(members);
    }

    private static JsonNode composeLabelJson(Set<IssueLabel> issueLabels) {
        List<ObjectNode> labels = new ArrayList<>();
        for (IssueLabel label : issueLabels) {
            ObjectNode labelNode = Json.newObject();
            labelNode.put("labelName", label.name);
            labelNode.put("labelColor", label.color);
            labelNode.put("category", label.category.name);
            labels.add(labelNode);
        }
        return toJson(labels);
    }

    public static JsonNode getAllLabels(List<IssueLabel> issueLabels) {
        List<ObjectNode> labels = new ArrayList<>();
        for (IssueLabel label : issueLabels) {
            ObjectNode labelNode = Json.newObject();
            labelNode.put("labelName", label.name);
            labelNode.put("labelColor", label.color);
            labelNode.put("category", label.category.name);
            labelNode.put("isExclusive", label.category.isExclusive);
            labels.add(labelNode);
        }
        return toJson(labels);
    }

    public static List<ObjectNode> composePlainCommentsJson(AbstractPosting posting) {
        List<ObjectNode> comments = new ArrayList<>();
        for (Comment comment : posting.getComments()) {
            ObjectNode commentNode = Json.newObject();
            commentNode.put("id", comment.id);
            commentNode.put("type", comment.asResource().getType().toString());
            User commentAuthor = User.find.byId(comment.authorId);
            commentNode.put("author", composeAuthorJson(commentAuthor));
            commentNode.put("createdAt", getDateString(comment.createdDate));
            commentNode.put("body", comment.contents);

            List<Attachment> attachments = Attachment.findByContainer(comment.asResource());
            if (attachments.size() > 0) {
                commentNode.put("attachments", toJson(attachments));
            }
            comments.add(commentNode);
        }
        return comments;
    }

    public static ObjectNode getMilestoneNode(Milestone m) {
        ObjectNode node = Json.newObject();
        node.put("id", m.id);
        node.put("title", m.title);
        node.put("state", m.state.state());
        node.put("description", m.contents);
        Optional.ofNullable(m.dueDate).ifPresent(dueDate
                -> node.put("dueDate", getDateString(dueDate)));
        return node;
    }

    @Transactional
    @IsCreatable(ResourceType.ISSUE_LABEL)
    public static Result newLabel(String owner, String projectName) {
        ObjectNode result = Json.newObject();
        JsonNode json = request().body().asJson();
        if (json == null) {
            return badRequest(result.put("message", "Expecting Json data"));
        }

        JsonNode labelsNode = json.findValue("labels");
        if (labelsNode == null || !labelsNode.isArray()) {
            return badRequest(result.put("message", "No issues key exists or value wasn't array!"));
        }

        Project project = Project.findByOwnerAndProjectName(owner, projectName);

        List<JsonNode> createdLabels = new ArrayList<>();

        for (JsonNode labelNode : labelsNode) {
            createdLabels.add(createLabelNode(labelNode, project));
        }
        project.update();

        return created(toJson(createdLabels));
    }

    private static JsonNode createLabelNode(JsonNode json, Project project) {
        String labelName = json.findValue("labelName").asText();
        String labelColor = json.findValue("labelColor").asText();
        String categoryName = json.findValue("category").asText();
        boolean isExclusive = json.findValue("isExclusive").isBoolean();

        IssueLabelCategory category = IssueLabelCategory.findByName(categoryName, project);
        if (category == null) {
            category = new IssueLabelCategory();
            category.project = project;
            category.name = categoryName;
            category.save();
        }

        IssueLabel label = IssueLabel.findByName(labelName, categoryName, project);
        if( label == null ) {
            label = new IssueLabel();
            label.category = category;
            label.color = labelColor;
            label.name = labelName;
            label.project = project;
            label.save();
        } else {
            return existedLabel(json);
        }

        ObjectNode result = Json.newObject();
        result.put("status", 201);
        result.put("label", label.name);
        result.put("category", category.name);
        result.put("labelColor", label.color);
        result.put("isExclusive", category.isExclusive);

        return result;
    }

    private static JsonNode existedLabel(JsonNode labelNode) {
        ObjectNode createdUserNode = Json.newObject();
        String message = Messages.get("label.error.duplicated");

        createdUserNode.put("status", 409);
        createdUserNode.put("reason", "Conflict");
        createdUserNode.put("message", message);
        createdUserNode.put("user", labelNode);

        return createdUserNode;
    }
}


text:
Freshy Spa
==========

Simple online system to manage customer and service scheduling.

Features:

* Collaborators
* Services
* Customers
* Scheduling

## Windows setup

1. Download and install [RailsInstaller](http://railsinstaller.org/en) with Ruby 2.2.

2. Download and install [NodeJS](https://nodejs.org/en/).

3. Restart your computer to apply all the changes.

4. Open up a command line prompt (Choose "Run" from your start menu and type 'cmd.exe') and verify that you have Ruby installed:

  ```sh
  ruby -v
  ```
  It should respond with a version.
5. Verify that you have Ruby on Rails installed:

  ```sh
  rails --version
  ```
  If the last command does not show the current version of rails, run the following command, wait until finish, and check version again:

  ```sh
  gem install rails
  ```
6. Configure yout GitHub account

  ```sh
  git config --global user.name "YOUR NAME FROM GITHUB ACCOUNT"
  git config --global user.email "YOUR EMAIL ADDRESS FROM GITHUB ACCOUNT"
  ```

## APP Setup

1. Using the command line prompt, change the current directory to the location where you want the cloned directory to be made.

  Example:
  ```sh
  cd projects
  ```
2. Clone a copy of the repository:

  ```sh
  git clone https://github.com/BrunoHideo/freshy-spa.git
  ```
  If you are having trouble, check [this link](https://help.github.com/articles/cloning-a-repository/).
3. Change to the cloned directory:

  ```sh
  cd freshy-spa
  ```
4. Install bundler:

  ```sh
  gem install bundler
  ```
5. Install all the libraries:

  ```sh
  bundle install
  ```
6. Setup the database:

  ```sh
  rake db:create 'db/development.sqlite3'
  rake db:migrate
  rake db:seed
  ```
7. Run rails server

  ```sh
  rails s
  ```
8. Open a browser and access http://localhost:3000/

9. Have fun!

To shut down the server, simply `ctrl + c` on command line prompt

## Development Team

Project developed for the discipline of software engineering lab, of the ADS graduation course from FATEC SP, as partial requirement for the semester conclusion.

Bruno Hideo

Fabio Domi

Lucas Ponciano

Luiz Martins

Sergio Donatti


text:
package snow.skittles;

import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;

/**
 * A contract to be implemented by a Skittle class
 */
public interface BaseSkittle {

  void setIconDrawable(Drawable drawable);

  Drawable getIcon();

  void setColor(@ColorInt int color);

  @ColorInt int getColor();

  @SkittleType int getType();
}


text:
<!DOCTYPE html>

    <title>ImageTools Demo Page</title>

ImageTools Demo Page


text:
"""Support for displaying collected data over SNMP."""
import logging
from datetime import timedelta

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.helpers.entity import Entity
from homeassistant.const import (
    CONF_HOST, CONF_NAME, CONF_PORT, CONF_UNIT_OF_MEASUREMENT, STATE_UNKNOWN,
    CONF_USERNAME, CONF_VALUE_TEMPLATE)

_LOGGER = logging.getLogger(__name__)

CONF_BASEOID = 'baseoid'
CONF_COMMUNITY = 'community'
CONF_VERSION = 'version'
CONF_AUTH_KEY = 'auth_key'
CONF_AUTH_PROTOCOL = 'auth_protocol'
CONF_PRIV_KEY = 'priv_key'
CONF_PRIV_PROTOCOL = 'priv_protocol'
CONF_ACCEPT_ERRORS = 'accept_errors'
CONF_DEFAULT_VALUE = 'default_value'

DEFAULT_COMMUNITY = 'public'
DEFAULT_HOST = 'localhost'
DEFAULT_NAME = 'SNMP'
DEFAULT_PORT = '161'
DEFAULT_VERSION = '1'
DEFAULT_AUTH_PROTOCOL = 'none'
DEFAULT_PRIV_PROTOCOL = 'none'

SNMP_VERSIONS = {
    '1': 0,
    '2c': 1,
    '3': None
}

MAP_AUTH_PROTOCOLS = {
    'none': 'usmNoAuthProtocol',
    'hmac-md5': 'usmHMACMD5AuthProtocol',
    'hmac-sha': 'usmHMACSHAAuthProtocol',
    'hmac128-sha224': 'usmHMAC128SHA224AuthProtocol',
    'hmac192-sha256': 'usmHMAC192SHA256AuthProtocol',
    'hmac256-sha384': 'usmHMAC256SHA384AuthProtocol',
    'hmac384-sha512': 'usmHMAC384SHA512AuthProtocol',
}

MAP_PRIV_PROTOCOLS = {
    'none': 'usmNoPrivProtocol',
    'des': 'usmDESPrivProtocol',
    '3des-ede': 'usm3DESEDEPrivProtocol',
    'aes-cfb-128': 'usmAesCfb128Protocol',
    'aes-cfb-192': 'usmAesCfb192Protocol',
    'aes-cfb-256': 'usmAesCfb256Protocol',
}

SCAN_INTERVAL = timedelta(seconds=10)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Required(CONF_BASEOID): cv.string,
    vol.Optional(CONF_ACCEPT_ERRORS, default=False): cv.boolean,
    vol.Optional(CONF_COMMUNITY, default=DEFAULT_COMMUNITY): cv.string,
    vol.Optional(CONF_DEFAULT_VALUE): cv.string,
    vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
    vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
    vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
    vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
    vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
    vol.Optional(CONF_VERSION, default=DEFAULT_VERSION): vol.In(SNMP_VERSIONS),
    vol.Optional(CONF_USERNAME): cv.string,
    vol.Optional(CONF_AUTH_KEY): cv.string,
    vol.Optional(CONF_AUTH_PROTOCOL, default=DEFAULT_AUTH_PROTOCOL):
        vol.In(MAP_AUTH_PROTOCOLS),
    vol.Optional(CONF_PRIV_KEY): cv.string,
    vol.Optional(CONF_PRIV_PROTOCOL, default=DEFAULT_PRIV_PROTOCOL):
        vol.In(MAP_PRIV_PROTOCOLS),
})

async def async_setup_platform(
        hass, config, async_add_entities, discovery_info=None):
    """Set up the SNMP sensor."""
    from pysnmp.hlapi.asyncio import (
        getCmd, CommunityData, SnmpEngine, UdpTransportTarget, ContextData,
        ObjectType, ObjectIdentity, UsmUserData)

    name = config.get(CONF_NAME)
    host = config.get(CONF_HOST)
    port = config.get(CONF_PORT)
    community = config.get(CONF_COMMUNITY)
    baseoid = config.get(CONF_BASEOID)
    unit = config.get(CONF_UNIT_OF_MEASUREMENT)
    version = config.get(CONF_VERSION)
    username = config.get(CONF_USERNAME)
    authkey = config.get(CONF_AUTH_KEY)
    authproto = config.get(CONF_AUTH_PROTOCOL)
    privkey = config.get(CONF_PRIV_KEY)
    privproto = config.get(CONF_PRIV_PROTOCOL)
    accept_errors = config.get(CONF_ACCEPT_ERRORS)
    default_value = config.get(CONF_DEFAULT_VALUE)
    value_template = config.get(CONF_VALUE_TEMPLATE)

    if value_template is not None:
        value_template.hass = hass

    if version == '3':
        import pysnmp.hlapi.asyncio as hlapi

        if not authkey:
            authproto = 'none'
        if not privkey:
            privproto = 'none'

        request_args = [
            SnmpEngine(),
            UsmUserData(
                username, authKey=authkey or None, privKey=privkey or None,
                authProtocol=getattr(hlapi, MAP_AUTH_PROTOCOLS[authproto]),
                privProtocol=getattr(hlapi, MAP_PRIV_PROTOCOLS[privproto]),),
            UdpTransportTarget((host, port)),
            ContextData(),
        ]
    else:
        request_args = [
            SnmpEngine(),
            CommunityData(community, mpModel=SNMP_VERSIONS[version]),
            UdpTransportTarget((host, port)),
            ContextData(),
        ]

    errindication, _, _, _ = await getCmd(
        *request_args, ObjectType(ObjectIdentity(baseoid)))

    if errindication and not accept_errors:
        _LOGGER.error("Please check the details in the configuration file")
        return

    data = SnmpData(request_args, baseoid, accept_errors, default_value)
    async_add_entities([SnmpSensor(data, name, unit, value_template)], True)

class SnmpSensor(Entity):
    """Representation of a SNMP sensor."""

    def __init__(self, data, name, unit_of_measurement, value_template):
        """Initialize the sensor."""
        self.data = data
        self._name = name
        self._state = None
        self._unit_of_measurement = unit_of_measurement
        self._value_template = value_template

    @property
    def name(self):
        """Return the name of the sensor."""
        return self._name

    @property
    def state(self):
        """Return the state of the sensor."""
        return self._state

    @property
    def unit_of_measurement(self):
        """Return the unit the value is expressed in."""
        return self._unit_of_measurement

    async def async_update(self):
        """Get the latest data and updates the states."""
        await self.data.async_update()
        value = self.data.value

        if value is None:
            value = STATE_UNKNOWN
        elif self._value_template is not None:
            value = self._value_template.async_render_with_possible_json_value(
                value, STATE_UNKNOWN)

        self._state = value

class SnmpData:
    """Get the latest data and update the states."""

    def __init__(self, request_args, baseoid, accept_errors, default_value):
        """Initialize the data object."""
        self._request_args = request_args
        self._baseoid = baseoid
        self._accept_errors = accept_errors
        self._default_value = default_value
        self.value = None

    async def async_update(self):
        """Get the latest data from the remote SNMP capable host."""
        from pysnmp.hlapi.asyncio import (getCmd, ObjectType, ObjectIdentity)

        errindication, errstatus, errindex, restable = await getCmd(
            *self._request_args, ObjectType(ObjectIdentity(self._baseoid)))

        if errindication and not self._accept_errors:
            _LOGGER.error("SNMP error: %s", errindication)
        elif errstatus and not self._accept_errors:
            _LOGGER.error("SNMP error: %s at %s", errstatus.prettyPrint(),
                          errindex and restable[-1][int(errindex) - 1] or '?')
        elif (errindication or errstatus) and self._accept_errors:
            self.value = self._default_value
        else:
            for resrow in restable:
                self.value = str(resrow[-1])


text:
import re
import json
import sys

def conceptnet_execute_query(input_dict):
    """
    Executes the input Conceptnet query.
    """
    import urllib2
    site = "http://conceptnet5.media.mit.edu/data/5.2/" + input_dict["query"]
    response = urllib2.urlopen(site)

    output_dict = {}
    output_dict["json"] = response.read()
    return output_dict

def conceptnet_create_search_query(input_dict):
    """
    Constructs a Conceptnet search query.
    """
    input_limit = input_dict['limit']
    input_offset = input_dict['offset']
    input_text = input_dict['text']
    input_minweight = input_dict['minweight']

    query = "search?"

    if input_limit != None and input_limit != '':
        query = query + 'limit=' + str(input_limit) + '&'
    if input_offset != None and input_offset != '':
        query = query + 'offset=' + str(input_offset) + '&'
    if input_text != None and input_text != '':
        query = query + 'text=' + str(input_text) + '&'
    if input_minweight != None and input_minweight != '':
        query = query + 'minWeight=' + str(input_minweight) + '&'

    output_dict = {}
    output_dict["query"] = query[0:len(query)-1]
    return output_dict

def concept_net_triplets(input_dict):
    """
    Extracts Subject, Predicate, Object triplets from the Conceptnet response JSON.
    """
    json_response = input_dict['concept_net_json_response']
    try:
        data = json.loads(json_response)
        triplets = [[e['start'], e['rel'], e['end']] for e in data['edges']]
    except:
        raise Exception("Cannot parse this JSON string. Is it a proper Conceptnet API response JSON?")

    # Make a separate triplet list without conceptnet prefixes
    basename_triplets = []
    for start, rel, end in triplets:
        start = start.split('/')[-1]
        rel = rel.split('/')[-1]
        end = end.split('/')[-1]
        basename_triplets.append([start, rel, end])

    output_dict = {}
    output_dict['triplets'] = json.dumps(triplets, indent=2)
    output_dict['basename_triplets'] = json.dumps(basename_triplets, indent=2)
    return output_dict


text:
//-----------------------------------------------
//
//	This file is part of the Siv3D Engine.
//
//	Copyright (c) 2008-2022 Ryo Suzuki
//	Copyright (c) 2016-2022 OpenSiv3D Project
//
//	Licensed under the MIT License.
//
//-----------------------------------------------

# include <Siv3D/Physics2D/P2Body.hpp>
# include "P2WheelJointDetail.hpp"
# include "P2WorldDetail.hpp"
# include "P2BodyDetail.hpp"

namespace s3d
{
	detail::P2WheelJointDetail::P2WheelJointDetail(const std::shared_ptr<detail::P2WorldDetail>& world, const P2Body& bodyA, const P2Body& bodyB, const Vec2& worldAnchorPos, const Vec2& normalizedAxis, const EnableCollision enableCollision)
		: m_world{ world }
		, m_bodyA{ bodyA.getWeakPtr() }
		, m_bodyB{ bodyB.getWeakPtr() }
	{
		b2WheelJointDef def;
		def.Initialize(m_bodyA.lock()->getBodyPtr(), m_bodyB.lock()->getBodyPtr(), detail::ToB2Vec2(worldAnchorPos), detail::ToB2Vec2(normalizedAxis));
		def.collideConnected = enableCollision.getBool();
		m_joint = static_cast<b2WheelJoint*>(m_world->getWorldPtr()->CreateJoint(&def));
	}

	detail::P2WheelJointDetail::~P2WheelJointDetail()
	{
		if (not m_joint)
		{
			return;
		}

		if (m_bodyA.expired() || m_bodyB.expired())
		{
			return;
		}

		m_world->getWorldPtr()->DestroyJoint(m_joint);
	}

	b2WheelJoint& detail::P2WheelJointDetail::getJoint()
	{
		assert(m_joint);

		return *m_joint;
	}

	const b2WheelJoint& detail::P2WheelJointDetail::getJoint() const
	{
		assert(m_joint);

		return *m_joint;
	}

	void detail::P2WheelJointDetail::setLinearStiffness(const double frequencyHz, const double dampingRatio) noexcept
	{
		assert(m_joint);

		auto pA = m_bodyA.lock();
		auto pB = m_bodyB.lock();

		if (not pA || not pB)
		{
			return;
		}

		float stiffness, damping;
		b2LinearStiffness(stiffness, damping, static_cast<float>(frequencyHz), static_cast<float>(dampingRatio),
			pA->getBodyPtr(), pB->getBodyPtr());

		m_joint->SetStiffness(stiffness);
		m_joint->SetDamping(damping);
	}
}


text:
package mdn.vtvsport.common;

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class AutoScaleImageViewerMatrix extends ImageView {

	public static int width_viewflow = 0;
	public static int height_viewflow = 0;

	public AutoScaleImageViewerMatrix(Context context) {
		super(context);
	}

	public AutoScaleImageViewerMatrix(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public AutoScaleImageViewerMatrix(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		int widthDrawable = 0;
		int heightDrawable = 0;
		int w = width;
		if (getDrawable() != null) {
			height = w * getDrawable().getIntrinsicHeight()
					/ getDrawable().getIntrinsicWidth();
			widthDrawable = getDrawable().getIntrinsicWidth();
			heightDrawable = getDrawable().getIntrinsicHeight();
			Matrix matrix = getImageMatrix();
			RectF drawableRect = new RectF(0, 0, widthDrawable, heightDrawable);
			RectF viewRect = new RectF(0, 0, w, height);
			matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
			setScaleType(ScaleType.MATRIX);
		}

		width_viewflow = width;
		height_viewflow = height;
		setMeasuredDimension(width, height);
	}
}


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
Beih. Nova Hedwigia 62: 158 (1979)

#### Original name
Buellia trypethelii Tuck.

### Remarks
null


text:
import distutils.version

try:
    import greenlet
    getcurrent = greenlet.greenlet.getcurrent
    GreenletExit = greenlet.greenlet.GreenletExit
    preserves_excinfo = (distutils.version.LooseVersion(greenlet.__version__)
                         >= distutils.version.LooseVersion('0.3.2'))
    greenlet = greenlet.greenlet
except ImportError as e:
    raise
    try:
        from py.magic import greenlet
        getcurrent = greenlet.getcurrent
        GreenletExit = greenlet.GreenletExit
        preserves_excinfo = False
    except ImportError:
        try:
            from stackless import greenlet
            getcurrent = greenlet.getcurrent
            GreenletExit = greenlet.GreenletExit
            preserves_excinfo = False
        except ImportError:
            try:
                from support.stacklesss import greenlet, getcurrent, GreenletExit
                preserves_excinfo = False
                (greenlet, getcurrent, GreenletExit)  # silence pyflakes
            except ImportError as e:
                raise ImportError("Unable to find an implementation of greenlet.")


text:
package com.crunchbang.sanskriti;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SocialFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater
				.inflate(R.layout.fragment_social, container, false);
		return view;

	}

}


text:
package org.jbpm.kie.services.impl.xml;

import org.jbpm.compiler.xml.ProcessDataEventListener;
import org.jbpm.compiler.xml.ProcessDataEventListenerProvider;

public class ServicesProcessDataEventListenerProvider implements ProcessDataEventListenerProvider {

    @Override
    public ProcessDataEventListener newInstance() {

        return new ServicesProcessDataEventListener();
    }

}


text:
package arma.ber4j;

public interface ConnectionHandler {
    void onConnected();
    void onDisconnected(DisconnectType disconnectType);
}


text:
<?php

// Memcache sessions are enabled in php.ini. Rock on.
session_start();


text:
var express = require('express');
var bootstrap = require('./Http/bootstrap');

var app = express();
global.__base = __dirname + '/';

// routes
bootstrap.bootstrap(app, express);

module.exports = app;


text:
<?php
class ModeratableListener extends Doctrine_Record_Listener
{
    /**
     * If the object's moderation has not been set already then perform
     * moderation, using all plugin options for this model
     *
     * To override this behaviour, create a preInsert method on your
     * Moderatable model that sets the moderation_status so this
     * preInsert ends up doing nothing
     *
     * @param Doctrine_Event $event
     */
    public function preInsert(Doctrine_Event $event)
    {
        $object = $event->getInvoker();

        // allows the default behaviour to be overidden. if a moderation_status
        // is already present for an insert then do nothing.
        if(empty($object['moderation_status']))
        {
            $object->doModeration();
        }

        return $object;
    }

    /**
     * Tack on the necessary checks for moderation issues to all DQL Selects
     *
     * @param Doctrine_Event $event
     **/
    public function preDqlSelect(Doctrine_Event $event)
    {
        $params = $event->getParams();
        $field  = $params['alias'] . '.' . 'moderation_status';
        $query  = $event->getQuery();
        $obj    = $event->getInvoker();

        // We only need to add the restriction if:
        // 1 - We are in the root query
        // 2 - We are in the subquery and it defines the component with that alias
        if ((
          !$query->isSubquery()
          || (
            $query->isSubquery()
            && $query->contains(' ' . $params['alias'] . ' ')
          ))
          && ! $query->contains($field))
        {
          $clause =
            '%a%.moderation_status = "safe"
            OR (%a%.moderation_status = "flagged" AND %a%.updated_at > "%now%")';

          if($obj->isPostMod())
          {
            $clause.= ' OR %a%.moderation_status = "unmoderated"';
          }

          $clause = strtr($clause,
          array(
            '%a%'   => $params['alias'],
            '%now%' => $obj->flaggedThreshold()
          )
        );

          $query->addPendingJoinCondition($params['alias'],$clause);
        }
    }
}


text:
<?php

/**
 * ERPDataIOCoreBundle for Elcodi
 *
 * This Bundle is part of ERPDataIO bridge for Elcodi
 *
 * @author Marc Morera <yuhu@mmoreram.com>
 * @link https://github.com/PaymentSuite/paymentsuite/blob/master/src/PaymentSuite/PaymentCoreBundle/Tests/Services/PaymentLoggerTest.php
 * @package ERPDataIOCoreBundle
 *
 * Arkaitz Garro 2014
 */

namespace ERPDataIO\ERPDataIOCoreBundle\Tests\Services;

use Psr\Log\LoggerInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use ERPDataIO\ERPDataIOCoreBundle\DependencyInjection\ERPDataIOCoreExtension;

/**
 * Base abstract class for testing
 */
abstract class AbstractERPDataIOTest extends \PHPUnit_Framework_TestCase
{
    private $extension;
    private $container;

    public function setUp()
    {
        $this->extension = new ERPDataIOCoreExtension();

        $this->container = new ContainerBuilder();
        $this->container->registerExtension($this->extension);

    }

    /**
     * Returns DI container
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
     */
    protected function getContainer()
    {
        return $this->container;
    }
}


text:
<!DOCTYPE html>

  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Brian Chu</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="bower_components/html5-boilerplate/css/normalize.css">
  <link rel="stylesheet" href="bower_components/html5-boilerplate/css/main.css">
  <link rel="stylesheet" href="app.css">


text:
function obj = setDefaultFile( obj )
% SETDEFAULTFILE Set default filenames for an nlsaKernelDensity_vb object
%
% Modified 2015/12/16

obj = setDefaultFile@nlsaKernelDensity_fb( obj );
obj = setDistanceNormalizationFile( obj, getDefaultDistanceNormalizationFile( obj ) );


text:
using ppapi::thunk::PPB_PDFFont_API;
using ppapi::thunk::EnterResource;

namespace ppapi {
namespace proxy {

class PrivateFontFile : public Resource,
                        public PPB_PDFFont_API {
 public:
  PrivateFontFile(const HostResource& resource)
      : Resource(OBJECT_IS_PROXY, resource) {
  }
  virtual ~PrivateFontFile() {}

  PPB_PDFFont_API* AsPPB_PDFFont_API() { return this; }

  // Sees if we have a cache of the font table and returns a pointer to it.
  // Returns NULL if we don't have it.
  std::string* GetFontTable(uint32_t table) const;

  std::string* AddFontTable(uint32_t table, const std::string& contents);

 private:
  typedef std::map<uint32_t, linked_ptr<std::string> > FontTableMap;
  FontTableMap font_tables_;

  DISALLOW_COPY_AND_ASSIGN(PrivateFontFile);
};

std::string* PrivateFontFile::GetFontTable(uint32_t table) const {
  FontTableMap::const_iterator found = font_tables_.find(table);
  if (found == font_tables_.end())
    return NULL;
  return found->second.get();
}

std::string* PrivateFontFile::AddFontTable(uint32_t table,
                                           const std::string& contents) {
  linked_ptr<std::string> heap_string(new std::string(contents));
  font_tables_[table] = heap_string;
  return heap_string.get();
}

namespace {

PP_Resource GetFontFileWithFallback(
    PP_Instance instance,
    const PP_FontDescription_Dev* description,
    PP_PrivateFontCharset charset) {
  ProxyAutoLock lock;

  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return 0;

  SerializedFontDescription desc;
  desc.SetFromPPFontDescription(*description);

  HostResource result;
  dispatcher->Send(new PpapiHostMsg_PPBPDF_GetFontFileWithFallback(
      API_ID_PPB_PDF, instance, desc, charset, &result));
  if (result.is_null())
    return 0;
  return (new PrivateFontFile(result))->GetReference();
}

bool GetFontTableForPrivateFontFile(PP_Resource font_file,
                                    uint32_t table,
                                    void* output,
                                    uint32_t* output_length) {
  EnterResource<PPB_PDFFont_API> enter(font_file, true);
  if (enter.failed())
    return false;

  PrivateFontFile* object = static_cast<PrivateFontFile*>(enter.object());
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(
      object->pp_instance());
  if (!dispatcher)
    return false;

  std::string* contents = object->GetFontTable(table);
  if (!contents) {
    std::string deserialized;
    dispatcher->Send(new PpapiHostMsg_PPBPDF_GetFontTableForPrivateFontFile(
        API_ID_PPB_PDF, object->host_resource(), table, &deserialized));
    if (deserialized.empty())
      return false;
    contents = object->AddFontTable(table, deserialized);
  }

  *output_length = static_cast<uint32_t>(contents->size());
  if (output)
    memcpy(output, contents->c_str(), *output_length);
  return true;
}

const PPB_PDF pdf_interface = {
  NULL,  // &GetLocalizedString,
  NULL,  // &GetResourceImage,
  &GetFontFileWithFallback,
  &GetFontTableForPrivateFontFile,
};

InterfaceProxy* CreatePDFProxy(Dispatcher* dispatcher) {
  return new PPB_PDF_Proxy(dispatcher);
}

}  // namespace

PPB_PDF_Proxy::PPB_PDF_Proxy(Dispatcher* dispatcher)
    : InterfaceProxy(dispatcher),
      ppb_pdf_impl_(NULL) {
  if (!dispatcher->IsPlugin()) {
    ppb_pdf_impl_ = static_cast<const PPB_PDF*>(
        dispatcher->local_get_interface()(PPB_PDF_INTERFACE));
  }
}

PPB_PDF_Proxy::~PPB_PDF_Proxy() {
}

// static
const InterfaceProxy::Info* PPB_PDF_Proxy::GetInfo() {
  static const Info info = {
    &pdf_interface,
    PPB_PDF_INTERFACE,
    API_ID_PPB_PDF,
    true,
    &CreatePDFProxy,
  };
  return &info;
}

bool PPB_PDF_Proxy::OnMessageReceived(const IPC::Message& msg) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(PPB_PDF_Proxy, msg)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBPDF_GetFontFileWithFallback,
                        OnMsgGetFontFileWithFallback)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBPDF_GetFontTableForPrivateFontFile,
                        OnMsgGetFontTableForPrivateFontFile)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  // TODO(brettw): handle bad messages!
  return handled;
}

void PPB_PDF_Proxy::OnMsgGetFontFileWithFallback(
    PP_Instance instance,
    const SerializedFontDescription& in_desc,
    int32_t charset,
    HostResource* result) {
  PP_FontDescription_Dev desc;
  in_desc.SetToPPFontDescription(&desc);
  result->SetHostResource(instance,
      ppb_pdf_impl_->GetFontFileWithFallback(
          instance, &desc, static_cast<PP_PrivateFontCharset>(charset)));

  // SetToPPFontDescription() creates a var which is owned by the caller.
  PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(desc.face);
}

void PPB_PDF_Proxy::OnMsgGetFontTableForPrivateFontFile(
    const HostResource& font_file,
    uint32_t table,
    std::string* result) {
  // TODO(brettw): It would be nice not to copy here. At least on Linux,
  // we can map the font file into shared memory and read it that way.
  uint32_t table_length = 0;
  if (!ppb_pdf_impl_->GetFontTableForPrivateFontFile(
          font_file.host_resource(), table, NULL, &table_length))
    return;

  result->resize(table_length);
  ppb_pdf_impl_->GetFontTableForPrivateFontFile(font_file.host_resource(),
      table, const_cast<char*>(result->c_str()), &table_length);
}

}  // namespace proxy
}  // namespace ppapi


text:
class Profile;

namespace drive {

bool SetUpUserDataDirectoryForDriveFsTest();

class FakeDriveFsHelper {
 public:
  static const char kPredefinedProfileSalt[];

  FakeDriveFsHelper(Profile* profile, const base::FilePath& mount_path);
  ~FakeDriveFsHelper();

  base::RepeatingCallback<std::unique_ptr<drivefs::DriveFsBootstrapListener>()>
  CreateFakeDriveFsListenerFactory();

  const base::FilePath& mount_path() { return mount_path_; }
  drivefs::FakeDriveFs& fake_drivefs() { return fake_drivefs_; }

 private:
  const base::FilePath mount_path_;
  drivefs::FakeDriveFs fake_drivefs_;

  DISALLOW_COPY_AND_ASSIGN(FakeDriveFsHelper);
};

}  // namespace drive

#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVEFS_TEST_SUPPORT_H_


text:
<?php
/**
 * Returns a collection of relevant Tweets matching a specified query.
 *
 * Please note that Twitter's search service and, by extension, the Search API is not meant to be an exhaustive source of Tweets. Not all Tweets will be indexed
 * or made available via the search interface.
 *
 * @see https://dev.twitter.com/docs/api/1.1/get/search/tweets
 * @see https://dev.twitter.com/docs/using-search Using the Twitter Search AP
 * @see https://dev.twitter.com/docs/working-with-timelines Working with Timelines
 *
 * @package Twitter
 * @version $Id$
 */
class ESL_Twitter_Request_Search extends ESL_Twitter_Request
{
	const REQUEST_PATH = 'search/tweets';
	const REQUEST_METHOD = 'GET';

	const RESULTTYPE_MIXED = 'mixed';
	const RESULTTYPE_RECENT = 'recent';
	const RESULTTYPE_POPULAIR = 'populair';

	/**
	 * A UTF-8 search query of 1,000 characters maximum, including operators. Queries may additionally be limited by complexity.
	 *
	 * @param string $sQuery.
	 */
	public function __construct($sQuery)
	{
		$this->aParameters['q'] = $sQuery;
	}

	/**
	 * Restricts tweets to the given language, given by an ISO 639-1 code.
	 *
	 * Language detection is best-effort.
	 *
	 * @param string $sLang
	 */
	public function setLang($sLang)
	{
		$this->aParameters['lang'] = $sLang;
	}

	/**
	 * Specifies what type of search results you would prefer to receive. The current default is "mixed"
	 *
	 * Valid values include:
	 *  RESULTTYPE_MIXED: Include both popular and real time results in the response.
	 *  RESULTTYPE_RECENT: return only the most recent results in the response
	 *  RESULTTYPE_POPULAR: return only the most popular results in the response.
	 *
	 * @param string $sResultType
	 */
	public function setResultType($sResultType)
	{
		$this->aParameters['result_type'] = $sResultType;
	}

	/**
	 * The number of tweets to return per page, up to a maximum of 100.
	 *
	 * Defaults to 15. This was formerly the "rpp" parameter in the old Search API.
	 *
	 * @param int $iCount
	 */
	public function setCount($iCount)
	{
		$this->aParameters['count'] = $iCount;
	}

	/**
	 * Returns tweets generated before the given date.
	 *
	 * Keep in mind that the search index may not go back as far as the date you specify here.
	 *
	 * @param DateTime $oUntil
	 */
	public function setUntil(DateTime $oUntil)
	{
		$this->aParameters['until'] = $oUntil->format('Y-m-d');
	}

	/**
	 * Returns results with an ID greater than (that is, more recent than) the specified ID.
	 *
	 * There are limits to the number of Tweets which can be accessed through the API. If the limit of Tweets has occured since the since_id, the since_id will be
	 * forced to the oldest ID available.
	 *
	 * @param string $sSinceId
	 */
	public function setSinceId($sSinceId)
	{
		$this->aParameters['since_id'] = $sSinceId;
	}

	/**
	 * Returns results with an ID less than (that is, older than) or equal to the specified ID.
	 *
	 * @param string $sMaxId
	 */
	public function setMaxId($sMaxId)
	{
		$this->aParameters['max_id'] = $sMaxId;
	}
}
?>


text:
package org.apache.camel.processor.enricher;
import java.io.File;

import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.junit.Before;
import org.junit.Test;

public class PollEnrichFileDefaultAggregationStrategyTest extends ContextTestSupport {

    @Override
    @Before
    public void setUp() throws Exception {
        deleteDirectory("target/data/enrich");
        deleteDirectory("target/data/enrichdata");
        super.setUp();
    }

    @Test
    public void testPollEnrichDefaultAggregationStrategyBody() throws Exception {

        getMockEndpoint("mock:start").expectedBodiesReceived("Start");

        MockEndpoint mock = getMockEndpoint("mock:result");
        mock.expectedBodiesReceived("Big file");
        mock.expectedFileExists("target/data/enrich/.done/AAA.fin");
        mock.expectedFileExists("target/data/enrichdata/.done/AAA.dat");

        template.sendBodyAndHeader("file://target/data/enrich", "Start", Exchange.FILE_NAME, "AAA.fin");

        log.info("Sleeping for 0.25 sec before writing enrichdata file");
        Thread.sleep(250);
        template.sendBodyAndHeader("file://target/data/enrichdata", "Big file", Exchange.FILE_NAME, "AAA.dat");
        log.info("... write done");

        assertMockEndpointsSatisfied();

        assertFileDoesNotExists("target/data/enrichdata/AAA.dat.camelLock");
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("file://target/data/enrich?initialDelay=0&delay=10&move=.done")
                    .to("mock:start")
                    .pollEnrich("file://target/data/enrichdata?initialDelay=0&delay=10&readLock=markerFile&move=.done", 10000)
                    .to("mock:result");
            }
        };
    }

    private static void assertFileDoesNotExists(String filename) {
        File file = new File(filename);
        assertFalse("File " + filename + " should not exist, it should have been deleted after being processed", file.exists());
    }
}


text:
import changelog from '../cli/changelog'

changelog.action({})


text:
	Optional elements and attributes

At times it may be required to have an optional XML element or attribute as the source XML may not contain
the attribute or element. Also, it may be that an object field is null and so cannot be serialized. In
such scenarios the element or attribute can be set as not required. The following code example demonstrates
an optional element and attribute.

@Root
public class OptionalExample {

   @Attribute(required=false)
   private int version;

   @Attribute
   private String id;

   @Element(required=false)
   private String name;

   @Element
   private String address;

   public int getId() {
      return id;
   }

   public int getVersion() {
      return version;
   }

   public String getName() {
      return name;
   }

   public String getAddress() {
      return address;
   }
}

For the above object the version and name are not required. So, and XML source document may not contain either of
these details and the object can still be serialized safely. For example take the following XML document, which
is a valid representation of the above object.

<optionalExample id="10">
   <address>Some example address</address>
</optionalExample>

Even without the name and version XML nodes this document can be deserialized in to an object. This feature is
useful when your XML contains optional details and allows more flexible parsing. To further clarify the implementation
of optional fields take the example shown below. This shows how the entry object is deserialized from the above document,
which is contained within a file. Once deserialized the object values can be examined.

Serializer serializer = new Persister();
File source = new File("example.xml");
OptionalExample example = serializer.read(OptionalExample.class, source);

assert example.getVersion() == 0;
assert example.getName() == null;
assert example.getId() == 10;


text:
/*
 *@name rotateDownLeft
 *@className rotateDownLeft animated
 *@cssCode
 *@editionLink codepen.io
 *@author Quinn Keaveney
 *@source AniCollection
 *@sourceUrl https://github.com/anicollection/anicollection
 *@issues https://github.com/anicollection/anicollection/issues
 *@license MIT
 */
@keyframes rotateDownLeft {
  0% {
    transform-origin: left bottom;
    transform: rotate3d(0, 0, 1, -45deg);
  }

  100% {
    transform-origin: left bottom;
    transform: none;
  }
}

.rotateDownLeft {
  animation-name: rotateDownLeft;
}


text:
import pylru

# For reference:
#   workspace_info:
#     0 ws_id id
#     1 ws_name workspace
#     2 username owner
#     3 timestamp moddate
#     4 int max_objid
#     5 permission user_permission
#     6 permission globalread
#     7 lock_status lockstat
#     8 usermeta metadata

class NarrativeInfoCache(object):

    def __init__(self, cache_size):
        self.cache = pylru.lrucache(int(cache_size))

    def clear_cache(self):
        self.cache.clear()

    def check_cache_size(self):
        return len(self.cache)

    def get_info_list(self, ws_lookup_table, wsClient):
        '''
            Given a set of WS info, lookup the corresponding Narrative
            information.

            input:
                ws_lookup_table = dict of ws_id -> workspace_info
            output:
                [{
                    'ws': workspace_info,
                    'nar': narrative_info
                 },
                 ...
                 ]
        '''
        # search the cache and extract what we have, mark what was missed
        res = self._search_cache(ws_lookup_table)
        items = res['items']
        missed_items = res['missed']

        # for objects that were missed, we have to go out and fetch that
        # narrative object info
        all_items = items
        if len(missed_items) > 0:
            more_items = self._fetch_objects_and_cache(missed_items, ws_lookup_table, wsClient)
            all_items += more_items

        return all_items

    def _search_cache(self, ws_lookup_table):
        items = []  # =[{'ws': [...], 'nar': [...]}, ...]
        missed = []  # =[ws_info1, ws_info2, ... ]
        for ws_info in ws_lookup_table.values():
            key = self._get_cache_key(ws_info)
            if key in self.cache:
                items.append({'ws': ws_info, 'nar': self.cache[key]})
            else:
                missed.append(ws_info)
        return {'items': items, 'missed': missed}

    def _fetch_objects_and_cache(self, ws_list, full_ws_lookup_table, wsClient):
        ''' Fetches narrative objects (if possible) for everything in ws_list '''
        obj_ref_list = []
        for ws_info in ws_list:
            if 'narrative' not in ws_info[8]:
                continue
            if not ws_info[8]['narrative'].isdigit() or not int(ws_info[8]['narrative']) > 0:
                continue
            ref = str(ws_info[0]) + '/' + str(ws_info[8]['narrative'])
            obj_ref_list.append({'ref': ref})

        if len(obj_ref_list) == 0:
            return []

        # ignore errors
        get_obj_params = {'objects': obj_ref_list, 'includeMetadata': 1, 'ignoreErrors': 1}
        narrative_list = wsClient.get_object_info3(get_obj_params)['infos']

        items = []
        for nar in narrative_list:
            if nar:
                ws_info = full_ws_lookup_table[nar[6]]
                items.append({'ws': ws_info, 'nar': nar})
                self.cache[self._get_cache_key(ws_info)] = nar
        return items

    def _get_cache_key(self, ws_info):
        return str(ws_info[0]) + '__' + str(ws_info[3])

class NarrativeListUtils(object):

    def __init__(self, cache_size):
        self.narrativeInfo = NarrativeInfoCache(cache_size)

    def list_public_narratives(self, wsClient):
        # get all the workspaces marked as narratorials
        ws_list = wsClient.list_workspace_info({})
        ws_global_list = []
        for ws_info in ws_list:
            if ws_info[6] == 'r':  # indicates that this ws is globally readable
                ws_global_list.append(ws_info)
        # build a ws_list lookup table
        ws_lookup_table = self._build_ws_lookup_table(ws_global_list)
        # based on the WS lookup table, lookup the narratives
        return self.narrativeInfo.get_info_list(ws_lookup_table, wsClient)

    def list_my_narratives(self, my_user_id, wsClient):
        # get all the workspaces marked as narratorials
        ws_list = wsClient.list_workspace_info({'owners': [my_user_id]})
        # build a ws_list lookup table
        ws_lookup_table = self._build_ws_lookup_table(ws_list)
        # based on the WS lookup table, lookup the narratives
        return self.narrativeInfo.get_info_list(ws_lookup_table, wsClient)

    def list_shared_narratives(self, my_user_id, wsClient):
        # get all the workspaces marked as narratorials
        ws_list = wsClient.list_workspace_info({})
        ws_shared_list = []
        for ws_info in ws_list:
            if ws_info[2] == my_user_id:
                continue
            if ws_info[5] == 'n':  # indicates that this ws is globally readable
                continue
            ws_shared_list.append(ws_info)
        # build a ws_list lookup table
        ws_lookup_table = self._build_ws_lookup_table(ws_shared_list)
        # based on the WS lookup table, lookup the narratives
        return self.narrativeInfo.get_info_list(ws_lookup_table, wsClient)

    def list_narratorials(self, wsClient):
        # get all the workspaces marked as narratorials
        ws_list = wsClient.list_workspace_info({'meta': {'narratorial': '1'}})
        # build a ws_list lookup table
        ws_lookup_table = self._build_ws_lookup_table(ws_list)
        # based on the WS lookup table, lookup the narratives
        return self.narrativeInfo.get_info_list(ws_lookup_table, wsClient)

    def _build_ws_lookup_table(self, ws_list):
        ''' builds a lookup table, skips anything without a 'narrative' metadata field set '''
        ws_lookup_table = {}
        for ws_info in ws_list:
            if 'narrative' in ws_info[8]:
                if ws_info[8]['narrative'].isdigit() and int(ws_info[8]['narrative']) > 0:
                    ws_lookup_table[ws_info[0]] = ws_info
        return ws_lookup_table

class NarratorialUtils(object):

    def __init__(self):
        pass

    def _get_workspace_identity(self, wsid):
        if str(wsid).isdigit():
            return {'id': int(wsid)}
        else:
            return {'workspace': str(wsid)}

    def set_narratorial(self, wsid, description, wsClient):
        wsi = self._get_workspace_identity(wsid)
        wsClient.alter_workspace_metadata({'wsi': wsi, 'new': {'narratorial': '1'}})
        wsClient.alter_workspace_metadata({'wsi': wsi, 'new': {'narratorial_description': description}})

    def remove_narratorial(self, wsid, wsClient):
        wsi = self._get_workspace_identity(wsid)
        wsClient.alter_workspace_metadata({'wsi': wsi, 'remove': ['narratorial', 'narratorial_description']})


text:
class HomeController < ApplicationController
  def index
    @appName = 'yatapi'
  end
end