Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* *********************************************************************
   * This Source Code Form is subject to the terms of the Mozilla Public
   * License, v. 2.0.
   * 
   * If a copy of the MPL was not distributed with this file, You can obtain
   * one at http://mozilla.org/MPL/2.0/.
   * 
   * This Source Code Form is "Incompatible With Secondary Licenses", as
   * defined by the Mozilla Public License, v. 2.0.
  * ********************************************************************* */
 package fiftyone.mobile.detection.handlers;
 
 import java.util.List;
Device detection handler using regular expressions to segment strings before matching specific segments.

Author(s):
51degrees.mobi
Version:
2.2.8.9
 
 public class RegexSegmentHandler extends SegmentHandler {

    
Contains regular expression and weight to apply to each segment of the User Agent String.
 
     public static class RegexSegment {

        
The regular expression to use to get the segment.
 
         private Pattern _pattern;
        
The weight that should be given to the segment.
 
         private int _weight;

        

Returns:
The regular expression to use to get the segment.
 
         public Pattern getPattern() {
             return ;
         }

        

Returns:
The weight that should be given to the segment. The lower the number the greater the significance.
 
         public int getWeight() {
             return ;
         }

        
Constructs a new instance of regular expression Segment.

Parameters:
pattern The regular expression for the segment.
weight The relative weight to apply to the segment.
 
         public RegexSegment(final String patternfinal int weight) {
              = Pattern.compile(pattern);
              = weight;
         }
     }
    
A list of segments to be found and matched by the handler.
 
     private List<RegexSegment_segments = new ArrayList<RegexSegment>();

    

Returns:
A list of the regular expressions used to create segments.
 
     public List<RegexSegmentgetSegments() {
         return ;
     }

    
Constructs a new instance of RegexSegmentHandler

Parameters:
provider Reference to the provider instance the handler will be associated with.
name Name of the handler for debugging purposes.
confidence The confidence this handler should be given compared to others.
checkUAProfs True if UAProfs should be checked.
 
    public RegexSegmentHandler(
            final Provider provider
            final String name
            final int confidence
            final boolean checkUAProfs) {
        super(providernameconfidencecheckUAProfs);
    }

    
Adds a new regular expression to the segment

Parameters:
pattern The regular expression.
weight It's weighting (higher the waiting the more important).
    void addSegment(
            final String pattern
            final int weight) {
        .add(new RegexSegment(patternweight));
    }

    
Returns true if the handler can match the requests User Agent string and at least one valid segment is returned as a segment.

Parameters:
userAgent User Agent to check.
Returns:
True if it can be handled, false if not.
    @Override
    public boolean canHandle(final String userAgent) {
        if (super.canHandle(userAgent) == false) {
            return false;
        }
        for (RegexSegment segment : ) {
            if (segment.getPattern().matcher(userAgent).find()) {
                return true;
            }
        }
        return false;
    }

    
Returns segments for the index specified checking in the stored results first if the StoreSegmentResults constant is enabled.

Parameters:
device The source User Agent String.
index The index of the regular expression to use to get the segments.
Returns:
The list of matching segments.
    @Override
    public List<SegmentcreateSegments(final BaseDeviceInfo devicefinal int index) {
        List<Segmentsegments;
            // Get the handlers data from the device.
            final List<List<Segment>> cachedSegments = device.getHandlerData(this);
            // If the segment does not already exist then add it.
            if (cachedSegments.size() <= index) {
                while (cachedSegments.size() <= index) {
                    cachedSegments.add(new ArrayList<Segment>());
                }
                segments = createSegments(device.getUserAgent(), .get(index));
                cachedSegments.set(indexsegments);
            } else {
                segments = cachedSegments.get(index);
            }
        } else {
            segments = createSegments(device.getUserAgent(), .get(index));
        }
        return segments;
    }

    
Returns the segments from the source string. Where a segment returns nothing a single empty segment will be added.

Parameters:
source String to be matched.
Returns:
A list of segments relating to the String.
    @Override
    public Segments createAllSegments(final String source) {
        final Segments results = new Segments();
        for (RegexSegment segment : ) {
            results.add((ArrayList)createSegments(sourcesegment));
        }
        return results;
    }

    
Finds all matches of the source against the segment. if no matches are found then the empty String is returned.

Parameters:
source the String to be compared.
segment the segment to be compared against.
Returns:
All matches of the source against the segment.
    private List<SegmentcreateSegments(
            final String source
            final RegexSegment segment) {
        boolean matched = false;
        final List<SegmentnewSegments = new ArrayList<Segment>();
        final List<Stringmatches = new ArrayList<String>();
        final Matcher m = segment.getPattern().matcher(source);
        //for each occurence of a match, add the match to the results ArrayList
        while (m.find()) {
            matches.add(m.group());
        }
        // Add a segment for each match found.
        for (String match : matches) {
            newSegments.add(new Segment(matchsegment.getWeight()));
            matched = true;
        }
        if (matched == false) {
            // Add an empty segment to avoid problems of missing segments
            // stopping others being compared correctly.
            newSegments.add(new Segment(""segment.getWeight()));
        }
        return newSegments;
    }
New to GrepCode? Check out our FAQ X