Package org.apache.oro.text.regex

Examples of org.apache.oro.text.regex.StringSubstitution


    // ************************************************************************
    // Replace image CIDs

    HashMap oDocumentImages = new HashMap(23);

    StringSubstitution oSrcSubs = new StringSubstitution();

    Parser oPrsr = Parser.createParser(sHtmlBody, sEncoding);

    String sCid, sSrc;

    try {

      if (sTextBody==null) {
          // ****************************
          // Extract plain text from HTML
          if (DebugFile.trace) DebugFile.writeln("new StringBean()");

          StringBean oStrBn = new StringBean();

          try {
            oPrsr.visitAllNodesWith (oStrBn);
          } catch (ParserException pe) {
          throw new MessagingException(pe.getMessage(), pe);
          }

          sTextBody = oStrBn.getStrings();

          oStrBn = null;
      } // fi (sTextBody==null)

      // *******************************
      // Set plain text alternative part

      oMsgPlainText.setDisposition("inline");
      oMsgPlainText.setText(sTextBody, sCharEnc, "plain");
      // oMsgPlainText.setContent(sTextBody, "text/plain; charset="+sCharEnc);
      if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/alternative).addBodyPart(text/plain)");
      oTextHtmlAlt.addBodyPart(oMsgPlainText);

      // *****************************************
      // Iterate images from HTML and replace CIDs

      NodeList oCollectionList = new NodeList();
      TagNameFilter oImgFilter = new TagNameFilter ("IMG");
      for (NodeIterator e = oPrsr.elements(); e.hasMoreNodes();)
        e.nextNode().collectInto(oCollectionList, oImgFilter);

      final int nImgs = oCollectionList.size();

      if (DebugFile.trace) DebugFile.writeln("NodeList.size() = " + String.valueOf(nImgs));

      for (int i=0; i<nImgs; i++) {

        sSrc = ((ImageTag) oCollectionList.elementAt(i)).extractImageLocn();

        // Keep a reference to every related image name so that the same image is not included twice in the message
        if (!oDocumentImages.containsKey(sSrc)) {

          // Find last slash from image url
          int iSlash = sSrc.lastIndexOf('/');

          // Take image name
          if (iSlash>=0) {
            while (sSrc.charAt(iSlash)=='/') { if (++iSlash==sSrc.length()) break; }
            sCid = sSrc.substring(iSlash);
          }
          else {
            sCid = sSrc;
          }

          //String sUid = Gadgets.generateUUID();
          //sCid = sUid.substring(0,12)+"$"+sUid.substring(12,20)+"$"+sUid.substring(20,28)+"@hipergate.org";

          if (DebugFile.trace) DebugFile.writeln("HashMap.put("+sSrc+","+sCid+")");

          oDocumentImages.put(sSrc, sCid);
        } // fi (!oDocumentImages.containsKey(sSrc))

        try {
          Pattern oPattern = oCompiler.compile(sSrc, Perl5Compiler.SINGLELINE_MASK);
          oSrcSubs.setSubstitution("cid:"+oDocumentImages.get(sSrc));
          if (DebugFile.trace) DebugFile.writeln("Util.substitute([PatternMatcher],"+ sSrc + ",cid:"+oDocumentImages.get(sSrc)+",...)");
          sHtmlBody = Util.substitute(oMatcher, oPattern, oSrcSubs, sHtmlBody);
        } catch (MalformedPatternException neverthrown) { }

      } // next
