Example usage for java.util.regex Matcher appendTail

List of usage examples for java.util.regex Matcher appendTail

Introduction

In this page you can find the example usage for java.util.regex Matcher appendTail.

Prototype

public StringBuilder appendTail(StringBuilder sb) 

Source Link

Document

Implements a terminal append-and-replace step.

Usage

From source file:com.hichinaschool.flashcards.libanki.importer.Anki2Importer.java

private String _mungeMedia(long mid, String fields) {
    String[] fs = Utils.splitFields(fields);

    for (int i = 0; i < fs.length; ++i) {
        for (Pattern p : Media.fMediaRegexps) {
            Matcher m = p.matcher(fs[i]);
            StringBuffer sb = new StringBuffer();
            while (m.find()) {
                String fname = m.group(2);
                BufferedInputStream srcData = _srcMediaData(fname);
                BufferedInputStream dstData = _dstMediaData(fname);
                if (srcData == null) {
                    // file was not in source, ignore
                    m.appendReplacement(sb, m.group(0));
                    continue;
                }/* w w w.  ja va2  s . c om*/
                // if model-local file exists from a previous import, use that
                int extPos = fname.lastIndexOf(".");
                if (extPos <= 0) {
                    extPos = fname.length();
                }
                String lname = String.format(Locale.US, "%s_%d%s", fname.substring(0, extPos), mid,
                        fname.substring(extPos));
                if (mDst.getMedia().have(lname)) {
                    m.appendReplacement(sb, m.group(0).replace(fname, lname));
                    continue;
                } else if (dstData == null || compareMedia(srcData, dstData)) { // if missing or the same, pass unmodified
                    // need to copy?
                    if (dstData == null) {
                        _writeDstMedia(fname, srcData);
                    }
                    m.appendReplacement(sb, m.group(0));
                    continue;
                }
                // exists but does not match, so we need to dedupe
                _writeDstMedia(lname, srcData);
                m.appendReplacement(sb, m.group(0).replace(fname, lname));
            }
            m.appendTail(sb);
            fs[i] = sb.toString();
        }
    }
    return fields;
}

From source file:com.ichi2.libanki.importer.Anki2Importer.java

private String _mungeMedia(long mid, String fields) {
    // running splitFields() on every note is fairly expensive and actually not necessary
    //String[] fs = Utils.splitFields(fields);
    //for (int i = 0; i < fs.length; ++i) {

    for (Pattern p : Media.mRegexps) {
        //Matcher m = p.matcher(fs[i]);
        Matcher m = p.matcher(fields);
        StringBuffer sb = new StringBuffer();
        int fnameIdx = Media.indexOfFname(p);
        while (m.find()) {
            String fname = m.group(fnameIdx);
            BufferedInputStream srcData = _srcMediaData(fname);
            BufferedInputStream dstData = _dstMediaData(fname);
            if (srcData == null) {
                // file was not in source, ignore
                m.appendReplacement(sb, m.group(0));
                continue;
            }/*  w  w  w.j a v  a2s. c o  m*/
            // if model-local file exists from a previous import, use that
            int extPos = fname.lastIndexOf(".");
            if (extPos <= 0) {
                extPos = fname.length();
            }
            String lname = String.format(Locale.US, "%s_%d%s", fname.substring(0, extPos), mid,
                    fname.substring(extPos));
            if (mDst.getMedia().have(lname)) {
                m.appendReplacement(sb, m.group(0).replace(fname, lname));
                continue;
            } else if (dstData == null || compareMedia(srcData, dstData)) { // if missing or the same, pass
                                                                            // unmodified
                                                                            // need to copy?
                if (dstData == null) {
                    _writeDstMedia(fname, srcData);
                }
                m.appendReplacement(sb, m.group(0));
                continue;
            }
            // exists but does not match, so we need to dedupe
            _writeDstMedia(lname, srcData);
            m.appendReplacement(sb, m.group(0).replace(fname, lname));
        }
        m.appendTail(sb);
        //fs[i] = sb.toString();
        fields = sb.toString();
    }
    //}
    return fields;
}

