text:
import os
import tempfile
import shutil

from django.test import TransactionTestCase
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import UploadedFile
from django.contrib.auth.models import Group
from django.db import IntegrityError

from hs_core import hydroshare
from hs_core.hydroshare import utils
from hs_core.models import CoreMetaData, Creator, Contributor, Coverage, Rights, Title, Language, \
    Publisher, Identifier, Type, Subject, Description, Date, Format, Relation, Source
from hs_core.testing import MockIRODSTestCaseMixin, TestCaseCommonUtilities
from hs_app_netCDF.models import NetcdfResource, Variable, OriginalCoverage

class TestNetcdfMetaData(MockIRODSTestCaseMixin, TestCaseCommonUtilities, TransactionTestCase):
    def setUp(self):
        super(TestNetcdfMetaData, self).setUp()
        self.group, _ = Group.objects.get_or_create(name='Resource Author')
        self.user = hydroshare.create_account(
            'user1@nowhere.com',
            username='user1',
            first_name='Creator_FirstName',
            last_name='Creator_LastName',
            superuser=False,
            groups=[self.group]
        )

        self.resNetcdf = hydroshare.create_resource(
            resource_type='NetcdfResource',
            owner=self.user,
            title='Snow water equivalent estimation at TWDEF site from Oct 2009 to June 2010'
        )

        self.temp_dir = tempfile.mkdtemp()
        self.netcdf_file_name = 'netcdf_valid.nc'
        self.netcdf_file = 'hs_app_netCDF/tests/{}'.format(self.netcdf_file_name)
        target_temp_netcdf_file = os.path.join(self.temp_dir, self.netcdf_file_name)
        shutil.copy(self.netcdf_file, target_temp_netcdf_file)
        self.netcdf_file_obj = open(target_temp_netcdf_file, 'r')

        self.temp_dir = tempfile.mkdtemp()
        self.netcdf_file_name_crs = 'netcdf_valid_crs.nc'
        self.netcdf_file_crs = 'hs_app_netCDF/tests/{}'.format(self.netcdf_file_name_crs)
        target_temp_netcdf_file_crs = os.path.join(self.temp_dir, self.netcdf_file_name_crs)
        shutil.copy(self.netcdf_file_crs, target_temp_netcdf_file_crs)
        self.netcdf_file_obj_crs = open(target_temp_netcdf_file_crs, 'r')

        self.netcdf_bad_file_name = 'netcdf_invalid.nc'
        self.netcdf_bad_file = 'hs_app_netCDF/tests/{}'.format(self.netcdf_bad_file_name)
        target_temp_bad_netcdf_file = os.path.join(self.temp_dir, self.netcdf_bad_file_name)
        shutil.copy(self.netcdf_bad_file, target_temp_bad_netcdf_file)
        self.netcdf_bad_file_obj = open(target_temp_bad_netcdf_file, 'r')

        temp_text_file = os.path.join(self.temp_dir, 'netcdf_text.txt')
        text_file = open(temp_text_file, 'w')
        text_file.write("NetCDF records")
        self.text_file_obj = open(temp_text_file, 'r')

    def tearDown(self):
        super(TestNetcdfMetaData, self).tearDown()
        if os.path.exists(self.temp_dir):
            shutil.rmtree(self.temp_dir)

    def test_allowed_file_types(self):
        # test allowed file type is '.nc'
        self.assertIn('.nc', NetcdfResource.get_supported_upload_file_types())
        self.assertEqual(len(NetcdfResource.get_supported_upload_file_types()), 1)

        # there should not be any content file
        self.assertEqual(self.resNetcdf.files.all().count(), 0)

        # trying to add a text file to this resource should raise exception
        files = [UploadedFile(file=self.text_file_obj, name=self.text_file_obj.name)]
        with self.assertRaises(utils.ResourceFileValidationException):
            utils.resource_file_add_pre_process(resource=self.resNetcdf, files=files,
                                                user=self.user,
                                                extract_metadata=False)

        # trying to add bad .nc file should raise file validation error
        files = [UploadedFile(file=self.netcdf_bad_file_obj, name=self.netcdf_bad_file_name)]
        with self.assertRaises(utils.ResourceFileValidationException):
            utils.resource_file_add_pre_process(resource=self.resNetcdf, files=files,
                                                user=self.user,
                                                extract_metadata=False)

        # trying to add valid .nc file should pass the file check
        files = [UploadedFile(file=self.netcdf_file_obj, name=self.netcdf_file_name)]
        utils.resource_file_add_pre_process(resource=self.resNetcdf, files=files, user=self.user,
                                            extract_metadata=False)
        utils.resource_file_add_process(resource=self.resNetcdf, files=files, user=self.user,
                                        extract_metadata=False)

        # there should be 2 content file: with ncdump file created by system
        self.assertEqual(self.resNetcdf.files.all().count(), 2)

        # file pre add process should raise validation error if we try to add a 2nd file
        # when the resource has already 2 content files

        with self.assertRaises(utils.ResourceFileValidationException):
            utils.resource_file_add_pre_process(resource=self.resNetcdf, files=files,
                                                user=self.user,
                                                extract_metadata=False)

    def test_metadata_extraction_on_resource_creation(self):
        # passing the file object that points to the temp dir doesn't work - create_resource
        # throws error open the file from the fixed file location
        self._create_netcdf_resource()
        super(TestNetcdfMetaData, self).netcdf_metadata_extraction()

    def test_metadata_extraction_on_content_file_add(self):
        # test the core metadata at this point
        self.assertEqual(
            self.resNetcdf.metadata.title.value,
            'Snow water equivalent estimation at TWDEF site from Oct 2009 to June 2010')

        # there shouldn't any abstract element
        self.assertEqual(self.resNetcdf.metadata.description, None)

        # there shouldn't any coverage element
        self.assertEqual(self.resNetcdf.metadata.coverages.all().count(), 0)

        # there shouldn't any format element
        self.assertEqual(self.resNetcdf.metadata.formats.all().count(), 0)

        # there shouldn't any subject element
        self.assertEqual(self.resNetcdf.metadata.subjects.all().count(), 0)

        # there shouldn't any contributor element
        self.assertEqual(self.resNetcdf.metadata.contributors.all().count(), 0)

        # there shouldn't any source element
        self.assertEqual(self.resNetcdf.metadata.sources.all().count(), 0)

        # there shouldn't any relation element
        self.assertEqual(self.resNetcdf.metadata.relations.all().filter(type='cites').count(), 0)

        # there should be 1 creator
        self.assertEqual(self.resNetcdf.metadata.creators.all().count(), 1)

        # there shouldn't any extended metadata
        self.assertEqual(self.resNetcdf.metadata.ori_coverage.all().count(), 0)
        self.assertEqual(self.resNetcdf.metadata.variables.all().count(), 0)

        # adding a valid netcdf file should generate some core metadata and all extended metadata
        files = [UploadedFile(file=self.netcdf_file_obj, name=self.netcdf_file_name)]
        utils.resource_file_add_pre_process(resource=self.resNetcdf, files=files, user=self.user,
                                            extract_metadata=False)
        utils.resource_file_add_process(resource=self.resNetcdf, files=files, user=self.user,
                                        extract_metadata=False)

        super(TestNetcdfMetaData, self).netcdf_metadata_extraction(expected_creators_count=2)

    def test_metadata_on_content_file_delete(self):
        # test that some of the metadata is not deleted on content file deletion
        # adding a valid netcdf file should generate some core metadata and all extended metadata
        files = [UploadedFile(file=self.netcdf_file_obj, name=self.netcdf_file_name)]
        utils.resource_file_add_pre_process(resource=self.resNetcdf, files=files, user=self.user,
                                            extract_metadata=False)

        utils.resource_file_add_process(resource=self.resNetcdf, files=files, user=self.user,
                                        extract_metadata=True)

        # there should be 1 content files
        self.assertEqual(self.resNetcdf.files.all().count(), 2)

        # there should be 1 format elements
        self.assertEqual(self.resNetcdf.metadata.formats.all().count(), 2)

        # delete content file that we added above
        hydroshare.delete_resource_file(self.resNetcdf.short_id, self.netcdf_file_name, self.user)

        # there should no content file
        self.assertEqual(self.resNetcdf.files.all().count(), 0)

        # there should be a title element
        self.assertNotEquals(self.resNetcdf.metadata.title, None)

        # there should be abstract element
        self.assertNotEquals(self.resNetcdf.metadata.description, None)

        # there should be 2 creator element
        self.assertEqual(self.resNetcdf.metadata.creators.all().count(), 2)

        # there should be 1 contributor element
        self.assertEqual(self.resNetcdf.metadata.contributors.all().count(), 1)

        # there should be no coverage element
        self.assertEqual(self.resNetcdf.metadata.coverages.all().count(), 0)

        # there should be no format element
        self.assertEqual(self.resNetcdf.metadata.formats.all().count(), 0)

        # there should be subject element
        self.assertNotEquals(self.resNetcdf.metadata.subjects.all().count(), 0)

        # testing extended metadata elements
        self.assertEqual(self.resNetcdf.metadata.ori_coverage.all().count(), 0)
        self.assertEqual(self.resNetcdf.metadata.variables.all().count(), 0)

    def test_metadata_delete_on_resource_delete(self):
        # adding a valid netcdf file should generate some core metadata and all extended metadata
        files = [UploadedFile(file=self.netcdf_file_obj, name=self.netcdf_file_name)]
        utils.resource_file_add_pre_process(resource=self.resNetcdf, files=files, user=self.user,
                                            extract_metadata=False)

        utils.resource_file_add_process(resource=self.resNetcdf, files=files, user=self.user,
                                        extract_metadata=True)

        # before resource delete
        # resource core metadata
        core_metadata_obj = self.resNetcdf.metadata
        self.assertEqual(CoreMetaData.objects.all().count(), 1)
        # there should be Creator metadata objects
        self.assertTrue(Creator.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Contributor metadata objects
        self.assertTrue(Contributor.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Identifier metadata objects
        self.assertTrue(Identifier.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Type metadata objects
        self.assertTrue(Type.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Source metadata objects
        self.assertTrue(Source.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Relation metadata objects
        self.assertTrue(Relation.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Publisher metadata objects
        self.assertFalse(Publisher.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Title metadata objects
        self.assertTrue(Title.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Description (Abstract) metadata objects
        self.assertTrue(Description.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Date metadata objects
        self.assertTrue(Date.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Subject metadata objects
        self.assertTrue(Subject.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Coverage metadata objects
        self.assertTrue(Coverage.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Format metadata objects
        self.assertTrue(Format.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Language metadata objects
        self.assertTrue(Language.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Rights metadata objects
        self.assertTrue(Rights.objects.filter(object_id=core_metadata_obj.id).exists())

        # resource specific metadata
        # there should be original coverage metadata objects
        self.assertTrue(OriginalCoverage.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Variable metadata objects
        self.assertTrue(Variable.objects.filter(object_id=core_metadata_obj.id).exists())

        # delete resource
        hydroshare.delete_resource(self.resNetcdf.short_id)
        self.assertEqual(CoreMetaData.objects.all().count(), 0)

        # there should be no Creator metadata objects
        self.assertFalse(Creator.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Contributor metadata objects
        self.assertFalse(Contributor.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Identifier metadata objects
        self.assertFalse(Identifier.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Type metadata objects
        self.assertFalse(Type.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Source metadata objects
        self.assertFalse(Source.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Relation metadata objects
        self.assertFalse(Relation.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Publisher metadata objects
        self.assertFalse(Publisher.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Title metadata objects
        self.assertFalse(Title.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Description (Abstract) metadata objects
        self.assertFalse(Description.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Date metadata objects
        self.assertFalse(Date.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Subject metadata objects
        self.assertFalse(Subject.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Coverage metadata objects
        self.assertFalse(Coverage.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Format metadata objects
        self.assertFalse(Format.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Language metadata objects
        self.assertFalse(Language.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be no Rights metadata objects
        self.assertFalse(Rights.objects.filter(object_id=core_metadata_obj.id).exists())

        # resource specific metadata
        # there should be original coverage metadata objects
        self.assertFalse(OriginalCoverage.objects.filter(object_id=core_metadata_obj.id).exists())
        # there should be Variable metadata objects
        self.assertFalse(Variable.objects.filter(object_id=core_metadata_obj.id).exists())

    def test_extended_metadata_CRUD(self):
        # create original coverage element
        self.assertEqual(self.resNetcdf.metadata.ori_coverage.all().count(), 0)
        value = {"northlimit": '12', "projection": "transverse_mercator",
                 "units": "meter", "southlimit": '10',
                 "eastlimit": '23', "westlimit": '2'}
        self.resNetcdf.metadata.create_element(
            'originalcoverage',
            value=value,
            projection_string_text='+proj=tmerc +lon_0=-111.0 +lat_0=0.0 +x_0=500000.0 '
                                   '+y_0=0.0 +k_0=0.9996',
            projection_string_type='Proj4 String'
            )
        ori_coverage = self.resNetcdf.metadata.ori_coverage.all().first()
        self.assertEqual(ori_coverage.value, value)
        self.assertEqual(ori_coverage.projection_string_text,
                         '+proj=tmerc +lon_0=-111.0 +lat_0=0.0 +x_0=500000.0 +y_0=0.0 +k_0=0.9996')
        self.assertEqual(ori_coverage.projection_string_type, 'Proj4 String')

        # multiple original coverage elements are not allowed - should raise exception
        with self.assertRaises(IntegrityError):
            self.resNetcdf.metadata.create_element(
                'originalcoverage',
                value=value,
                projection_string_text='+proj=tmerc +lon_0=-111.0 '
                                       '+lat_0=0.0 +x_0=500000.0 +y_0=0.0 +k_0=0.9996',
                projection_string_type='Proj4 String')

        # create variable element
        self.assertEqual(self.resNetcdf.metadata.variables.all().count(), 0)
        self.resNetcdf.metadata.create_element('variable', name='SWE', type='Float',
                                               shape='y,x,time', unit='m',
                                               missing_value='-9999',
                                               descriptive_name='Snow water equivalent',
                                               method='model simulation of UEB')

        variable_element = self.resNetcdf.metadata.variables.all().first()
        self.assertEqual(variable_element.name, 'SWE')
        self.assertEqual(variable_element.type, 'Float')
        self.assertEqual(variable_element.shape, 'y,x,time')
        self.assertEqual(variable_element.unit, 'm')
        self.assertEqual(variable_element.missing_value, '-9999')
        self.assertEqual(variable_element.descriptive_name, 'Snow water equivalent')
        self.assertEqual(variable_element.method, 'model simulation of UEB')

        # multiple variable elements are allowed
        self.resNetcdf.metadata.create_element('variable', name='x', type='Float',
                                               shape='x', unit='m',
                                               descriptive_name='x coordinate of projection')
        self.assertEqual(self.resNetcdf.metadata.variables.all().count(), 2)

        # delete
        # variable deletion is not allowed
        with self.assertRaises(ValidationError):
            self.resNetcdf.metadata.delete_element(
                'variable', self.resNetcdf.metadata.variables.all().filter(name='SWE').first().id)

        # update
        # update original coverage element
        value_2 = {"northlimit": '12.5', "projection": "transverse_mercator",
                   "units": "meter", "southlimit": '10.5',
                   "eastlimit": '23.5', "westlimit": '2.5'}
        self.resNetcdf.metadata.update_element('originalcoverage',
                                               ori_coverage.id,
                                               value=value_2,
                                               projection_string_text='2009',
                                               projection_string_type='EPSG Code'
                                               )
        ori_coverage = self.resNetcdf.metadata.ori_coverage.all().first()
        self.assertEqual(ori_coverage.value, value_2)
        self.assertEqual(ori_coverage.projection_string_text, '2009')
        self.assertEqual(ori_coverage.projection_string_type, 'EPSG Code')

        # update variable element
        variable = self.resNetcdf.metadata.variables.all().filter(name='SWE').first()
        self.resNetcdf.metadata.update_element('variable', variable.id,
                                               name='SWE2', type='Double',
                                               shape='y,x,time', unit='m',
                                               missing_value='-999',
                                               descriptive_name='snow water equivalent',
                                               method='model result of UEB')
        # need to refer to the variable again!
        variable = self.resNetcdf.metadata.variables.all().filter(name='SWE2').first()
        self.assertEqual(variable.name, 'SWE2')
        self.assertEqual(variable.type, 'Double')
        self.assertEqual(variable.shape, 'y,x,time')
        self.assertEqual(variable.unit, 'm')
        self.assertEqual(variable.missing_value, '-999')
        self.assertEqual(variable.descriptive_name, 'snow water equivalent')
        self.assertEqual(variable.method, 'model result of UEB')

    def test_have_multiple_content_files(self):
        self.assertFalse(NetcdfResource.can_have_multiple_files())

    def test_can_upload_multiple_content_files(self):
        # only one file can be uploaded
        self.assertFalse(NetcdfResource.allow_multiple_file_upload())

    def test_public_or_discoverable(self):
        self.assertFalse(self.resNetcdf.has_required_content_files())
        self.assertFalse(self.resNetcdf.metadata.has_all_required_elements())
        self.assertFalse(self.resNetcdf.can_be_public_or_discoverable)

        # adding a valid netcdf file
        files = [UploadedFile(file=self.netcdf_file_obj, name=self.netcdf_file_name)]
        utils.resource_file_add_pre_process(resource=self.resNetcdf, files=files, user=self.user,
                                            extract_metadata=False)

        utils.resource_file_add_process(resource=self.resNetcdf, files=files, user=self.user,
                                        extract_metadata=True)

        self.assertTrue(self.resNetcdf.has_required_content_files())
        self.assertTrue(self.resNetcdf.metadata.has_all_required_elements())
        self.assertTrue(self.resNetcdf.can_be_public_or_discoverable)

    def test_metadata_extraction_of_wkt_crs_on_resource_creation(self):
        files = [UploadedFile(file=self.netcdf_file_obj_crs, name=self.netcdf_file_name_crs)]
        _, _, metadata, _ = utils.resource_pre_create_actions(
            resource_type='NetcdfResource',
            resource_title='Snow water equivalent estimation at TWDEF site '
                           'from Oct 2009 to June 2010',
            page_redirect_url_key=None,
            files=files,
            metadata=None,)

        self.resNetcdf = hydroshare.create_resource(
            'NetcdfResource',
            self.user,
            'Snow water equivalent estimation at TWDEF site from Oct 2009 to June 2010',
            files=files,
            metadata=metadata
            )

        utils.resource_post_create_actions(self.resNetcdf, self.user, metadata)

        self._test_metadata_extraction_wkt_crs()

    def test_metadata_extraction_of_wkt_crs_on_content_file_add(self):
        files = [UploadedFile(file=self.netcdf_file_obj_crs, name=self.netcdf_file_name_crs)]
        utils.resource_file_add_pre_process(resource=self.resNetcdf, files=files, user=self.user,
                                            extract_metadata=False)
        utils.resource_file_add_process(resource=self.resNetcdf, files=files, user=self.user,
                                        extract_metadata=False)

        self._test_metadata_extraction_wkt_crs()

    def test_bulk_metadata_update(self):
        # here we are testing the update() method of the NetcdfMetaData class

        # update of resource specific metadata should fail when the resource does not have content
        # files
        self.assertEqual(self.resNetcdf.files.all().count(), 0)
        value = {"northlimit": '12', "projection": "transverse_mercator",
                 "units": "meter", "southlimit": '10',
                 "eastlimit": '23', "westlimit": '2'}
        metadata = []
        metadata.append({'originalcoverage': {'value': value}})
        with self.assertRaises(ValidationError):
            self.resNetcdf.metadata.update(metadata, self.user)

        del metadata[:]
        metadata.append({'variable': {'name': 'SWE', 'type': 'Float',
                                      'shape': 'y,x,time', 'unit': 'm',
                                      'missing_value': '-9999',
                                      'descriptive_name': 'Snow water equivalent',
                                      'method': 'model simulation of UEB'
                                      }
                         })
        with self.assertRaises(ValidationError):
            self.resNetcdf.metadata.update(metadata, self.user)
        self.resNetcdf.delete()
        # create netcdf resource with uploaded valid netcdf file
        self._create_netcdf_resource()
        # there should be 2 files in the resource
        self.assertEqual(self.resNetcdf.files.all().count(), 2)
        self.assertNotEqual(self.resNetcdf.metadata.originalCoverage, None)
        self.assertEqual(self.resNetcdf.metadata.originalCoverage.value['northlimit'],
                         '4.63515e+06')

        # projection should be ignored by the update
        value = {"northlimit": '12', "projection": "transverse_mercator-new",
                 "units": "meter", "southlimit": '10',
                 "eastlimit": '23', "westlimit": '2'}

        del metadata[:]
        metadata.append({'originalcoverage': {'value': value}})
        self.resNetcdf.metadata.update(metadata, self.user)
        self.assertNotEqual(self.resNetcdf.metadata.originalCoverage, None)
        self.assertEqual(self.resNetcdf.metadata.originalCoverage.value['northlimit'], '12')
        self.assertNotEqual(self.resNetcdf.metadata.originalCoverage.value['projection'],
                            "transverse_mercator-new")
        # 'datum', 'projection_string_type', 'projection_string_text' should be ignored
        value = {"northlimit": '15', "projection": "transverse_mercator-new",
                 "units": "meter", "southlimit": '10',
                 "eastlimit": '23', "westlimit": '2'}
        del metadata[:]
        metadata.append({'originalcoverage': {'value': value, 'datum': 'some datum',
                                              'projection_string_type': 'some proj string type',
                                              'projection_string_text': 'some prog string text'
                                              }
                         })

        self.resNetcdf.metadata.update(metadata, self.user)
        self.assertNotEqual(self.resNetcdf.metadata.originalCoverage, None)
        self.assertEqual(self.resNetcdf.metadata.originalCoverage.value['northlimit'], '15')
        self.assertNotEqual(self.resNetcdf.metadata.originalCoverage.value['projection'],
                            "transverse_mercator-new")
        self.assertNotEqual(self.resNetcdf.metadata.originalCoverage.datum, 'some datum')
        self.assertNotEqual(self.resNetcdf.metadata.originalCoverage.projection_string_type,
                            'some proj string type')
        self.assertNotEqual(self.resNetcdf.metadata.originalCoverage.projection_string_text,
                            'some proj string text')

        # test updating variable data
        self.assertEqual(self.resNetcdf.metadata.variables.count(), 5)
        # there should one variable with name SWE
        self.assertEqual(self.resNetcdf.metadata.variables.filter(name='SWE').count(), 1)
        swe_variable = self.resNetcdf.metadata.variables.filter(name='SWE').first()
        self.assertEqual(swe_variable.unit, 'm')
        # test that we can update the SWE variable by changing the unit to feet
        del metadata[:]
        metadata.append({'variable': {'name': 'SWE', 'type': 'Float',
                                      'shape': 'y,x,time', 'unit': 'feet',
                                      'missing_value': '-9999',
                                      'descriptive_name': 'Snow water equivalent',
                                      'method': 'model simulation of UEB'
                                      }
                         })

        self.resNetcdf.metadata.update(metadata, self.user)
        swe_variable = self.resNetcdf.metadata.variables.filter(name='SWE').first()
        self.assertEqual(swe_variable.unit, 'feet')
        # test that update of variable should fail if update is made for non-existing variable
        # (SWE1)
        del metadata[:]
        metadata.append({'variable': {'name': 'SWE1', 'type': 'Float',
                                      'shape': 'y,x,time', 'unit': 'm',
                                      'missing_value': '-9999',
                                      'descriptive_name': 'Snow water equivalent',
                                      'method': 'model simulation of UEB'
                                      }
                         })

        with self.assertRaises(ValidationError):
            self.resNetcdf.metadata.update(metadata, self.user)

        # there should no variable with name SWE1
        self.assertEqual(self.resNetcdf.metadata.variables.filter(name='SWE1').count(), 0)
        swe_variable = self.resNetcdf.metadata.variables.filter(name='SWE').first()
        self.assertEqual(swe_variable.unit, 'feet')

        # test updating multiple variables
        x_variable = self.resNetcdf.metadata.variables.filter(name='x').first()
        self.assertEqual(x_variable.unit, 'Meter')
        del metadata[:]
        metadata.append({'variable': {'name': 'SWE',
                                      'unit': 'm',
                                      'missing_value': '-9999',
                                      'descriptive_name': 'Snow water equivalent',
                                      'method': 'model simulation of UEB'
                                      }
                         })
        metadata.append({'variable': {'name': 'x', 'unit': 'Feet'}})

        self.resNetcdf.metadata.update(metadata, self.user)
        swe_variable = self.resNetcdf.metadata.variables.filter(name='SWE').first()
        self.assertEqual(swe_variable.unit, 'm')
        x_variable = self.resNetcdf.metadata.variables.filter(name='x').first()
        self.assertEqual(x_variable.unit, 'Feet')
        # test that is is not possible update variable attributes: type and shape
        x_variable = self.resNetcdf.metadata.variables.filter(name='x').first()
        self.assertEqual(x_variable.type, 'Float')
        self.assertEqual(x_variable.shape, 'x')

        del metadata[:]
        metadata.append({'variable': {'name': 'x', 'type': 'Double', 'shape': 'y'}})
        self.resNetcdf.metadata.update(metadata, self.user)
        x_variable = self.resNetcdf.metadata.variables.filter(name='x').first()
        self.assertEqual(x_variable.type, 'Float')
        self.assertEqual(x_variable.shape, 'x')

        # test updating core metadata with resource specific metadata

        # there should be 1 creator
        self.assertEqual(self.resNetcdf.metadata.creators.all().count(), 1)
        # there should be 1 contributor
        self.assertEqual(self.resNetcdf.metadata.contributors.all().count(), 1)
        del metadata[:]
        metadata.append({'creator': {'name': 'creator one'}})
        metadata.append({'creator': {'name': 'creator two'}})
        metadata.append({'contributor': {'name': 'contributor one'}})
        metadata.append({'contributor': {'name': 'contributor two'}})
        metadata.append({'variable': {'name': 'x', 'unit': 'Meter'}})
        self.resNetcdf.metadata.update(metadata, self.user)
        x_variable = self.resNetcdf.metadata.variables.filter(name='x').first()
        self.assertEqual(x_variable.unit, 'Meter')
        # there should be 2 creators (the previous creator gets deleted as part of the update)
        self.assertEqual(self.resNetcdf.metadata.creators.all().count(), 2)
        # there should be 2 contributors (the previous contributor gets deleted as part
        # of the update)
        self.assertEqual(self.resNetcdf.metadata.contributors.all().count(), 2)
        self.resNetcdf.delete()

    def _create_netcdf_resource(self):
        files = [UploadedFile(file=self.netcdf_file_obj, name=self.netcdf_file_name)]
        _, _, metadata, _ = utils.resource_pre_create_actions(
            resource_type='NetcdfResource',
            resource_title='Snow water equivalent estimation at TWDEF site '
                           'from Oct 2009 to June 2010',
            page_redirect_url_key=None,
            files=files,
            metadata=None)

        self.resNetcdf = hydroshare.create_resource(
            'NetcdfResource',
            self.user,
            'Snow water equivalent estimation at TWDEF site from Oct 2009 to June 2010',
            files=files,
            metadata=metadata
        )

        utils.resource_post_create_actions(self.resNetcdf, self.user, metadata)

    def _test_metadata_extraction_wkt_crs(self, create_res_mode=True):

        # testing extended metadata element: original coverage
        ori_coverage = self.resNetcdf.metadata.ori_coverage.all().first()
        self.assertNotEquals(ori_coverage, None)
        self.assertEqual(ori_coverage.value['northlimit'], '4662377.44692')
        self.assertEqual(ori_coverage.value['eastlimit'], '461939.019091')
        self.assertEqual(ori_coverage.value['southlimit'], '4612607.44692')
        self.assertEqual(ori_coverage.value['westlimit'], '432419.019091')
        self.assertEqual(ori_coverage.value['units'], 'Meter')
        self.assertEqual(ori_coverage.value['projection'], 'NAD83 / UTM zone 12N')

        self.assertEqual(ori_coverage.projection_string_type, 'WKT String')
        proj_text = 'PROJCS["NAD83 / UTM zone 12N",GEOGCS["NAD83",' \
                    'DATUM["North_American_Datum_1983",' \
                    'SPHEROID["GRS 1980",6378137,298.2572221010002,AUTHORITY["EPSG","7019"]],' \
                    'AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0],' \
                    'UNIT["degree",0.0174532925199433],' \
                    'AUTHORITY["EPSG","4269"]],PROJECTION["Transverse_Mercator"],' \
                    'PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-111],' \
                    'PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],' \
                    'PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],' \
                    'AUTHORITY["EPSG","26912"]]'
        self.assertEqual(ori_coverage.projection_string_text, proj_text)
        self.assertEqual(ori_coverage.datum, 'North_American_Datum_1983')


text:
package ch.epfl.data.squall.api.scala.queries

import ch.epfl.data.squall.api.scala.SquallType._
import ch.epfl.data.squall.api.scala.Stream._
import ch.epfl.data.squall.query_plans.QueryBuilder
import ch.epfl.data.squall.query_plans.QueryPlan
import ch.epfl.data.squall.api.scala._
import ch.epfl.data.squall.api.scala.TPCHSchema._

/**
 * @author mohamed
 * Hyracks Query
 *
 * SELECT C_MKTSEGMENT, COUNT(O_ORDERKEY)
 * FROM CUSTOMER join ORDERS on C_CUSTKEY = O_CUSTKEY
 * GROUP BY C_MKTSEGMENT
 */
class ScalaHyracksPlan(dataPath: String, extension: String, conf: java.util.Map[String, Object]) extends QueryPlan {

  override def getQueryPlan(): QueryBuilder = {
    val customers = Source[Customer]("customer").map { t => (t.custkey, t.mktsegment) }
    val orders = Source[Orders]("orders").map { _.custkey }
    val join = (customers join orders)(_._1)(x => x)
    val agg = join.groupByKey(x => 1, _._1._2)
    agg.execute(conf)
  }

}

redpajama_set_name: RedPajamaGithub


text:
namespace mojo {
namespace system {
namespace {

TEST(ChannelEndpointIdTest, Basic) {
  ChannelEndpointId invalid;
  ChannelEndpointId bootstrap(ChannelEndpointId::GetBootstrap());

  EXPECT_EQ(invalid, invalid);
  EXPECT_EQ(bootstrap, bootstrap);
  EXPECT_FALSE(invalid == bootstrap);

  EXPECT_FALSE(invalid != invalid);
  EXPECT_FALSE(bootstrap != bootstrap);
  EXPECT_NE(invalid, bootstrap);

  EXPECT_FALSE(invalid < invalid);
  EXPECT_LT(invalid, bootstrap);

  EXPECT_FALSE(invalid.is_valid());
  EXPECT_TRUE(bootstrap.is_valid());

  EXPECT_FALSE(invalid.is_remote());
  EXPECT_FALSE(bootstrap.is_remote());

  // Test assignment.
  ChannelEndpointId copy;
  copy = bootstrap;
  EXPECT_EQ(copy, bootstrap);
  copy = invalid;
  EXPECT_EQ(copy, invalid);
}

// Tests values of invalid and bootstrap IDs. (This tests implementation
// details.)
TEST(ChannelEndpointIdTest, Value) {
  EXPECT_EQ(0u, ChannelEndpointId().value());
  EXPECT_EQ(1u, ChannelEndpointId::GetBootstrap().value());
}

// Tests ostream output. (This tests implementation details.)
TEST(ChannelEndpointIdTest, Ostream) {
  {
    std::ostringstream stream;
    stream << ChannelEndpointId();
    EXPECT_EQ("0", stream.str());
  }
  {
    std::ostringstream stream;
    stream << ChannelEndpointId::GetBootstrap();
    EXPECT_EQ("1", stream.str());
  }
}

TEST(LocalChannelEndpointIdGeneratorTest, Basic) {
  LocalChannelEndpointIdGenerator gen;

  ChannelEndpointId id1;
  EXPECT_FALSE(id1.is_valid());  // Check sanity.

  id1 = gen.GetNext();
  EXPECT_TRUE(id1.is_valid());
  EXPECT_FALSE(id1.is_remote());

  EXPECT_EQ(ChannelEndpointId::GetBootstrap().value(), id1.value());

  ChannelEndpointId id2 = gen.GetNext();
  EXPECT_TRUE(id2.is_valid());
  EXPECT_FALSE(id2.is_remote());
  // Technically, nonequality here is an implementation detail, since, e.g.,
  // random generation of IDs would be a valid implementation.
  EXPECT_NE(id2, id1);
  // ... but right now we just increment to generate IDs.
  EXPECT_EQ(2u, id2.value());
}

// Note: LocalChannelEndpointIdGeneratorTest.WrapAround is defined further
// below, outside the anonymous namespace.

TEST(RemoteChannelEndpointIdGeneratorTest, Basic) {
  RemoteChannelEndpointIdGenerator gen;

  ChannelEndpointId id1;
  EXPECT_FALSE(id1.is_valid());  // Check sanity.

  id1 = gen.GetNext();
  EXPECT_TRUE(id1.is_valid());
  EXPECT_TRUE(id1.is_remote());

  // This tests an implementation detail.
  EXPECT_EQ(ChannelEndpointId::kRemoteFlag, id1.value());

  ChannelEndpointId id2 = gen.GetNext();
  EXPECT_TRUE(id2.is_valid());
  EXPECT_TRUE(id2.is_remote());
  // Technically, nonequality here is an implementation detail, since, e.g.,
  // random generation of IDs would be a valid implementation.
  EXPECT_NE(id2, id1);
  // ... but right now we just increment to generate IDs.
  EXPECT_EQ(ChannelEndpointId::kRemoteFlag + 1, id2.value());
}

// Note: RemoteChannelEndpointIdGeneratorTest.WrapAround is defined further
// below, outside the anonymous namespace.

}  // namespace

// Tests that |LocalChannelEndpointIdGenerator| handles wrap-around correctly.
// (This tests implementation details.) This test isn't in an anonymous
// namespace, since it needs to be friended.
TEST(LocalChannelEndpointIdGeneratorTest, WrapAround) {
  LocalChannelEndpointIdGenerator gen;
  gen.next_ = ChannelEndpointId(ChannelEndpointId::kRemoteFlag - 1);

  ChannelEndpointId id = gen.GetNext();
  EXPECT_TRUE(id.is_valid());
  EXPECT_FALSE(id.is_remote());
  EXPECT_EQ(ChannelEndpointId::kRemoteFlag - 1, id.value());

  id = gen.GetNext();
  EXPECT_TRUE(id.is_valid());
  EXPECT_FALSE(id.is_remote());
  EXPECT_EQ(1u, id.value());
}

// Tests that |RemoteChannelEndpointIdGenerator| handles wrap-around correctly.
// (This tests implementation details.) This test isn't in an anonymous
// namespace, since it needs to be friended.
TEST(RemoteChannelEndpointIdGeneratorTest, WrapAround) {
  RemoteChannelEndpointIdGenerator gen;
  gen.next_ = ChannelEndpointId(~0u);

  ChannelEndpointId id = gen.GetNext();
  EXPECT_TRUE(id.is_valid());
  EXPECT_TRUE(id.is_remote());
  EXPECT_EQ(~0u, id.value());

  id = gen.GetNext();
  EXPECT_TRUE(id.is_valid());
  EXPECT_TRUE(id.is_remote());
  EXPECT_EQ(ChannelEndpointId::kRemoteFlag, id.value());
}

}  // namespace system
}  // namespace mojo


text:
layout: page
title: O projektu
---

 **Vítejte na stránkách KniHry**

Naším cílem je pomocí různých kvízů, hříček, hádanek a procházek po Brně probudit zájem středoškoláků o čtení povinné literatury a usnadnit jim tak závěrečné projití maturitou. Všechny tři ji už máme za sebou, takže si dovedeme živě představit, jak se do toho žákům chce a kolik na to mají času. Známe to, když si průměrný středoškolák přečte aspoň polovinu knih, může to učitel brát jako úspěch. I když to ten student dělá pro sebe. Jenže do toho, co je povinné se málokomu chce dobrovolně. My bychom jim rády usnadnily a kdo ví, třeba tak v nich i probudili lásku ke knihám a čtení obecně.

Projekt funguje na několika provázaných pracovních listech, které tu budeme postupně zveřejňovat, tak aby bylo dostatek času na jeho vypracování.

Pokud chcete uspět, jistě vám pomůže, když si projdete několik následujících odkazů a knih:
<http://www.databazeknih.cz/> - asi nejznámější a nejpoužívanější databáze knih, kde je možné najít snad skoro všechno

<http://www.cbdb.cz/> - pokud preferujete jinou, zde je ta druhá, asi nejznámější

<http://www.citarny.cz/> - kromě jiného se zde dá najít i mnoho z aktuálního dění, ocenění, nebo o autorech, to se bude hodit

<http://www.iliteratura.cz/> - články, knihy, recenze nebo o obálka, to vše se může hodit

<https://www.knihovny.cz/> - portál snažící se sjednotit všechny knihovny, může se hodit, když si budete potřeovat danou knihu vypůjčit a máte přístup k více než jedné knihovně

<http://www.pruvodcebrnem.cz/> - jelikož se tam budou objevovat i nějaké odkazy na Brno, je potřeba vědět, kam se podívat.

<http://encyklopedie.brna.cz/home-mmb/> - další encyklopedie na Brno, čím lépe ho budete znát, tím lépe pro vás

A z newebových zdrojů vybíráme například tyto, můžou Vám pomoci rozvinout kreativitu či tvůrčí myšlení, což se pro KniHru určitě hodí!

HIRTLOVÁ, Petra. *Kreativní mysl.* Kolín: Nezávislé centrum pro studium politiky, o.s., 2013. Monografie. ISBN 978-80-86879-40-6.

ŽÁK, Petr. *Kreativita a její rozvoj.* Brno: Computer Press, 2004. Business books. ISBN 80-251-0457-5.

PELÁNEK, Radek. *Hlavolamikon: [sbírka hlavolamů, hádanek, šifer a logických úloh].* Brno: Computer Press, 2014. ISBN 978-80-251-4303-2.
PELÁNEK, Radek. *Jak to vyřešit?: logické úlohy a hry.* Praha: Portál, 2011. ISBN 978-80-7367-872-2.

PHILLIPS, Charles. *Logické myšlení: 50 cvičení pro rozvoj logického myšlení.* Praha: Grada, 2012. Trénink myšlení. ISBN 978-80-247-4510-7.
*Velká kniha hádanek.* Praha: Plot, 2010. ISBN 978-80-7428-037-5.

KÖNIGOVÁ, Marie. *Jak myslet kreativně*. Praha: Grada, 2006. Psychologie pro každého. ISBN 80-247-1626-7.

GRANT, Andrew, Gaia GRANT a Jason GALLATE. *Who killed creativity?: --and how can we get it back?* : seven essential strategies to make yourself, your team and your organisation more innovative. Milton: John Wiley, 2012. ISBN 978-1-118-23252-1.

Jelikož to ale není jen o knížkách, ale také o hraní, můžete se podívat i na toto

<https://www.youtube.com/watch?v=5DnjWX2QGcA> aneb, co všechno se dá dělat s knihou. **Upozornění 9 z 10 knihovníků doporučuje knihu použít standardním způsobem.**
A hlavně přitom nezapomeňte správně sedět! <https://pbs.twimg.com/media/BdPom4JIIAATDG-.jpg>

Jelikož je stránka zajíme ještě v plenkách, chápeme, že není úplně na oko nejlepší, nicméně věřte, že zlepšení se blíží. Časem zde bude pozadí příjemné barvy a knižní police, kam si imaginárně můžete odkládat své knižní úspěchy.

A kým bychom se chtěli inspirovat? Takových webů je mnoho!
Jednou z inspirací může být například stránka Čtenářské výzvy, v jednoduchosti je totiž krása! A síla!
<http://www.ctenarskavyzva.cz>
A navíc jde o mladé, takže sociální média budou taky moc důležitá:
<https://www.instagram.com/knihovnanakrizovatce/?hl=cs>

**A co tu bude?**
Ze začátku zveřejníme hlavní seznamy literatury, které jsme vybraly, později první pracovní listy. Určitě se je na co těsit! :)


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

package com.google.security.zynamics.binnavi.Gui.FilterPanel.FilterExpressions.AbstractTree;

import java.util.ArrayList;
import java.util.List;

/**
 * Represents an OR expression in the filter tree.
 */
public final class CAbstractOrExpression implements IAbstractNode {
  /**
   * The children of the OR expression.
   */
  private final List<IAbstractNode> m_children;

  /**
   * Creates a new OR expression.
   *
   * @param children The children of the OR expression.
   */
  public CAbstractOrExpression(final List<IAbstractNode> children) {
    m_children = new ArrayList<IAbstractNode>(children);
  }

  /**
   * Returns the children of the OR expression.
   *
   * @return The children of the OR expression.
   */
  public List<IAbstractNode> getChildren() {
    return new ArrayList<IAbstractNode>(m_children);
  }
}


text:
package org.jetbrains.idea.maven.indices;

import com.intellij.ide.projectWizard.ProjectWizardTestCase;
import com.intellij.ide.util.newProjectWizard.AbstractProjectWizard;
import com.intellij.maven.testFramework.MavenTestCase;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.io.PathKt;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.concurrency.Promise;
import org.jetbrains.idea.maven.model.MavenExplicitProfiles;
import org.jetbrains.idea.maven.project.MavenProjectsManager;
import org.jetbrains.idea.maven.wizards.MavenProjectBuilder;
import org.jetbrains.idea.maven.wizards.MavenProjectImportProvider;

import java.nio.file.Path;
import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

public class MavenMultiProjectImportTest extends ProjectWizardTestCase<AbstractProjectWizard> {

  private Path myDir;

  public void testIndicesForDifferentProjectsShouldBeSameInstance() {
    myDir = getTempDir().newPath("", true);
    VirtualFile pom1 = createPomXml("projectDir1", "<groupId>test</groupId>" +
                                                   "<artifactId>project1</artifactId>" +
                                                   "<version>1</version>");
    importMaven(myProject, pom1);

    VirtualFile pom2 = createPomXml("projectDir2", "<groupId>test</groupId>" +
                                                   "<artifactId>project2</artifactId>" +
                                                   "<version>1</version>");

    MavenProjectImportProvider provider = new MavenProjectImportProvider();
    MavenProjectBuilder builder = (MavenProjectBuilder)provider.getBuilder();
    builder.setFileToImport(pom2);
    Module module = importProjectFrom(pom2.getPath(), null, provider);
    Project project2 = module.getProject();
    importMaven(project2, pom2);
    MavenIndicesManager.getInstance(project2).updateIndicesListSync();
    MavenIndicesManager.getInstance(myProject).updateIndicesListSync();

    MavenIndexHolder firstIndices = MavenIndicesManager.getInstance(myProject).getIndex();
    MavenIndexHolder secondIndices = MavenIndicesManager.getInstance(project2).getIndex();
    assertThat(firstIndices.getIndices()).hasSize(2);
    assertThat(secondIndices.getIndices()).hasSize(2);
    //    assertSame(firstIndices.getLocalIndex(), secondIndices.getLocalIndex());
    //   assertSame(firstIndices.getRemoteIndices().get(0), secondIndices.getRemoteIndices().get(0));
  }

  private VirtualFile createPomXml(String dir,
                                   @Language(value = "XML", prefix = "<project>", suffix = "</project>") String pomxml) {
    Path projectDir = myDir.resolve(dir);
    projectDir.toFile().mkdirs();
    Path pom = projectDir.resolve("pom.xml");
    PathKt.write(pom, MavenTestCase.createPomXml(pomxml));
    return LocalFileSystem.getInstance().refreshAndFindFileByNioFile(pom);
  }

  private void importMaven(@NotNull Project project, @NotNull VirtualFile file) {
    MavenProjectsManager manager = MavenProjectsManager.getInstance(project);
    manager.initForTests();
    manager.waitForImportCompletion();
    manager.resetManagedFilesAndProfilesInTests(Collections.singletonList(file), MavenExplicitProfiles.NONE);
    ApplicationManager.getApplication().invokeAndWait(() -> {
      manager.scheduleImportInTests(Collections.singletonList(file));
      manager.importProjects();
    });

    Promise<?> promise = manager.waitForImportCompletion();
    PlatformTestUtil.waitForPromise(promise);
  }
}


text:
package org.apache.crunch.lib;

import static org.apache.crunch.types.avro.Avros.*;
import static org.junit.Assert.assertEquals;

import java.io.Serializable;

import org.apache.crunch.MapFn;
import org.apache.crunch.PTable;
import org.apache.crunch.Pair;
import org.apache.crunch.Pipeline;
import org.apache.crunch.impl.mr.MRPipeline;
import org.apache.crunch.io.From;
import org.apache.crunch.test.CrunchTestSupport;
import org.junit.Test;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;

public class SecondarySortIT extends CrunchTestSupport implements Serializable {

  @Test
  public void testSecondarySort() throws Exception {
    Pipeline p = new MRPipeline(SecondarySortIT.class, tempDir.getDefaultConfiguration());
    String inputFile = tempDir.copyResourceFileName("secondary_sort_input.txt");

    PTable<String, Pair<Integer, Integer>> in = p.read(From.textFile(inputFile))
        .parallelDo(new MapFn<String, Pair<String, Pair<Integer, Integer>>>() {
          @Override
          public Pair<String, Pair<Integer, Integer>> map(String input) {
            String[] pieces = input.split(",");
            return Pair.of(pieces[0],
                Pair.of(Integer.valueOf(pieces[1].trim()), Integer.valueOf(pieces[2].trim())));
          }
        }, tableOf(strings(), pairs(ints(), ints())));
    Iterable<String> lines = SecondarySort.sortAndApply(in, new MapFn<Pair<String, Iterable<Pair<Integer, Integer>>>, String>() {
      @Override
      public String map(Pair<String, Iterable<Pair<Integer, Integer>>> input) {
        Joiner j = Joiner.on(',');
        return j.join(input.first(), j.join(input.second()));
      }
    }, strings()).materialize();
    assertEquals(ImmutableList.of("one,[-5,10],[1,1],[2,-3]", "three,[0,-1]", "two,[1,7],[2,6],[4,5]"),
        ImmutableList.copyOf(lines));
    p.done();
  }
}


text:
layout: news
title: Another Marie Curie Fellowship for the lab
date: 17-02-2015
excerpt: Dr. Irina Moreira from Porto University got a Marie Curie fellowship to join the CSB lab
tags: [HADDOCK, Utrecht University, Alexandre Bonvin, Docking]
image:
  feature:
---

Another [H2020 Marie Sklodowska-Curie Fellow](http://ec.europa.eu/programmes/horizon2020/en/h2020-section/marie-sklodowska-curie-actions) will join our lab later this year in the person of [Dr. Irina Moreira from the University of Porto in Portugal](http://www.fc.up.pt/pessoas/irina.moreira/blog/?page_id=80). This is the second such grant coming to our lab - [Anna Vangone](http://www.bonvinlab.org/news/Marie-Curie-fellowship-for-Anna/) got a similar fellowship in the same round.
With this two years grant, Irina will work on developing novel methods to model the structure of membrane proteins and their interactions, and apply them to G-Protein Coupled Receptors.

**Looking forward to have you in the team Irina!**


text:
package sun.security.provider;

import java.io.*;
import java.lang.reflect.*;
import java.net.URL;
import java.util.*;

import java.security.AccessController;
import java.security.CodeSource;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.Permission;
import java.security.Permissions;
import java.security.PermissionCollection;
import java.security.Principal;
import java.security.PrivilegedAction;
import java.security.UnresolvedPermission;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;

import javax.security.auth.Subject;
import javax.security.auth.PrivateCredentialPermission;

import sun.security.provider.PolicyParser.GrantEntry;
import sun.security.provider.PolicyParser.PermissionEntry;
import sun.security.provider.PolicyParser.PrincipalEntry;
import sun.security.util.Debug;
import sun.security.util.PolicyUtil;
import sun.security.util.PropertyExpander;

/**
 * See {@code com.sun.security.auth.PolicyFile} for the class description.
 * This class is necessary in order to support a default
 * {@code javax.security.auth.Policy} implementation on the compact1 and
 * compact2 profiles.
 *
 * @deprecated As of JDK 1.4, replaced by
 *             {@code sun.security.provider.PolicyFile}.
 *             This class is entirely deprecated.
 */
@Deprecated
public class AuthPolicyFile extends javax.security.auth.Policy {

    static final ResourceBundle rb =
        AccessController.doPrivileged(new PrivilegedAction<ResourceBundle>() {
            @Override public ResourceBundle run() {
                return (ResourceBundle.getBundle
                        ("sun.security.util.AuthResources"));
            }
        });

    private static final Debug debug = Debug.getInstance("policy",
                                                         "\t[Auth Policy]");

    private static final String AUTH_POLICY = "java.security.auth.policy";
    private static final String SECURITY_MANAGER = "java.security.manager";
    private static final String AUTH_POLICY_URL = "auth.policy.url.";

    private Vector<PolicyEntry> policyEntries;
    private Hashtable<Object, Object> aliasMapping;

    private boolean initialized = false;

    private boolean expandProperties = true;
    private boolean ignoreIdentityScope = true;

    // for use with the reflection API
    private static final Class<?>[] PARAMS = { String.class, String.class};

    /**
     * Initializes the Policy object and reads the default policy
     * configuration file(s) into the Policy object.
     */
    public AuthPolicyFile() {
        // initialize Policy if either the AUTH_POLICY or
        // SECURITY_MANAGER properties are set
        String prop = System.getProperty(AUTH_POLICY);

        if (prop == null) {
            prop = System.getProperty(SECURITY_MANAGER);
        }
        if (prop != null) {
            init();
        }
    }

    private synchronized void init() {
        if (initialized) {
            return;
        }

        policyEntries = new Vector<PolicyEntry>();
        aliasMapping = new Hashtable<Object, Object>(11);

        initPolicyFile();
        initialized = true;
    }

    @Override
    public synchronized void refresh() {

        java.lang.SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new javax.security.auth.AuthPermission
                                ("refreshPolicy"));
        }

        // XXX
        //
        // 1)   if code instantiates PolicyFile directly, then it will need
        //      all the permissions required for the PolicyFile initialization
        // 2)   if code calls Policy.getPolicy, then it simply needs
        //      AuthPermission(getPolicy), and the javax.security.auth.Policy
        //      implementation instantiates PolicyFile in a doPrivileged block
        // 3)   if after instantiating a Policy (either via #1 or #2),
        //      code calls refresh, it simply needs
        //      AuthPermission(refreshPolicy).  then PolicyFile wraps
        //      the refresh in a doPrivileged block.
        initialized = false;
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override public Void run() {
                init();
                return null;
            }
        });
    }

    private KeyStore initKeyStore(URL policyUrl, String keyStoreName,
                                  String keyStoreType) {
        if (keyStoreName != null) {
            try {
                /*
                 * location of keystore is specified as absolute URL in policy
                 * file, or is relative to URL of policy file
                 */
                URL keyStoreUrl = null;
                try {
                    keyStoreUrl = new URL(keyStoreName);
                    // absolute URL
                } catch (java.net.MalformedURLException e) {
                    // relative URL
                    keyStoreUrl = new URL(policyUrl, keyStoreName);
                }

                if (debug != null) {
                    debug.println("reading keystore"+keyStoreUrl);
                }

                InputStream inStream = new BufferedInputStream(
                    PolicyUtil.getInputStream(keyStoreUrl));

                KeyStore ks;
                if (keyStoreType != null)
                    ks = KeyStore.getInstance(keyStoreType);
                else
                    ks = KeyStore.getInstance(KeyStore.getDefaultType());
                ks.load(inStream, null);
                inStream.close();
                return ks;
            } catch (Exception e) {
                // ignore, treat it like we have no keystore
                if (debug != null) {
                    e.printStackTrace();
                }
                return null;
            }
        }
        return null;
    }

    private void initPolicyFile() {

        String prop = Security.getProperty("policy.expandProperties");
        if (prop != null) {
            expandProperties = prop.equalsIgnoreCase("true");
        }

        String iscp = Security.getProperty("policy.ignoreIdentityScope");
        if (iscp != null) {
            ignoreIdentityScope = iscp.equalsIgnoreCase("true");
        }

        String allowSys = Security.getProperty("policy.allowSystemProperty");
        if (allowSys != null && allowSys.equalsIgnoreCase("true")) {
            String extra_policy = System.getProperty(AUTH_POLICY);
            if (extra_policy != null) {
                boolean overrideAll = false;
                if (extra_policy.startsWith("=")) {
                    overrideAll = true;
                    extra_policy = extra_policy.substring(1);
                }
                try {
                    extra_policy = PropertyExpander.expand(extra_policy);
                    URL policyURL;
                    File policyFile = new File(extra_policy);
                    if (policyFile.exists()) {
                        policyURL =
                            new URL("file:" + policyFile.getCanonicalPath());
                    } else {
                        policyURL = new URL(extra_policy);
                    }
                    if (debug != null) {
                        debug.println("reading " + policyURL);
                    }
                    init(policyURL);
                } catch (Exception e) {
                    // ignore.
                    if (debug != null) {
                        debug.println("caught exception: " + e);
                    }

                }
                if (overrideAll) {
                    if (debug != null) {
                        debug.println("overriding other policies!");
                    }
                    return;
                }
            }
        }

        int n = 1;
        boolean loaded_one = false;
        String policy_url;

        while ((policy_url = Security.getProperty(AUTH_POLICY_URL+n)) != null) {
            try {
                policy_url = PropertyExpander.expand(policy_url).replace
                                                (File.separatorChar, '/');
                if (debug != null) {
                    debug.println("reading " + policy_url);
                }
                init(new URL(policy_url));
                loaded_one = true;
            } catch (Exception e) {
                if (debug != null) {
                    debug.println("error reading policy " + e);
                    e.printStackTrace();
                }
                // ignore that policy
            }
            n++;
        }

        if (loaded_one == false) {
            // do not load a static policy
        }
    }

    /**
     * Checks public key. If it is marked as trusted in
     * the identity database, add it to the policy
     * with the AllPermission.
     */
    private boolean checkForTrustedIdentity(final Certificate cert) {
        return false;
    }

    /**
     * Reads a policy configuration into the Policy object using a
     * Reader object.
     *
     * @param policyFile the policy Reader object.
     */
    private void init(URL policy) {
        PolicyParser pp = new PolicyParser(expandProperties);
        try (InputStreamReader isr
                = new InputStreamReader(PolicyUtil.getInputStream(policy))) {
            pp.read(isr);
            KeyStore keyStore = initKeyStore(policy, pp.getKeyStoreUrl(),
                                             pp.getKeyStoreType());
            Enumeration<GrantEntry> enum_ = pp.grantElements();
            while (enum_.hasMoreElements()) {
                GrantEntry ge = enum_.nextElement();
                addGrantEntry(ge, keyStore);
            }
        } catch (PolicyParser.ParsingException pe) {
            System.err.println(AUTH_POLICY +
                               rb.getString(".error.parsing.") + policy);
            System.err.println(AUTH_POLICY + rb.getString("COLON") +
                               pe.getMessage());
            if (debug != null) {
                pe.printStackTrace();
            }
        } catch (Exception e) {
            if (debug != null) {
                debug.println("error parsing " + policy);
                debug.println(e.toString());
                e.printStackTrace();
            }
        }
    }

    /**
     * Given a PermissionEntry, create a codeSource.
     *
     * @return null if signedBy alias is not recognized
     */
    CodeSource getCodeSource(GrantEntry ge, KeyStore keyStore)
            throws java.net.MalformedURLException
    {
        Certificate[] certs = null;
        if (ge.signedBy != null) {
            certs = getCertificates(keyStore, ge.signedBy);
            if (certs == null) {
                // we don't have a key for this alias,
                // just return
                if (debug != null) {
                    debug.println(" no certs for alias " +
                                       ge.signedBy + ", ignoring.");
                }
                return null;
            }
        }

        URL location;
        if (ge.codeBase != null) {
            location = new URL(ge.codeBase);
        } else {
            location = null;
        }

        if (ge.principals == null || ge.principals.size() == 0) {
            return (canonicalizeCodebase
                        (new CodeSource(location, certs),
                        false));
        } else {
            return (canonicalizeCodebase
                (new SubjectCodeSource(null, ge.principals, location, certs),
                false));
        }
    }

    /**
     * Add one policy entry to the vector.
     */
    private void addGrantEntry(GrantEntry ge, KeyStore keyStore) {

        if (debug != null) {
            debug.println("Adding policy entry: ");
            debug.println("  signedBy " + ge.signedBy);
            debug.println("  codeBase " + ge.codeBase);
            if (ge.principals != null) {
                for (PrincipalEntry pppe : ge.principals) {
                    debug.println("  " + pppe.getPrincipalClass() +
                                        " " + pppe.getPrincipalName());
                }
            }
            debug.println();
        }

        try {
            CodeSource codesource = getCodeSource(ge, keyStore);
            // skip if signedBy alias was unknown...
            if (codesource == null) return;

            PolicyEntry entry = new PolicyEntry(codesource);
            Enumeration<PermissionEntry> enum_ = ge.permissionElements();
            while (enum_.hasMoreElements()) {
                PermissionEntry pe = enum_.nextElement();
                try {
                    // XXX special case PrivateCredentialPermission-SELF
                    Permission perm;
                    if (pe.permission.equals
                        ("javax.security.auth.PrivateCredentialPermission") &&
                        pe.name.endsWith(" self")) {
                        perm = getInstance(pe.permission,
                                         pe.name + " \"self\"",
                                         pe.action);
                    } else {
                        perm = getInstance(pe.permission,
                                         pe.name,
                                         pe.action);
                    }
                    entry.add(perm);
                    if (debug != null) {
                        debug.println("  "+perm);
                    }
                } catch (ClassNotFoundException cnfe) {
                    Certificate certs[];
                    if (pe.signedBy != null) {
                        certs = getCertificates(keyStore, pe.signedBy);
                    } else {
                        certs = null;
                    }

                    // only add if we had no signer or we had a
                    // a signer and found the keys for it.
                    if (certs != null || pe.signedBy == null) {
                            Permission perm = new UnresolvedPermission(
                                             pe.permission,
                                             pe.name,
                                             pe.action,
                                             certs);
                            entry.add(perm);
                            if (debug != null) {
                                debug.println("  "+perm);
                            }
                    }
                } catch (java.lang.reflect.InvocationTargetException ite) {
                    System.err.println
                        (AUTH_POLICY +
                        rb.getString(".error.adding.Permission.") +
                        pe.permission +
                        rb.getString("SPACE") +
                        ite.getTargetException());
                } catch (Exception e) {
                    System.err.println
                        (AUTH_POLICY +
                        rb.getString(".error.adding.Permission.") +
                        pe.permission +
                        rb.getString("SPACE") +
                        e);
                }
            }
            policyEntries.addElement(entry);
        } catch (Exception e) {
            System.err.println
                (AUTH_POLICY +
                rb.getString(".error.adding.Entry.") +
                ge +
                rb.getString("SPACE") +
                e);
        }

        if (debug != null) {
            debug.println();
        }
    }

    /**
     * Returns a new Permission object of the given Type. The Permission is
     * created by getting the
     * Class object using the Class.forName method, and using
     * the reflection API to invoke the (String name, String actions)
     * constructor on the
     * object.
     *
     * @param type the type of Permission being created.
     * @param name the name of the Permission being created.
     * @param actions the actions of the Permission being created.
     *
     * @exception  ClassNotFoundException  if the particular Permission
     *             class could not be found.
     *
     * @exception  IllegalAccessException  if the class or initializer is
     *               not accessible.
     *
     * @exception  InstantiationException  if getInstance tries to
     *               instantiate an abstract class or an interface, or if the
     *               instantiation fails for some other reason.
     *
     * @exception  NoSuchMethodException if the (String, String) constructor
     *               is not found.
     *
     * @exception  InvocationTargetException if the underlying Permission
     *               constructor throws an exception.
     *
     */
    private static final Permission getInstance(String type,
                                    String name,
                                    String actions)
        throws ClassNotFoundException,
               InstantiationException,
               IllegalAccessException,
               NoSuchMethodException,
               InvocationTargetException
    {
        //XXX we might want to keep a hash of created factories...
        Class<?> pc = Class.forName(type);
        Constructor<?> c = pc.getConstructor(PARAMS);
        return (Permission) c.newInstance(new Object[] { name, actions });
    }

    /**
     * Fetch all certs associated with this alias.
     */
    Certificate[] getCertificates(KeyStore keyStore, String aliases) {

        Vector<Certificate> vcerts = null;

        StringTokenizer st = new StringTokenizer(aliases, ",");
        int n = 0;

        while (st.hasMoreTokens()) {
            String alias = st.nextToken().trim();
            n++;
            Certificate cert = null;
            // See if this alias's cert has already been cached
            cert = (Certificate) aliasMapping.get(alias);
            if (cert == null && keyStore != null) {

                try {
                    cert = keyStore.getCertificate(alias);
                } catch (KeyStoreException kse) {
                    // never happens, because keystore has already been loaded
                    // when we call this
                }
                if (cert != null) {
                    aliasMapping.put(alias, cert);
                    aliasMapping.put(cert, alias);
                }
            }

            if (cert != null) {
                if (vcerts == null) {
                    vcerts = new Vector<Certificate>();
                }
                vcerts.addElement(cert);
            }
        }

        // make sure n == vcerts.size, since we are doing a logical *and*
        if (vcerts != null && n == vcerts.size()) {
            Certificate[] certs = new Certificate[vcerts.size()];
            vcerts.copyInto(certs);
            return certs;
        } else {
            return null;
        }
    }

    /**
     * Enumerate all the entries in the global policy object.
     * This method is used by policy admin tools.   The tools
     * should use the Enumeration methods on the returned object
     * to fetch the elements sequentially.
     */
    private final synchronized Enumeration<PolicyEntry> elements() {
        return policyEntries.elements();
    }

    @Override
    public PermissionCollection getPermissions(final Subject subject,
                                               final CodeSource codesource) {

        // 1)   if code instantiates PolicyFile directly, then it will need
        //      all the permissions required for the PolicyFile initialization
        // 2)   if code calls Policy.getPolicy, then it simply needs
        //      AuthPermission(getPolicy), and the javax.security.auth.Policy
        //      implementation instantiates PolicyFile in a doPrivileged block
        // 3)   if after instantiating a Policy (either via #1 or #2),
        //      code calls getPermissions, PolicyFile wraps the call
        //      in a doPrivileged block.
        return AccessController.doPrivileged
            (new PrivilegedAction<PermissionCollection>() {
            @Override public PermissionCollection run() {
                SubjectCodeSource scs = new SubjectCodeSource(
                    subject, null,
                    codesource == null ? null : codesource.getLocation(),
                    codesource == null ? null : codesource.getCertificates());
                if (initialized) {
                    return getPermissions(new Permissions(), scs);
                } else {
                    return new PolicyPermissions(AuthPolicyFile.this, scs);
                }
            }
        });
    }

    /**
     * Examines the global policy for the specified CodeSource, and
     * creates a PermissionCollection object with
     * the set of permissions for that principal's protection domain.
     *
     * @param CodeSource the codesource associated with the caller.
     * This encapsulates the original location of the code (where the code
     * came from) and the public key(s) of its signer.
     *
     * @return the set of permissions according to the policy.
     */
    PermissionCollection getPermissions(CodeSource codesource) {

        if (initialized) {
            return getPermissions(new Permissions(), codesource);
        } else {
            return new PolicyPermissions(this, codesource);
        }
    }

    /**
     * Examines the global policy for the specified CodeSource, and
     * creates a PermissionCollection object with
     * the set of permissions for that principal's protection domain.
     *
     * @param permissions the permissions to populate
     * @param codesource the codesource associated with the caller.
     * This encapsulates the original location of the code (where the code
     * came from) and the public key(s) of its signer.
     *
     * @return the set of permissions according to the policy.
     */
    Permissions getPermissions(final Permissions perms,
                               final CodeSource cs)
    {
        if (!initialized) {
            init();
        }

        final CodeSource codesource[] = {null};

        codesource[0] = canonicalizeCodebase(cs, true);

        if (debug != null) {
            debug.println("evaluate(" + codesource[0] + ")\n");
        }

        // needs to be in a begin/endPrivileged block because
        // codesource.implies calls URL.equals which does an
        // InetAddress lookup

        for (int i = 0; i < policyEntries.size(); i++) {

           PolicyEntry entry = policyEntries.elementAt(i);

           if (debug != null) {
                debug.println("PolicyFile CodeSource implies: " +
                              entry.codesource.toString() + "\n\n" +
                              "\t" + codesource[0].toString() + "\n\n");
           }

           if (entry.codesource.implies(codesource[0])) {
               for (int j = 0; j < entry.permissions.size(); j++) {
                    Permission p = entry.permissions.elementAt(j);
                    if (debug != null) {
                        debug.println("  granting " + p);
                    }
                    if (!addSelfPermissions(p, entry.codesource,
                                            codesource[0], perms)) {
                        // we could check for duplicates
                        // before adding new permissions,
                        // but the SubjectDomainCombiner
                        // already checks for duplicates later
                        perms.add(p);
                    }
                }
            }
        }

        // now see if any of the keys are trusted ids.

        if (!ignoreIdentityScope) {
            Certificate certs[] = codesource[0].getCertificates();
            if (certs != null) {
                for (int k=0; k < certs.length; k++) {
                    if (aliasMapping.get(certs[k]) == null &&
                        checkForTrustedIdentity(certs[k])) {
                        // checkForTrustedIdentity added it
                        // to the policy for us. next time
                        // around we'll find it. This time
                        // around we need to add it.
                        perms.add(new java.security.AllPermission());
                    }
                }
            }
        }
        return perms;
    }

    /**
     * Returns true if 'Self' permissions were added to the provided
     * 'perms', and false otherwise.
     *
     *

     *
     * @param p check to see if this Permission is a "SELF"
     *                  PrivateCredentialPermission.

     *
     * @param entryCs the codesource for the Policy entry.
     *
     * @param accCs the codesource for from the current AccessControlContext.
     *
     * @param perms the PermissionCollection where the individual
     *                  PrivateCredentialPermissions will be added.
     */
    private boolean addSelfPermissions(final Permission p,
                                       CodeSource entryCs,
                                       CodeSource accCs,
                                       Permissions perms) {

        if (!(p instanceof PrivateCredentialPermission)) {
            return false;
        }

        if (!(entryCs instanceof SubjectCodeSource)) {
            return false;
        }

        PrivateCredentialPermission pcp = (PrivateCredentialPermission)p;
        SubjectCodeSource scs = (SubjectCodeSource)entryCs;

        // see if it is a SELF permission
        String[][] pPrincipals = pcp.getPrincipals();
        if (pPrincipals.length <= 0 ||
            !pPrincipals[0][0].equalsIgnoreCase("self") ||
            !pPrincipals[0][1].equalsIgnoreCase("self")) {

            // regular PrivateCredentialPermission
            return false;
        } else {

            // granted a SELF permission - create a
            // PrivateCredentialPermission for each
            // of the Policy entry's CodeSource Principals

            if (scs.getPrincipals() == null) {
                // XXX SubjectCodeSource has no Subject???
                return true;
            }

            for (PrincipalEntry principal : scs.getPrincipals()) {

                //      if the Policy entry's Principal does not contain a
                //              WILDCARD for the Principal name, then a
                //              new PrivateCredentialPermission is created
                //              for the Principal listed in the Policy entry.
                //      if the Policy entry's Principal contains a WILDCARD
                //              for the Principal name, then a new
                //              PrivateCredentialPermission is created
                //              for each Principal associated with the Subject
                //              in the current ACC.

                String[][] principalInfo = getPrincipalInfo(principal, accCs);

                for (int i = 0; i < principalInfo.length; i++) {

                    // here's the new PrivateCredentialPermission

                    PrivateCredentialPermission newPcp =
                        new PrivateCredentialPermission
                                (pcp.getCredentialClass() +
                                        " " +
                                        principalInfo[i][0] +
                                        " " +
                                        "\"" + principalInfo[i][1] + "\"",
                                "read");

                    if (debug != null) {
                        debug.println("adding SELF permission: " +
                                        newPcp.toString());
                    }

                    perms.add(newPcp);
                }
            }
        }
        return true;
    }

    /**
     * return the principal class/name pair in the 2D array.
     * array[x][y]:     x corresponds to the array length.
     *                  if (y == 0), it's the principal class.
     *                  if (y == 1), it's the principal name.
     */
    private String[][] getPrincipalInfo(PrincipalEntry principal,
                                        final CodeSource accCs) {

        // there are 3 possibilities:
        // 1) the entry's Principal class and name are not wildcarded
        // 2) the entry's Principal name is wildcarded only
        // 3) the entry's Principal class and name are wildcarded

        if (!principal.getPrincipalClass().equals
                (PrincipalEntry.WILDCARD_CLASS) &&
            !principal.getPrincipalName().equals
                (PrincipalEntry.WILDCARD_NAME)) {

            // build a PrivateCredentialPermission for the principal
            // from the Policy entry
            String[][] info = new String[1][2];
            info[0][0] = principal.getPrincipalClass();
            info[0][1] = principal.getPrincipalName();
            return info;

        } else if (!principal.getPrincipalClass().equals
                (PrincipalEntry.WILDCARD_CLASS) &&
            principal.getPrincipalName().equals
                (PrincipalEntry.WILDCARD_NAME)) {

            // build a PrivateCredentialPermission for all
            // the Subject's principals that are instances of principalClass

            // the accCs is guaranteed to be a SubjectCodeSource
            // because the earlier CodeSource.implies succeeded
            SubjectCodeSource scs = (SubjectCodeSource)accCs;

            Set<? extends Principal> principalSet = null;
            try {
                // principal.principalClass should extend Principal
                // If it doesn't, we should stop here with a ClassCastException.
                @SuppressWarnings("unchecked")
                Class<? extends Principal> pClass = (Class<? extends Principal>)
                        Class.forName(principal.getPrincipalClass(), false,
                                      ClassLoader.getSystemClassLoader());
                principalSet = scs.getSubject().getPrincipals(pClass);
            } catch (Exception e) {
                if (debug != null) {
                    debug.println("problem finding Principal Class " +
                                  "when expanding SELF permission: " +
                                  e.toString());
                }
            }

            if (principalSet == null) {
                // error
                return new String[0][0];
            }

            String[][] info = new String[principalSet.size()][2];

            int i = 0;
            for (Principal p : principalSet) {
                info[i][0] = p.getClass().getName();
                info[i][1] = p.getName();
                i++;
            }
            return info;

        } else {

            // build a PrivateCredentialPermission for every
            // one of the current Subject's principals

            // the accCs is guaranteed to be a SubjectCodeSource
            // because the earlier CodeSource.implies succeeded
            SubjectCodeSource scs = (SubjectCodeSource)accCs;
            Set<Principal> principalSet = scs.getSubject().getPrincipals();

            String[][] info = new String[principalSet.size()][2];

            int i = 0;
            for (Principal p : principalSet) {
                info[i][0] = p.getClass().getName();
                info[i][1] = p.getName();
                i++;
            }
            return info;
        }
    }

    /*
     * Returns the signer certificates from the list of certificates associated
     * with the given code source.
     *
     * The signer certificates are those certificates that were used to verify
     * signed code originating from the codesource location.
     *
     * This method assumes that in the given code source, each signer
     * certificate is followed by its supporting certificate chain
     * (which may be empty), and that the signer certificate and its
     * supporting certificate chain are ordered bottom-to-top (i.e., with the
     * signer certificate first and the (root) certificate authority last).
     */
    Certificate[] getSignerCertificates(CodeSource cs) {
        Certificate[] certs = null;
        if ((certs = cs.getCertificates()) == null) {
            return null;
        }
        for (int i = 0; i < certs.length; i++) {
            if (!(certs[i] instanceof X509Certificate))
                return cs.getCertificates();
        }

        // Do we have to do anything?
        int i = 0;
        int count = 0;
        while (i < certs.length) {
            count++;
            while (((i+1) < certs.length)
                   && ((X509Certificate)certs[i]).getIssuerDN().equals(
                           ((X509Certificate)certs[i+1]).getSubjectDN())) {
                i++;
            }
            i++;
        }
        if (count == certs.length) {
            // Done
            return certs;
        }

        ArrayList<Certificate> userCertList = new ArrayList<>();
        i = 0;
        while (i < certs.length) {
            userCertList.add(certs[i]);
            while (((i+1) < certs.length)
                   && ((X509Certificate)certs[i]).getIssuerDN().equals(
                           ((X509Certificate)certs[i+1]).getSubjectDN())) {
                i++;
            }
            i++;
        }
        Certificate[] userCerts = new Certificate[userCertList.size()];
        userCertList.toArray(userCerts);
        return userCerts;
    }

    private CodeSource canonicalizeCodebase(CodeSource cs,
                                            boolean extractSignerCerts) {
        CodeSource canonCs = cs;
        if (cs.getLocation() != null &&
            cs.getLocation().getProtocol().equalsIgnoreCase("file")) {
            try {
                String path = cs.getLocation().getFile().replace
                                                        ('/',
                                                        File.separatorChar);
                URL csUrl = null;
                if (path.endsWith("*")) {
                    // remove trailing '*' because it causes canonicalization
                    // to fail on win32
                    path = path.substring(0, path.length()-1);
                    boolean appendFileSep = false;
                    if (path.endsWith(File.separator)) {
                        appendFileSep = true;
                    }
                    if (path.equals("")) {
                        path = System.getProperty("user.dir");
                    }
                    File f = new File(path);
                    path = f.getCanonicalPath();
                    StringBuffer sb = new StringBuffer(path);
                    // reappend '*' to canonicalized filename (note that
                    // canonicalization may have removed trailing file
                    // separator, so we have to check for that, too)
                    if (!path.endsWith(File.separator) &&
                        (appendFileSep || f.isDirectory())) {
                        sb.append(File.separatorChar);
                    }
                    sb.append('*');
                    path = sb.toString();
                } else {
                    path = new File(path).getCanonicalPath();
                }
                csUrl = new File(path).toURL();

                if (cs instanceof SubjectCodeSource) {
                    SubjectCodeSource scs = (SubjectCodeSource)cs;
                    if (extractSignerCerts) {
                        canonCs = new SubjectCodeSource(scs.getSubject(),
                                                        scs.getPrincipals(),
                                                        csUrl,
                                                        getSignerCertificates(scs));
                    } else {
                        canonCs = new SubjectCodeSource(scs.getSubject(),
                                                        scs.getPrincipals(),
                                                        csUrl,
                                                        scs.getCertificates());
                    }
                } else {
                    if (extractSignerCerts) {
                        canonCs = new CodeSource(csUrl,
                                                 getSignerCertificates(cs));
                    } else {
                        canonCs = new CodeSource(csUrl,
                                                 cs.getCertificates());
                    }
                }
            } catch (IOException ioe) {
                // leave codesource as it is, unless we have to extract its
                // signer certificates
                if (extractSignerCerts) {
                    if (!(cs instanceof SubjectCodeSource)) {
                        canonCs = new CodeSource(cs.getLocation(),
                                                getSignerCertificates(cs));
                    } else {
                        SubjectCodeSource scs = (SubjectCodeSource)cs;
                        canonCs = new SubjectCodeSource(scs.getSubject(),
                                                scs.getPrincipals(),
                                                scs.getLocation(),
                                                getSignerCertificates(scs));
                    }
                }
            }
        } else {
            if (extractSignerCerts) {
                if (!(cs instanceof SubjectCodeSource)) {
                    canonCs = new CodeSource(cs.getLocation(),
                                        getSignerCertificates(cs));
                } else {
                    SubjectCodeSource scs = (SubjectCodeSource)cs;
                    canonCs = new SubjectCodeSource(scs.getSubject(),
                                        scs.getPrincipals(),
                                        scs.getLocation(),
                                        getSignerCertificates(scs));
                }
            }
        }
        return canonCs;
    }

    /**
     * Each entry in the policy configuration file is represented by a
     * PolicyEntry object.

     *
     * A PolicyEntry is a (CodeSource,Permission) pair.  The
     * CodeSource contains the (URL, PublicKey) that together identify
     * where the Java bytecodes come from and who (if anyone) signed
     * them.  The URL could refer to localhost.  The URL could also be
     * null, meaning that this policy entry is given to all comers, as
     * long as they match the signer field.  The signer could be null,
     * meaning the code is not signed.

     *
     * The Permission contains the (Type, Name, Action) triplet.

     *
     * For now, the Policy object retrieves the public key from the
     * X.509 certificate on disk that corresponds to the signedBy
     * alias specified in the Policy config file.  For reasons of
     * efficiency, the Policy object keeps a hashtable of certs already
     * read in.  This could be replaced by a secure internal key
     * store.
     *
     *

     * For example, the entry
     *

     *          permission java.io.File "/tmp", "read,write",
     *          signedBy "Duke";
     *

     * is represented internally
     *

     *
     * FilePermission f = new FilePermission("/tmp", "read,write");
     * PublicKey p = publickeys.get("Duke");
     * URL u = InetAddress.getLocalHost();
     * CodeBase c = new CodeBase( p, u );
     * pe = new PolicyEntry(f, c);
     *

     *
     * @author Marianne Mueller
     * @author Roland Schemers
     * @see java.security.CodeSource
     * @see java.security.Policy
     * @see java.security.Permissions
     * @see java.security.ProtectionDomain
     */
    private static class PolicyEntry {

        CodeSource codesource;
        Vector<Permission> permissions;

        /**
         * Given a Permission and a CodeSource, create a policy entry.
         *
         * XXX Decide if/how to add validity fields and "purpose" fields to
         * XXX policy entries
         *
         * @param cs the CodeSource, which encapsulates the URL and the public
         *        key attributes from the policy config file. Validity checks
         *        are performed on the public key before PolicyEntry is called.
         *
         */
        PolicyEntry(CodeSource cs) {
            this.codesource = cs;
            this.permissions = new Vector<Permission>();
        }

        /**
         * add a Permission object to this entry.
         */
        void add(Permission p) {
            permissions.addElement(p);
        }

        /**
         * Return the CodeSource for this policy entry
         */
        CodeSource getCodeSource() {
            return this.codesource;
        }

        @Override
        public String toString(){
            StringBuffer sb = new StringBuffer();
            sb.append(rb.getString("LPARAM"));
            sb.append(getCodeSource());
            sb.append("\n");
            for (int j = 0; j < permissions.size(); j++) {
                Permission p = permissions.elementAt(j);
                sb.append(rb.getString("SPACE"));
                sb.append(rb.getString("SPACE"));
                sb.append(p);
                sb.append(rb.getString("NEWLINE"));
            }
            sb.append(rb.getString("RPARAM"));
            sb.append(rb.getString("NEWLINE"));
            return sb.toString();
        }

    }
}

@SuppressWarnings("deprecation")
class PolicyPermissions extends PermissionCollection {

    private static final long serialVersionUID = -1954188373270545523L;

    private CodeSource codesource;
    private Permissions perms;
    private AuthPolicyFile policy;
    private boolean notInit; // have we pulled in the policy permissions yet?
    private Vector<Permission> additionalPerms;

    PolicyPermissions(AuthPolicyFile policy,
                      CodeSource codesource)
    {
        this.codesource = codesource;
        this.policy = policy;
        this.perms = null;
        this.notInit = true;
        this.additionalPerms = null;
    }

    @Override
    public void add(Permission permission) {
        if (isReadOnly())
            throw new SecurityException
            (AuthPolicyFile.rb.getString
            ("attempt.to.add.a.Permission.to.a.readonly.PermissionCollection"));

        if (perms == null) {
            if (additionalPerms == null) {
                additionalPerms = new Vector<Permission>();
            }
            additionalPerms.add(permission);
        } else {
            perms.add(permission);
        }
    }

    private synchronized void init() {
        if (notInit) {
            if (perms == null) {
                perms = new Permissions();
            }
            if (additionalPerms != null) {
                Enumeration<Permission> e = additionalPerms.elements();
                while (e.hasMoreElements()) {
                    perms.add(e.nextElement());
                }
                additionalPerms = null;
            }
            policy.getPermissions(perms, codesource);
            notInit = false;
        }
    }

    @Override
    public boolean implies(Permission permission) {
        if (notInit) {
            init();
        }
        return perms.implies(permission);
    }

    @Override
    public Enumeration<Permission> elements() {
        if (notInit) {
            init();
        }
        return perms.elements();
    }

    @Override
    public String toString() {
        if (notInit) {
            init();
        }
        return perms.toString();
    }
}


text:
package com.workplacesystems.utilsj;

/**
 *
 * @author dave
 */
public interface Condition
{
    public boolean isTrue(int read_hold_count);
}


text:
@implementation HotTub

-(instancetype)init {
    self = [super init];

    _temperature = 102;

    return self;
}

-(void)heatUp {
    self.temperature = self.temperature + 1;
}

-(void)coolDown {
    self.temperature = self.temperature - 1;
}

@end


text:
<!doctype html>

	<title>event</title>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="mystyle.css">

	<form >
		<input type = "text" onchange="ch(this)">

		<input type = "text" onselect="select(this)" onfocus="sele2(this)">
	</form>


text:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="Settings" >

    <PreferenceCategory
        android:title="View">

        <MultiSelectListPreference
            android:title="Pages"
            android:key="Pages"
            android:summary="Tap to select pages to view"
            android:entries="@array/pages"
            android:entryValues="@array/page_values"
            android:defaultValue="@array/page_defaults"/>

    </PreferenceCategory>

    <PreferenceCategory
        android:title="Advanced">

        <CheckBoxPreference
            android:title="Javascript"
            android:key="Javascript"
            android:summary="Keep checked to load all contents of page"
            android:defaultValue="true"/>

    </PreferenceCategory>

</PreferenceScreen>


text:
@class NSArray, NSMutableArray, NSObject<OS_dispatch_queue>;

@interface XCSLogger : NSObject
{
    NSMutableArray *_destinations;
    NSMutableArray *_tags;
    NSObject<OS_dispatch_queue> *_queue;
}

+ (id)sharedLogger;
+ (void)initialize;
@property(retain, nonatomic) NSObject<OS_dispatch_queue> *queue; // @synthesize queue=_queue;
@property(retain, nonatomic) NSMutableArray *tags; // @synthesize tags=_tags;
@property(retain) NSArray *destinations; // @synthesize destinations=_destinations;
- (void).cxx_destruct;
- (void)logMessageFromFile:(id)arg1 lineNumber:(long long)arg2 className:(id)arg3 methodName:(id)arg4 tags:(id)arg5 userInfo:(id)arg6 format:(id)arg7 arguments:(struct __va_list_tag [1])arg8;
- (void)logMessageFromFile:(const char *)arg1 lineNumber:(long long)arg2 class:(Class)arg3 method:(SEL)arg4 arguments:(id)arg5;
- (void)loadDestinationsFromConfiguration:(id)arg1;
- (void)reloadConfigurationFromDefaults;
- (void)removeTag:(id)arg1;
- (void)addTag:(id)arg1;
- (void)removeDestination:(id)arg1;
- (void)addDestination:(id)arg1;
- (id)initWithDestinationConfiguration:(id)arg1;
- (id)init;

@end