jakarta-oro-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject cvs commit: jakarta-oro/src/java/org/apache/oro/text/java JavaCompiler.java JavaCompilerOptions.java JavaEngine.java JavaMatchResult.java JavaMatcher.java JavaPattern.java
Date Tue, 15 Jun 2004 02:46:00 GMT
dfs         2004/06/14 19:46:00

  Added:       src/java/org/apache/oro/text/java JavaCompiler.java
                        JavaCompilerOptions.java JavaEngine.java
                        JavaMatchResult.java JavaMatcher.java
                        JavaPattern.java
  Log:
  Added classes that wrap the java.util.regex package.
  
  Revision  Changes    Path
  1.1                  jakarta-oro/src/java/org/apache/oro/text/java/JavaCompiler.java
  
  Index: JavaCompiler.java
  ===================================================================
  /* 
   * $Id: JavaCompiler.java,v 1.1 2004/06/15 02:46:00 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * 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 org.apache.oro.text.java;
  
  import java.util.regex.*;
  
  import org.apache.oro.text.regex.*;
  
  /**
   *
   * @version @version@
   * @since 2.1
   */
  
  public final class JavaCompiler implements PatternCompiler {
  
    public org.apache.oro.text.regex.Pattern compile(String pattern)
      throws MalformedPatternException
    {
      return compile(pattern, 0);
    }
  
  
    public org.apache.oro.text.regex.Pattern compile(String pattern, int options)
      throws MalformedPatternException
    {
      try {
        JavaPattern jp = new JavaPattern(pattern);
        return jp;
      } catch(Exception e) {
        // We can't wrap the exception without making MalformedPatternException
        // dependent on J2SE 1.4.
        throw new MalformedPatternException(e.getMessage());
      }
    }
  
  
    public org.apache.oro.text.regex.Pattern compile(char[] pattern)
      throws MalformedPatternException 
    {
      return compile(new String(pattern));
    }
  
  
    public org.apache.oro.text.regex.Pattern compile(char[] pattern, int options)
         throws MalformedPatternException
    {
      return compile(new String(pattern));
    }
  
  }
  
  
  
  1.1                  jakarta-oro/src/java/org/apache/oro/text/java/JavaCompilerOptions.java
  
  Index: JavaCompilerOptions.java
  ===================================================================
  /* 
   * $Id: JavaCompilerOptions.java,v 1.1 2004/06/15 02:46:00 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * 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 org.apache.oro.text.java;
  
  import java.util.regex.Pattern;
  
  import org.apache.oro.text.regex.*;
  
  /**
   * 
   * @version @version@
   * @since 2.1
   */
  public class JavaCompilerOptions implements PatternCompilerOptions {
  
    public int getMask(int option) throws UnsupportedOperationException {
      switch(option) {
      case DEFAULT          : return 0;
      case CASE_INSENSITIVE : return java.util.regex.Pattern.CASE_INSENSITIVE;
      case MULTILINE        : return java.util.regex.Pattern.MULTILINE;
      }
  
      throw new UnsupportedOperationException();
    }
  
  }
  
  
  
  1.1                  jakarta-oro/src/java/org/apache/oro/text/java/JavaEngine.java
  
  Index: JavaEngine.java
  ===================================================================
  /* 
   * $Id: JavaEngine.java,v 1.1 2004/06/15 02:46:00 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * 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 org.apache.oro.text.java;
  
  import org.apache.oro.text.regex.*;
  
  /**
   * 
   * @version @version@
   * @since 2.1
   */
  public class JavaEngine implements PatternMatchingEngine {
  
    private static final JavaCompilerOptions __OPTIONS =
      new JavaCompilerOptions();
  
    public PatternCompiler createCompiler() {
      return new JavaCompiler();
    }
  
    public PatternMatcher createMatcher() {
      return new JavaMatcher();
    }
  
    public PatternCompilerOptions getOptions() {
      return __OPTIONS;
    }
  
  }
  
  
  
  1.1                  jakarta-oro/src/java/org/apache/oro/text/java/JavaMatchResult.java
  
  Index: JavaMatchResult.java
  ===================================================================
  /* 
   * $Id: JavaMatchResult.java,v 1.1 2004/06/15 02:46:00 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * 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 org.apache.oro.text.java;
  
  
  import java.util.regex.*;
  
  import org.apache.oro.text.regex.*;
  
  /**
   *
   * @version @version@
   * @since 2.1
   */
  final class JavaMatchResult extends Perl5MatchResult {
  
    JavaMatchResult(Matcher matcher){
      super(matcher.groupCount());
      _match_ = matcher.group();
      _matchBeginOffset_ = matcher.start();
      for(int i = 0; i < _beginGroupOffset_.length; ++i) {
        _beginGroupOffset_[i] = matcher.start(i) - _matchBeginOffset_;
        _endGroupOffset_[i]   = matcher.end(i) - _matchBeginOffset_;
      }
    }
  
  }
  
  
  
  1.1                  jakarta-oro/src/java/org/apache/oro/text/java/JavaMatcher.java
  
  Index: JavaMatcher.java
  ===================================================================
  /* 
   * $Id: JavaMatcher.java,v 1.1 2004/06/15 02:46:00 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * 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 org.apache.oro.text.java;
  
  import java.nio.*;
  import java.util.regex.*;
  
  import org.apache.oro.text.regex.*;
  
  /**
   *
   * @version @version@
   * @since 2.1
   */
  public final class JavaMatcher implements PatternMatcher {
  
    private Matcher __matcher;
  
    public JavaMatcher() {
      __matcher = null;
    }
  
    /** Currently throws an UnsupportedOperationException. */
    public boolean matchesPrefix(char[] input,
                                 org.apache.oro.text.regex.Pattern pattern,
                                 int offset)
    {
      throw new UnsupportedOperationException();
    }
  
    /** Currently throws an UnsupportedOperationException. */
    public boolean matchesPrefix(String input,
                                 org.apache.oro.text.regex.Pattern pattern)
    {
      throw new UnsupportedOperationException();
    }
  
    /** Currently throws an UnsupportedOperationException. */
    public boolean matchesPrefix(char[] input,
                                 org.apache.oro.text.regex.Pattern pattern)
    {
      throw new UnsupportedOperationException();
    }
  
    /** Currently throws an UnsupportedOperationException. */
    public boolean matchesPrefix(PatternMatcherInput input,
                                 org.apache.oro.text.regex.Pattern pattern)
    {
      throw new UnsupportedOperationException();
    }
  
    /** Currently throws an UnsupportedOperationException. */
    public boolean matches(String input,
                           org.apache.oro.text.regex.Pattern pattern)
    {
      throw new UnsupportedOperationException();
    }
  
    /** Currently throws an UnsupportedOperationException. */
    public boolean matches(char[] input,
                           org.apache.oro.text.regex.Pattern pattern)
    {
      throw new UnsupportedOperationException();
    }
  
  
    /** Currently throws an UnsupportedOperationException. */
    public boolean matches(PatternMatcherInput input,
                           org.apache.oro.text.regex.Pattern pattern)
    {
      throw new UnsupportedOperationException();
    }
  
  
    private boolean __contains(Matcher matcher) {
      boolean matched;
  
      matched = matcher.find();
  
      if(matched)
        __matcher = matcher;
      else
        __matcher = null;
  
      return matched;
    }
  
    // These wrappers are not the height of efficiency (java.util.regex.Matcher
    // instances are not reused) because of the quirkiness of java.util.regex.
    // Some time should be spent on improving their efficiency in the future.
    public boolean contains(String input,
                            org.apache.oro.text.regex.Pattern pattern)
    {
      JavaPattern jp = (JavaPattern)pattern;
      return __contains(jp._matcher(input));
    }
  
  
    public boolean contains(char[] input,
                            org.apache.oro.text.regex.Pattern pattern)
    {
      JavaPattern jp = (JavaPattern)pattern;
      return __contains(jp._matcher(CharBuffer.wrap(input)));
    }
  
  
    public boolean contains(PatternMatcherInput input,
                            org.apache.oro.text.regex.Pattern pattern)
    {
      JavaPattern jp    = (JavaPattern)pattern;
      CharBuffer buffer = 
        CharBuffer.wrap(input.getBuffer(), input.getCurrentOffset(),
                        input.getEndOffset() - input.getCurrentOffset());
      Matcher matcher = jp._matcher(buffer);
      boolean matched = __contains(matcher);
  
      if(matched) {
        input.setCurrentOffset(matcher.end());
        input.setMatchOffsets(matcher.start(), matcher.end());
      } else {
        input.setCurrentOffset(input.getEndOffset());
        input.setMatchOffsets(-1, -1);
      }
  
      return matched;
    }
  
  
    public MatchResult getMatch() {
      if(__matcher == null)
        return null;
      return new JavaMatchResult(__matcher);
    }
  }
  
  
  
  1.1                  jakarta-oro/src/java/org/apache/oro/text/java/JavaPattern.java
  
  Index: JavaPattern.java
  ===================================================================
  /* 
   * $Id: JavaPattern.java,v 1.1 2004/06/15 02:46:00 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * 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 org.apache.oro.text.java;
  
  import java.util.regex.*;
  
  import org.apache.oro.text.regex.*;
  
  /**
   *
   * @version @version@
   * @since 2.1
   */
  public final class JavaPattern
    implements org.apache.oro.text.regex.Pattern, java.io.Serializable, Cloneable
  {
    String _expression;
    java.util.regex.Pattern _pattern;
  
    JavaPattern(String expression, int options)
      throws IllegalArgumentException, PatternSyntaxException
    {
      _expression = expression;
      _pattern = java.util.regex.Pattern.compile(expression, options);
    }
  
    JavaPattern(String expression)
      throws IllegalArgumentException, PatternSyntaxException
    {
      this(expression, 0);
    }
  
    Matcher _matcher(CharSequence seq) {
      return _pattern.matcher(seq);
    }
  
    public String getPattern() { return _expression; }
  
    public int getOptions()    { return _pattern.flags(); }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: oro-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: oro-dev-help@jakarta.apache.org


Mime
View raw message