Package com.volantis.mcs.policies.variants

Examples of com.volantis.mcs.policies.variants.Variant


            return null;
        }

        ActivatedVariablePolicy policy = selected.getPolicy();

        Variant variant = selected.getVariant();
        if (variant == null) {
            return null;
        }

        Content content = variant.getContent();
        if (content instanceof EmbeddedContent) {
            EmbeddedContent embedded = (EmbeddedContent) content;
            return embedded.getData();
        } else {
            MarinerURL marinerURL = computeURL((Asset) selected.getOldObject());
View Full Code Here


        return group;
    }

    public BaseLocation getBaseLocation(SelectedVariant selected) {

        Variant variant = selected.getVariant();
        Content content = variant.getContent();
        if (content instanceof BaseURLRelative) {
            BaseURLRelative relative = (BaseURLRelative) content;
            BaseLocation baseLocation = relative.getBaseLocation();
            if (baseLocation != BaseLocation.DEFAULT) {
                return baseLocation;
View Full Code Here

    protected String getTextFromSelectedVariant(TextEncoding encoding) {
        String text = null;

        SelectedVariant selectedVariant = getSelectedVariant();
        if (selectedVariant != null) {
            Variant variant = selectedVariant.getVariant();
            if (variant != null) {
                TextMetaData metaData = (TextMetaData) variant.getMetaData();
                if (metaData.getTextEncoding() == encoding) {
                    text = assetResolver.getContentsFromVariant(selectedVariant);
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Text for policy " +
View Full Code Here

               
        // The external link rendered for device independent resource will have
        // correct URL if used device support encoding from selected variant
        // otherwise returned variant will be null because selected variant is
        // filtered by PolicyVariantSelector#filter method       
        Variant variant = selectedVariant.getVariant();
        VariantType variantType = null;

        if(variant != null) {
            variantType = variant.getVariantType();
        }

        // for MTXT resource
        if (variantType == VariantType.TEXT) {
            PolicyReferenceResolver resolver = pageContext
                    .getPolicyReferenceResolver();
            TextAssetReference textReference = resolver
                    .resolveUnquotedTextExpression(src);

            final String text = textReference.getText(TextEncoding.PLAIN);
            if (text != null) {
                // We have found the text, so let's try and write it out to
                // attributes
                attributes.setTextContainer(text);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("No text exists for text asset  " + src);
                }
            }
            // remove src pointed at text component
            attributes.setSrc(null);

            // remove srcType which contains list of device independent mime
            // types
            attributes.setSrcType(null);
        }

        // retrieve url only for IMAGE, AUDIO and VIDEO resource so far
        String url = assetResolver.retrieveVariantURLAsString(selectedVariant);

        // get dimensions for image and video and save it to params into
        // attributes
        if (variantType == VariantType.IMAGE
                || variantType == VariantType.VIDEO) {

            PixelDimensionsMetaData dimMetaData =
                (PixelDimensionsMetaData) selectedVariant
                    .getVariant().getMetaData();
            // get the Map of parameters
            Map params = attributes.getParamMap();

            // save height and width in pixels unit to object's parameter map
            if (dimMetaData.getHeight() != 0) {
                params.put("height", StringConvertor.valueOf(dimMetaData
                        .getHeight()));
            }
            if (dimMetaData.getWidth() != 0) {
                params.put("width", StringConvertor.valueOf(dimMetaData
                        .getWidth()));
            }
        }

        // convert image if needed
        if (variantType == VariantType.IMAGE) {
            ImageMetaData imageMetaData = (ImageMetaData) selectedVariant
                    .getVariant().getMetaData();

            // image must be convert
            if (url != null
                    && imageMetaData.getConversionMode() ==
                        ImageConversionMode.ALWAYS_CONVERT) {
                url = assetResolver.rewriteURLWithPageURLRewriter(url,
                        PageURLType.IMAGE);

                attributes.setConvertibleImageAsset(true);               
            }
        }

        if (url != null) {
            // new mime types for device dependent resources
            String mimeTypes = "";
            Iterator encodingIterator = null;
            // for each variant type get encoding and save its all mime types
            // into attributes' srcType
            if (variantType == VariantType.VIDEO) {
                VideoMetaData metaData = (VideoMetaData) variant.getMetaData();
                encodingIterator = metaData.getVideoEncoding().mimeTypes();
            } else if (variantType == VariantType.AUDIO) {
                AudioMetaData metaData = (AudioMetaData) variant.getMetaData();
                encodingIterator = metaData.getAudioEncoding().mimeTypes();
            } else if (variantType == VariantType.IMAGE) {
                // This is incorrect if the asset is marked as
                // <conversion-mode>always</conversion-mode>
                // as we do not know what MAP will convert it to.
                ImageMetaData metaData = (ImageMetaData) variant.getMetaData();
                encodingIterator = metaData.getImageEncoding().mimeTypes();
            }

            int counter = 0;
            while (encodingIterator != null && encodingIterator.hasNext()) {
View Full Code Here

        Iterator variants = delegate.variantIterator();

        Map device2Variant = null;
        Map category2Variant = null;
        Map encoding2Variant = null;
        Variant defaultVariant = null;
        List genericImages = null;

        boolean hasCategories = delegate.getCategorizationScheme() != null;

        // Iterate over all the variants populating selection specific data
        // structures.
        while (variants.hasNext()) {
            Variant variant = (Variant) variants.next();
            Selection selection = variant.getSelection();
            if (selection instanceof TargetedSelection) {
                TargetedSelection targetedSelection =
                        (TargetedSelection) selection;

                // Only generate categories if they are supported by this
                // policy.
                if (hasCategories) {
                    List categories = targetedSelection.getCategoryReferences();
                    if (categories != null && !categories.isEmpty()) {
                        category2Variant =
                                targetVariantAtCategories(variant, categories,
                                        category2Variant);
                    }
                }

                List devices = targetedSelection.getDeviceReferences();
                if (devices != null && !devices.isEmpty()) {
                    device2Variant = targetVariantAtDevices(variant, devices,
                            device2Variant);
                }
            } else if (selection instanceof DefaultSelection) {
                defaultVariant = variant;
            } else if (selection instanceof EncodingSelection) {
                InternalSingleEncoding encoding = (InternalSingleEncoding)
                        variant.getMetaData();
                if (encoding2Variant == null) {
                    encoding2Variant = new HashMap();
                }
                encoding2Variant.put(encoding.getEncoding(), variant);
            } else if (selection instanceof GenericImageSelection) {
View Full Code Here

            SelectionContext context, ActivatedVariablePolicy variablePolicy) {

        InternalDevice device = context.getDevice();

        EncodingCollection supportedEncodings = device.getSupportedVideoEncodings();
        Variant variant;

        // Try TV first if it is supported.
        if (supportedEncodings.contains(VideoEncoding.TV)) {
            variant = variablePolicy.getVariantWithEncoding(VideoEncoding.TV);
            if (variant != null) {
View Full Code Here

            public void execute(File temporaryFile) throws Exception {

                final String assetGroup = "assetgroup";
                final String deviceName = "wapdevice";
                FileImageAssetFactory factory = new FileImageAssetFactory();
                Variant variant = factory.createDeviceImageVariant(
                        temporaryFile, assetGroup, deviceName).getVariant();

                // Test selection.
                TargetedSelection selection = (TargetedSelection)
                        variant.getSelection();
                List devices = selection.getDeviceReferences();
                assertEquals("Device List Size", 1, devices.size());
                assertEquals("Device", deviceName,
                        ((DeviceReference) devices.get(0)).getDeviceName());
View Full Code Here

        fileManager.executeWith(new TemporaryFileExecutor() {
            public void execute(File temporaryFile) throws Exception {

                FileImageAssetFactory factory = new FileImageAssetFactory();
                final String assetGroup = "assetgroup";
                Variant variant = factory.createGenericImageVariant(
                        temporaryFile, assetGroup, 50).getVariant();

                // Test selection.
                GenericImageSelection selection = (GenericImageSelection)
                        variant.getSelection();
                assertEquals("", 50, selection.getWidthHint());

                // Test meta data.
                checkMetaData(variant, 36, 36, 8, ImageRendering.COLOR,
                        ImageEncoding.GIF);
View Full Code Here

        fileManager.executeWith(new TemporaryFileExecutor() {
            public void execute(File temporaryFile) throws Exception {

                FileImageAssetFactory factory = new FileImageAssetFactory();
                final String assetGroup = "assetgroup";
                Variant variant = factory.createConvertibleImageVariant(
                        temporaryFile, assetGroup).getVariant();

                // Test selection.
                DefaultSelection selection = (DefaultSelection)
                        variant.getSelection();

                // Test meta data.
                checkMetaData(variant, 100, 75, 24, ImageRendering.COLOR,
                        ImageEncoding.PNG);
View Full Code Here

        // Check each image is supported by the requesting device.  If not then
        // we can discard the image from our selection. Also remove any images that
        // are wider than the width of the device.
//        for (int i = 0; i < images.length; i++) {
        for (Iterator i = images.iterator(); i.hasNext();) {
            Variant variant = (Variant) i.next();
            ImageMetaData image = (ImageMetaData) variant.getMetaData();

            ImageEncoding encoding = image.getImageEncoding();
            if (!supportedEncodings.contains(encoding)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Discarding variant " + variant +
                            " as device does not support encoding.");
                }
                i.remove();
                continue;
            }

            int currentWidth = image.getWidth();
            if (currentWidth > deviceWidth) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Discarding variant " + variant +
                            " - width greater than device width.");
                }
                i.remove();
                continue;
            }
        }

        // If the list is empty then the device does not support any of the
        // images in the collection we are testing so we can return null.
        if (images.isEmpty()) {
            return null;
        }


        // Remove any images that are wider than the specified widthHint percentage
        // of the device width.  In this loop we also find the maximum width of all
        // the remaining variants.
        for (Iterator i = images.iterator(); i.hasNext();) {
            Variant variant = (Variant) i.next();
            ImageMetaData image = (ImageMetaData) variant.getMetaData();

            GenericImageSelection generic = (GenericImageSelection)
                    variant.getSelection();

            int widthHint = generic.getWidthHint();
            int currentWidth = image.getWidth();
            if (widthHint != 0) {
                int allowedWidth = (widthHint * deviceWidth) / 100;
                if (currentWidth > allowedWidth) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Discarding variant " + variant +
                                " - width (" + currentWidth +
                                ") greater than " + widthHint +
                                "% of device width (" + deviceWidth + ")");
                    }
                    i.remove();
                    continue;
                }
            }

            if (currentWidth > maxWidth) {
                maxWidth = currentWidth;
            }
        }

        // If the list is empty then the device does not support any of the
        // images in the collection we are testing so we can return null.
        if (images.isEmpty()) {
            return null;
        }

        // Remove any images that are less than the maximum width as we always want
        // to return the largest possible image.  Here we also check if any of the
        // images' rendering type matches the device.
        ImageRendering deviceRenderingMode = device.getRenderMode();
        boolean renderingMatch = false;
        for (Iterator i = images.iterator(); i.hasNext();) {
            Variant variant = (Variant) i.next();
            ImageMetaData image = (ImageMetaData) variant.getMetaData();

            if (image.getWidth() < maxWidth) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Discarding variant " + variant +
                            " - width less than best width.");
                }
                i.remove();
                continue;
            }

            if (image.getRendering() == deviceRenderingMode) {
                renderingMatch = true;
            }
        }

        // If the list is empty then the device does not support any of the
        // images in the collection we are testing so we can return null.
        if (images.isEmpty()) {
            return null;
        }

        int devicePixelDepth = device.getPixelDepth();

        // there is an image with a pixelDepth => devicePixelDepth
        boolean pixelDepthMatch = false;
        // there is an image with a pixelDepth of 1 when the device supports better
        boolean poorPixelDepthMatch = false;
        // there is an image with a pixelDepth greater than that of the device
        boolean pixelDepthMismatch = false;

        for (Iterator i = images.iterator(); i.hasNext();) {
            Variant variant = (Variant) i.next();
            ImageMetaData image = (ImageMetaData) variant.getMetaData();

            if (renderingMatch) {
                // If there was at least one rendering match then we remove all those
                // images that were not a match.
                if (image.getRendering() != deviceRenderingMode) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Discarding variant " + variant +
                                " - rendering type does not match.");
                    }
                    i.remove();
                    continue;
                }
            } else {
                // If there wasn't any rendering matches then if image encoding is in
                // the set JPEG, GIF, PNG, BMP or TIFF it can still be used.  Otherwise the
                // image is removed.
                ImageEncoding currentImageEncoding = image.getImageEncoding();
                if (currentImageEncoding != ImageEncoding.JPEG
                        && currentImageEncoding != ImageEncoding.GIF
                        && currentImageEncoding != ImageEncoding.PNG
                        && currentImageEncoding != ImageEncoding.TIFF
                        && currentImageEncoding != ImageEncoding.BMP) {

                    if (logger.isDebugEnabled()) {
                        logger.debug("Discarding variant " + variant +
                                " - encoding type does not allow " +
                                "override of rendering.");
                    }

                    i.remove();
                    continue;
                }
            }

            if (image.getPixelDepth() <= devicePixelDepth) {
                if (image.getPixelDepth() == 1 && devicePixelDepth > 1) {
                    poorPixelDepthMatch = true;
                } else {
                    pixelDepthMatch = true;
                }
            } else {
                pixelDepthMismatch = true;
            }
        }

        // If the list is empty then there wasn't a render match and none
        // of the images had an encoding type of JPEG, GIF, PNG, BMP.  So we
        // can return null as none of the images are suitable.
        if (images.isEmpty()) {
            return null;
        }

        // If we didn't match a pixel depth but did find a 1bit image when the
        // device supports better, and we don't have anything better, then our
        // 1bit image really was our pixelDepthMatch so set the flag to true.
        if (!pixelDepthMatch && poorPixelDepthMatch && !pixelDepthMismatch) {
            pixelDepthMatch = true;
        }


        // There was at least one image with a pixel depth within the allowed
        // range, so remove all those that have a pixel depth greater than the
        // device.
        if (pixelDepthMatch) {
            for (Iterator i = images.iterator(); i.hasNext();) {
                Variant variant = (Variant) i.next();
                ImageMetaData image = (ImageMetaData) variant.getMetaData();

                if (image.getPixelDepth() > devicePixelDepth) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Discarding variant " + variant +
                                " - pixelDepth less than best available.");
                    }
                    i.remove();
                    continue;
                }
            }
        } else {
            // All of the images have a pixel depth greater than the device.  However
            // we can still use those images in the set JPEG, GIF, PNG, BMP or TIFF.  All
            // others must be removed
            for (Iterator i = images.iterator(); i.hasNext();) {
                Variant variant = (Variant) i.next();
                ImageMetaData image = (ImageMetaData) variant.getMetaData();

                ImageEncoding currentImageEncoding = image.getImageEncoding();
                if (currentImageEncoding != ImageEncoding.JPEG
                        && currentImageEncoding != ImageEncoding.GIF
                        && currentImageEncoding != ImageEncoding.PNG
                        && currentImageEncoding != ImageEncoding.TIFF
                        && currentImageEncoding != ImageEncoding.BMP) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Discarding variant " + variant +
                                " - pixelDepth too great and" +
                                " outside of valid set of encodings capable" +
                                " of reducing in depth.");
                    }
                    i.remove();
                    continue;
                }
            }
        }

        // If the list is empty then there wasn't an image with a pixel depth
        // less than the device or with an encoding type of JPEG, GIF, PNG, BMP
        // or TIFF. So we can return null as none of the images are suitable.
        if (images.isEmpty()) {
            return null;
        } else if (images.size() == 1) {
            return (Variant) images.get(0);
        }

        // Find the best pixelDepth of the remaining images.
        // If we had a pixelDepth match then it is simply the largest.
        // Otherwise it is the closest above the device pixel depth.
        int bestDepth = -1;
        if (pixelDepthMatch) {
            for (Iterator i = images.iterator(); i.hasNext();) {
                Variant variant = (Variant) i.next();
                ImageMetaData image = (ImageMetaData) variant.getMetaData();

                if (image.getPixelDepth() > bestDepth) {
                    bestDepth = image.getPixelDepth();
                }
            }
        } else {
            int bestDiff = 0;
            for (Iterator i = images.iterator(); i.hasNext();) {
                Variant variant = (Variant) i.next();
                ImageMetaData image = (ImageMetaData) variant.getMetaData();

                int pixelDepth = image.getPixelDepth();

                if (pixelDepth == 1) {
                    if (bestDepth == -1) {
                        bestDepth = pixelDepth;
                    }
                    continue;
                }

                int diff = pixelDepth - devicePixelDepth;

                // make the value absolute;
                if (diff < 0) {
                    diff = 0 - diff;
                }

                // diff cannot be 0 if we are in this loop because we would have
                // matched pixelDepth earlier.  So, it is safe to use an initial value
                // of zero for bestDiff.
                if (bestDiff == 0 || diff < bestDiff) {
                    bestDiff = diff;
                    bestDepth = image.getPixelDepth();
                }
            }
        }


        Variant imageJPEG = null;
        Variant imageGIF = null;
        Variant imagePNG = null;
        Variant imageTIFF = null;

        // Remove those variants with a pixelDepth not equal to the best
        for (Iterator i = images.iterator(); i.hasNext();) {
            Variant variant = (Variant) i.next();
            ImageMetaData image = (ImageMetaData) variant.getMetaData();

            if (image.getPixelDepth() != bestDepth) {
                i.remove();
            }

View Full Code Here

TOP

Related Classes of com.volantis.mcs.policies.variants.Variant

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.