View Full Code Here


   
    String sMatch = "";
   
    if (DebugFile.trace) DebugFile.writeln("HtmlMomeBodyPart.doSubstitution(..., "+sAttributeName+","+sFormerValue+","+sNewValue);

    StringSubstitution oSrcSubs = new StringSubstitution();

    final String sPattern = "("+sAttributeName.toLowerCase()+"|"+sAttributeName.toUpperCase()+"|"+sAttributeName+")\\s*=\\s*(\"|')?" + sFormerValue + "(\"|')?";

    try {

      if (DebugFile.trace) DebugFile.writeln("Perl5Compiler.compile(\""+sPattern+"\", Perl5Compiler.SINGLELINE_MASK)");
      Pattern oPattern = oCompiler.compile(sPattern, Perl5Compiler.SINGLELINE_MASK);

      if (oMatcher.contains(sBase, oPattern)) {
        MatchResult oMtrs = oMatcher.getMatch();
        if (oMtrs!=null) sMatch = oMtrs.toString();
        if (sMatch.length()==0) {
          oCompiler = new Perl5Compiler();
          oMatcher = new Perl5Matcher();
          oPattern = oCompiler.compile(sPattern, Perl5Compiler.SINGLELINE_MASK);
          if (oMatcher.contains(sBase, oPattern)) {
            oMtrs = oMatcher.getMatch();
            if (oMtrs!=nullsMatch = oMtrs.toString();
          }
        } // fi
        if (sMatch.length()==0)
          throw new ParserException("Match could not be retrieved for pattern " + sPattern);       
        int iDquote = sMatch.indexOf('"');
        int iSquote = sMatch.indexOf("'");
        char cQuote = (char) 0;
        if (iDquote>0 && iSquote>0)
          cQuote = iDquote<iSquote ? (char)34 : (char)39;
        else if (iDquote>0)
          cQuote = (char)34;
        else if (iSquote>0)
          cQuote = (char)39;
    if (cQuote==(char)0)
          oSrcSubs.setSubstitution(sMatch.substring(0,sAttributeName.length())+"="+sNewValue);
        else
          oSrcSubs.setSubstitution(sMatch.substring(0,sAttributeName.length())+"="+cQuote+sNewValue+cQuote);
      if (DebugFile.trace) DebugFile.writeln("Util.substitute("+sPattern+","+sMatch.substring(0,sAttributeName.length())+"="+cQuote+sNewValue+cQuote+")");
      return Util.substitute(oMatcher, oPattern, oSrcSubs, sBase);     
      } else {
        return sBase;
      } // fi (oMatcher.contains())
View Full Code Here

    if (replacement.isString()) {
      String s = replacement.toString();
      if (s.indexOf('$') != -1) {
        substitution = new Perl5Substitution(s);
      } else {
        substitution = new StringSubstitution(s);
      }
    } else {
      substitution = new AnyUtils.FunctionSubstitute(context, replacement);
    }
   
View Full Code Here

    if (replacement.isString()) {
      String s = replacement.toString();
      if (s.indexOf('$') != -1) {
        substitution = new Perl5Substitution(s);
      } else {
        substitution = new StringSubstitution(s);
      }
    } else {
      substitution = new AnyUtils.FunctionSubstitute(context, replacement);
    }
    Perl5Matcher matcher = new Perl5Matcher();
View Full Code Here

      if (DebugFile.trace) DebugFile.writeln("<base href="+sBaseHref+">");
    }
 
      PatternMatcherInput oMatchInput = new PatternMatcherInput(sHtml);
    oPattern = oCompiler.compile("\\x20(src=|background=|background-image:url\\x28)(\"|')?([^'\"\\r\\n]+)(\"|')?(\\x20|\\x29|/|>)", Perl5Compiler.CASE_INSENSITIVE_MASK);
    StringSubstitution oSrcSubs = new StringSubstitution();
    int nMatches = 0;
    while (oMatcher.contains(oMatchInput, oPattern)) {
      nMatches++;
      String sMatch = oMatcher.getMatch().toString();
      String sAttr = oMatcher.getMatch().group(1);
      String sQuo = oMatcher.getMatch().group(2);
      if (sQuo==null) sQuo = "";
      String sSrc = oMatcher.getMatch().group(3);
      if (DebugFile.trace) DebugFile.writeln("Source file found at "+sSrc);
      String sEnd = oMatcher.getMatch().group(5);
      if (!oFiles.contains(sSrc)) oFiles.add(sSrc);
      String sFilename = sSrc.substring(sSrc.replace('\\','/').lastIndexOf('/')+1);
      if (DebugFile.trace)
        DebugFile.writeln("StringSubstitution.setSubstitution("+sMatch+" replace with "+sMatch.substring(0,sAttr.length()+1)+sQuo+sFilename+sQuo+sEnd+")");
      oSrcSubs.setSubstitution(sMatch.substring(0,sAttr.length()+1)+sQuo+sFilename+sQuo+sEnd);
        sHtml = Util.substitute(oReplacer, oCompiler.compile(sMatch), oSrcSubs, sHtml, Util.SUBSTITUTE_ALL);
    } //wend
   
    oMatchInput = new PatternMatcherInput(sHtml);
    oPattern = oCompiler.compile("<link\\x20+(rel=(\"|')?stylesheet(\"|')?\\x20+)?(type=(\"|')?text/css(\"|')?\\x20+)?href=(\"|')?([^'\"\\r\\n]+)(\"|')?");
    while (oMatcher.contains(oMatchInput, oPattern)) {
      nMatches++;
      String sMatch = oMatcher.getMatch().toString();
      String sSrc = oMatcher.getMatch().group(8);
      String sFilename = sSrc.substring(sSrc.replace('\\','/').lastIndexOf('/')+1);
      if (!oFiles.contains(sSrc)) oFiles.add(sSrc);
      if (DebugFile.trace)
        DebugFile.writeln("StringSubstitution.setSubstitution("+sMatch+" replace with "+Gadgets.replace(sMatch, sSrc, sFilename)+")");
      oSrcSubs.setSubstitution(Gadgets.replace(sMatch, sSrc, sFilename));
        sHtml = Util.substitute(oReplacer, oCompiler.compile(sMatch), oSrcSubs, sHtml);    
    } // wend   
   
    if (DebugFile.trace) {
      DebugFile.writeln(String.valueOf(nMatches)+" matches found");
View Full Code Here

            String value = getVariables().get(key);
            if (regexMatch) {
                try {
                    pattern = compiler.compile(value);
                    input = Util.substitute(pm, pattern,
                            new StringSubstitution(FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX),
                            input, Util.SUBSTITUTE_ALL);
                } catch (MalformedPatternException e) {
                    log.warn("Malformed pattern " + value);
                }
            } else {
View Full Code Here

            String value = entry.getValue();
            if (regexMatch) {
                try {
                    pattern = compiler.compile("\\b("+value+")\\b");
                    input = Util.substitute(pm, pattern,
                            new StringSubstitution(FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX),
                            input, Util.SUBSTITUTE_ALL);
                } catch (MalformedPatternException e) {
                    log.warn("Malformed pattern " + value);
                }
            } else {
View Full Code Here

            String value = entry.getValue();
            if (regexMatch) {
                try {
                    pattern = compiler.compile("\\b("+value+")\\b");
                    input = Util.substitute(pm, pattern,
                            new StringSubstitution(FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX),
                            input, Util.SUBSTITUTE_ALL);
                } catch (MalformedPatternException e) {
                    log.warn("Malformed pattern " + value);
                }
            } else {
View Full Code Here

            String value = entry.getValue();
            if (regexMatch) {
                try {
                    pattern = compiler.compile(value);
                    input = Util.substitute(pm, pattern,
                            new StringSubstitution(FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX),
                            input, Util.SUBSTITUTE_ALL);
                } catch (MalformedPatternException e) {
                    log.warn("Malformed pattern " + value);
                }
            } else {
View Full Code Here

            String value = (String) getVariables().get(key);
            if (regexMatch) {
                try {
                    pattern = compiler.compile(value);
                    input = Util.substitute(pm, pattern,
                            new StringSubstitution(FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX),
                            input, Util.SUBSTITUTE_ALL);
                } catch (MalformedPatternException e) {
                    log.warn("Malformed pattern " + value);
                }
            } else {
View Full Code Here

TOP

Related Classes of org.apache.oro.text.regex.StringSubstitution

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.