From source file:org.etudes.mneme.impl.ExportQtiServiceImpl.java

/**
 * Parses the text and if embed media found then translates the path and adds the file to the zip package.
 * //from  www.  ja  v  a2s  . c o  m
 * @param zip
 *        The zip package
 * @param text
 *        Text
 * @param mediaFiles
 *        List of embedded files found
 * @return The translated Text
 */
protected String translateEmbedData(ZipOutputStream zip, String subFolder, String writeSubFolder, String text,
        List<String> mediaFiles) {
    Pattern p = Pattern.compile("(src|href)[\\s]*=[\\s]*\"([^#\"]*)([#\"])",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    StringBuffer sb = new StringBuffer();
    subFolder = (subFolder == null || subFolder.length() == 0) ? "Resources/" : subFolder;
    Matcher m = p.matcher(text);

    // security advisor
    pushAdvisor();

    while (m.find()) {
        if (m.groupCount() != 3)
            continue;
        String ref = m.group(2);
        if (!ref.contains("/access/mneme/content/"))
            continue;

        String resource_id = ref.replace("/access/mneme", "");
        String resource_name = ref.substring(ref.lastIndexOf("/") + 1);
        resource_name = resource_name.replaceAll("%20", " ");
        resource_name = Validator.escapeResourceName(resource_name);
        mediaFiles.add(subFolder + resource_name);
        m.appendReplacement(sb, m.group(1) + "= \"" + writeSubFolder + resource_name + "\"");

        writeContentResourceToZip(zip, subFolder, resource_id, resource_name);
    }

    popAdvisor();
    m.appendTail(sb);
    return sb.toString();
}

From source file:openthinks.others.htmlunit.PageKeeper.java

private StringBuffer keepStyleReference(String styleCtx) {
    Matcher matcher = RESOURCE_STYLE_REFERENCE_PATTERN.matcher(styleCtx);
    File refDir = new File(keepDir, RESOURCE_STYLE_REFERENCE_DIR);
    if (!refDir.exists())
        refDir.mkdirs();/*from  w  w w  .ja  v  a  2 s  .c om*/
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        String relativeURL = matcher.group(1);
        String styleRefUrl = getFullyQualifiedUrl(relativeURL);
        String styleRefName = getResourceName(styleRefUrl);
        String styleRefCtx = "";
        File keepFile = new File(refDir, styleRefName);
        WebResponse wrp = null;
        try {
            checkIfAlreadExist(keepFile);
            wrp = getResourceResponse(styleRefUrl);
            if (wrp.getContentType().startsWith("image") || wrp.getContentType().startsWith("IMAGE")) {
                ImageInputStream iis = ImageIO.createImageInputStream(wrp.getContentAsStream());
                Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
                ImageReader imageReader = iter.next();
                imageReader.setInput(iis);
                ImageIO.write(imageReader.read(0), imageReader.getFormatName(), keepFile);
            } else {
                styleRefCtx = wrp.getContentAsString("UTF-8");
                store(styleRefCtx, keepFile);
            }
            matcher.appendReplacement(sb, "url(" + RESOURCE_STYLE_REFERENCE_URL + "/" + styleRefName + ")");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            matcher.appendReplacement(sb, "url(" + RESOURCE_STYLE_REFERENCE_URL + "/" + styleRefName + ")");
        } catch (NoSuchElementException e) {
            if (wrp != null) {
                styleRefCtx = wrp.getContentAsString("UTF-8");
                store(styleRefCtx, keepFile);
                matcher.appendReplacement(sb, "url(" + RESOURCE_STYLE_REFERENCE_URL + "/" + styleRefName + ")");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    matcher.appendTail(sb);
    return sb;
}

From source file:org.etudes.mneme.impl.ExportQtiServiceImpl.java

/**
 * /*from   w ww.j a v a 2 s. c  o m*/
 * @param questionDocument
 * @param itemBody
 * @param question
 * @param questionParts
 * @return
 */
public Element getFillBlanksResponseChoices(Document questionDocument, Element itemBody,
        FillBlanksQuestionImpl question, Map<String, Element> questionParts) {
    if (question == null)
        return itemBody;

    // itemBody
    String text = question.getText();

    Pattern p_fillBlanks_curly = Pattern.compile("([^{]*.?)(\\{)([^}]*.?)(\\})",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m_fillBlanks = p_fillBlanks_curly.matcher(text);
    StringBuffer sb = new StringBuffer();

    // in each part look for {} fill in the blank symbol and create render_fib tag
    int count = 1;
    while (m_fillBlanks.find()) {
        String fib = m_fillBlanks.group(1);
        Element textDiv = questionDocument.createElement("div");
        textDiv.setTextContent(fib);
        itemBody.appendChild(textDiv);

        String fib_curly = m_fillBlanks.group(2);
        Element fbInteraction = questionDocument.createElement("textEntryInteraction");
        fbInteraction.setAttribute("responseIdentifier", "RESPONSE" + (count++));
        fbInteraction.setAttribute("expectedLength", Integer.toString(fib_curly.length()));
        itemBody.appendChild(fbInteraction);
        m_fillBlanks.appendReplacement(sb, "");
    }
    m_fillBlanks.appendTail(sb);

    if (sb.length() > 0) {
        Element textDiv = questionDocument.createElement("div");
        textDiv.setTextContent(sb.toString());
        itemBody.appendChild(textDiv);
    }

    // answer
    List<String> correctAnswers = new ArrayList<String>();
    question.parseCorrectAnswers(correctAnswers);

    int responseCount = 1;
    for (String answer : correctAnswers) {
        Element responseDeclaration = createResponseDeclaration(questionDocument, "RESPONSE" + responseCount,
                "single", "string");
        Element correctResponse = questionDocument.createElement("correctResponse");
        Element correctResponseValue = questionDocument.createElement("value");
        answer = FormattedText.unEscapeHtml(answer);
        correctResponseValue.setTextContent(answer);
        correctResponse.appendChild(correctResponseValue);
        responseDeclaration.appendChild(correctResponse);
        questionParts.put("responseDeclaration" + responseCount, responseDeclaration);
        responseCount++;
    }

    Element countDiv = questionDocument.createElement("div");
    countDiv.setTextContent(Integer.toString(responseCount));
    questionParts.put("responseDeclarationCount", countDiv);
    questionParts.put("itemBody", itemBody);
    return itemBody;
}

From source file:uk.ac.ebi.intact.editor.controller.misc.MyNotesController.java

private String replaceACs(String line, Pattern pattern) {
    Matcher matcher = pattern.matcher(line);

    StringBuffer sb = new StringBuffer(line.length() * 2);

    while (matcher.find()) {
        String ac = matcher.group();

        String replacement;//from   ww  w  . j a  va 2s  . c  o  m

        Class aoClass = getMyNotesService().loadClassFromAc(ac);

        if (aoClass == null) {
            addWarningMessage("Accession problem: " + ac,
                    "Some accession numbers in the note could not be auto-linked because there is no object type for "
                            + "that accession, or it does not exist in the database");

            replacement = ac;
        }

        String urlFolderName = null;

        if (IntactPublication.class.isAssignableFrom(aoClass)) {
            urlFolderName = "publication";
        } else if (IntactExperiment.class.isAssignableFrom(aoClass)) {
            urlFolderName = "experiment";
        } else if (IntactInteractionEvidence.class.isAssignableFrom(aoClass)) {
            urlFolderName = "interaction";
        } else if (IntactComplex.class.isAssignableFrom(aoClass)) {
            urlFolderName = "complex";
        } else if (IntactInteractor.class.isAssignableFrom(aoClass)) {
            urlFolderName = "interactor";
        } else if (IntactModelledParticipant.class.isAssignableFrom(aoClass)) {
            urlFolderName = "complex participant";
        } else if (IntactParticipantEvidence.class.isAssignableFrom(aoClass)) {
            urlFolderName = "participant";
        } else if (IntactModelledFeature.class.isAssignableFrom(aoClass)) {
            urlFolderName = "complex feature";
        } else if (IntactFeatureEvidence.class.isAssignableFrom(aoClass)) {
            urlFolderName = "feature";
        } else if (IntactOrganism.class.isAssignableFrom(aoClass)) {
            urlFolderName = "organism";
        } else if (IntactCvTerm.class.isAssignableFrom(aoClass)) {
            urlFolderName = "cvobject";
        } else if (IntactSource.class.isAssignableFrom(aoClass)) {
            urlFolderName = "institution";
        }

        replacement = "<a href=\"" + absoluteContextPath + "/" + urlFolderName + "/" + ac + "\">" + ac + "</a>";

        matcher.appendReplacement(sb, replacement);
    }

    matcher.appendTail(sb);

    return sb.toString();
}

From source file:org.apache.sling.resourceresolver.impl.ResourceResolverImpl.java

private String mangleNamespaces(String absPath) {
    if (factory.isMangleNamespacePrefixes() && absPath != null
            && absPath.contains(MANGLE_NAMESPACE_OUT_SUFFIX)) {
        final Matcher m = MANLE_NAMESPACE_OUT_PATTERN.matcher(absPath);

        final StringBuffer buf = new StringBuffer();
        while (m.find()) {
            final String namespace = m.group(1);
            try {

                // throws if "namespace" is not a registered
                // namespace prefix
                final Session session = getSession();
                if (session != null) {
                    session.getNamespaceURI(namespace);
                    final String replacement = MANGLE_NAMESPACE_IN_PREFIX + namespace
                            + MANGLE_NAMESPACE_IN_SUFFIX;
                    m.appendReplacement(buf, replacement);
                } else {
                    logger.debug("mangleNamespaces: '{}' is not a prefix, not mangling", namespace);
                }//  www  . jav a  2  s. c  o  m

            } catch (final NamespaceException ne) {

                // not a valid prefix
                logger.debug("mangleNamespaces: '{}' is not a prefix, not mangling", namespace);

            } catch (final RepositoryException re) {

                logger.warn("mangleNamespaces: Problem checking namespace '{}'", namespace, re);

            }
        }

        m.appendTail(buf);

        absPath = buf.toString();
    }

    return absPath;
}

From source file:org.apache.sling.resourceresolver.impl.ResourceResolverImpl.java

private String unmangleNamespaces(String absPath) {
    if (factory.isMangleNamespacePrefixes() && absPath.contains(MANGLE_NAMESPACE_IN_PREFIX)) {
        final Matcher m = MANGLE_NAMESPACE_IN_PATTERN.matcher(absPath);
        final StringBuffer buf = new StringBuffer();
        while (m.find()) {
            final String namespace = m.group(1);
            try {

                // throws if "namespace" is not a registered
                // namespace prefix
                final Session session = getSession();
                if (session != null) {
                    session.getNamespaceURI(namespace);
                    final String replacement = MANGLE_NAMESPACE_OUT_PREFIX + namespace
                            + MANGLE_NAMESPACE_OUT_SUFFIX;
                    m.appendReplacement(buf, replacement);
                } else {
                    logger.debug("unmangleNamespaces: '{}' is not a prefix, not unmangling", namespace);
                }//from w w w . j av a2 s . c  o m

            } catch (final NamespaceException ne) {

                // not a valid prefix
                logger.debug("unmangleNamespaces: '{}' is not a prefix, not unmangling", namespace);

            } catch (final RepositoryException re) {

                logger.warn("unmangleNamespaces: Problem checking namespace '{}'", namespace, re);

            }
        }
        m.appendTail(buf);
        absPath = buf.toString();
    }

    return absPath;
}

From source file:org.sakaiproject.lessonbuildertool.service.LessonBuilderEntityProducer.java

private String convertHtmlContent(ContentCopyContext context, String content, String contentUrl,
        Map<Long, Long> itemMap) {

    // this old code (below) seems to have come from an old version of the kerne's reference migrator.
    // It's too complex for me to verify that it's right.
    // At this point the kernel just does string replacements. So I'm going to
    // replace /access/content/group/NNN with the new value
    // and also fix up the dummy references.

    String oldurl = "/access/content/group/" + context.getOldSiteId().replace(" ", "%20");
    String newurl = "/access/content/group/" + context.getNewSiteId().replace(" ", "%20");

    content = content.replace(oldurl, newurl);

    // no point doing this code unless we actually have a dummy url in it
    if (content.indexOf(ITEMDUMMY) >= 0) {

        StringBuffer newcontent = new StringBuffer();
        Matcher matcher = dummyPattern.matcher(content);
        while (matcher.find()) {
            String itemnumber = matcher.group().substring(ITEMDUMMY.length());
            long oldItem = 0;
            try {
                oldItem = Long.parseLong(itemnumber);
                Long newItem = itemMap.get(oldItem);
                if (newItem != null)
                    matcher.appendReplacement(newcontent, ITEMDUMMY + newItem);
            } catch (Exception e) {
            }/*  w w  w .ja v  a2s.com*/
        }
        matcher.appendTail(newcontent);
        content = newcontent.toString();
    }

    if (false) {
        StringBuilder output = new StringBuilder();
        Matcher matcher = attributePattern.matcher(content);
        int contentPos = 0;

        StringBuffer newContent = new StringBuffer();
        while (matcher.find()) {
            String url = matcher.group(3);

            // processUrl does a parse of the URL. It will fail if there is a space
            // in it. But spaces are often used by humans, since they actually work.
            // And a CKEditor bug inserts them. So handle them correctly.
            url = url.replace(" ", "%20");
            url = processUrl(context, url, contentUrl, itemMap);
            // Content up to the match.
            int copyTo = matcher.start(3);
            // Start the second copy after the match.
            int copyFrom = matcher.end(3);
            int copyEnd = matcher.end();

            output.append(content.substring(contentPos, copyTo));
            output.append(url);
            output.append(content.substring(copyFrom, copyEnd));
            contentPos = copyEnd;
        }
        output.append(content.substring(contentPos));
        return output.toString();
    } // end of if false

    return content;

}

From source file:org.etudes.util.XrefHelper.java

/**
 * Replace any embedded references in the html data with the translated, new references listed in translations.
 * //from   w  w  w.j  av  a  2  s  . c om
 * @param data
 *        the html data.
 * @param translations
 *        The translations.
 * @param siteId
 *        The site id.
 * @param parentRef
 *        Reference of the resource that has data as body.
 * @return The translated html data.
 */
@SuppressWarnings("unchecked")
public static String translateEmbeddedReferences(String data, Collection<Translation> translations,
        String siteId, String parentRef) {
    if (data == null)
        return data;
    if (translations == null)
        return data;

    // get our thread-local list of translations made in this thread (if any)
    List<Translation> threadTranslations = (List<Translation>) ThreadLocalManager.get(THREAD_TRANSLATIONS_KEY);

    Pattern p = getPattern();
    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    // process each "harvested" string (avoiding like strings that are not in src= or href= patterns)
    while (m.find()) {
        if (m.groupCount() == 3) {
            String ref = m.group(2);
            String terminator = m.group(3);

            if (ref != null)
                ref = ref.trim();

            // expand to a full reference if relative
            ref = adjustRelativeReference(ref, parentRef);

            // harvest any content hosting reference
            int index = indexContentReference(ref);
            if (index != -1) {
                // except those we don't want to harvest
                if (exception(ref, siteId))
                    index = -1;
            }

            if (index != -1) {
                // save just the reference part (i.e. after the /access);
                String normal = ref.substring(index + 7);

                // deal with %20, &amp;, and other encoded URL stuff
                normal = decodeUrl(normal);

                // translate the normal form
                String translated = normal;
                for (Translation translation : translations) {
                    translated = translation.translate(translated);
                }

                // also translate with our global list
                if (threadTranslations != null) {
                    for (Translation translation : threadTranslations) {
                        translated = translation.translate(translated);
                    }
                }

                // URL encode translated
                String escaped = escapeUrl(translated);

                // if changed, replace
                if (!normal.equals(translated)) {
                    m.appendReplacement(sb, Matcher.quoteReplacement(
                            m.group(1) + "=\"" + ref.substring(0, index + 7) + escaped + terminator));
                }
            }
        }
    }

    m.appendTail(sb);

    return sb.toString();
}