Example usage for java.util.regex Pattern DOTALL

List of usage examples for java.util.regex Pattern DOTALL

Introduction

In this page you can find the example usage for java.util.regex Pattern DOTALL.

Prototype

int DOTALL

To view the source code for java.util.regex Pattern DOTALL.

Click Source Link

Document

Enables dotall mode.

Usage

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

/**
 * // w  w w  . java  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:com.moviejukebox.plugin.ImdbPlugin.java

/**
 * Search for the IMDB Id in the NFO file
 *
 * @param nfo/*from   w w w . ja va 2s. c o  m*/
 * @param movie
 * @return
 */
private static String searchIMDB(String nfo, Movie movie) {
    final int flags = Pattern.CASE_INSENSITIVE | Pattern.DOTALL;
    String imdbPattern = ")[\\W].*?(tt\\d{7})";
    // Issue 1912 escape special regex characters in title
    String title = Pattern.quote(movie.getTitle());
    String id = UNKNOWN;

    Pattern patternTitle;
    Matcher matchTitle;

    try {
        patternTitle = Pattern.compile("(" + title + imdbPattern, flags);
        matchTitle = patternTitle.matcher(nfo);
        if (matchTitle.find()) {
            id = matchTitle.group(2);
        } else {
            String dir = FileTools.getParentFolderName(movie.getFile());
            Pattern patternDir = Pattern.compile("(" + dir + imdbPattern, flags);
            Matcher matchDir = patternDir.matcher(nfo);
            if (matchDir.find()) {
                id = matchDir.group(2);
            } else {
                String strippedNfo = nfo.replaceAll("(?is)[^\\w\\r\\n]", "");
                String strippedTitle = title.replaceAll("(?is)[^\\w\\r\\n]", "");
                Pattern patternStrippedTitle = Pattern.compile("(" + strippedTitle + imdbPattern, flags);
                Matcher matchStrippedTitle = patternStrippedTitle.matcher(strippedNfo);
                if (matchStrippedTitle.find()) {
                    id = matchTitle.group(2);
                } else {
                    String strippedDir = dir.replaceAll("(?is)[^\\w\\r\\n]", "");
                    Pattern patternStrippedDir = Pattern.compile("(" + strippedDir + imdbPattern, flags);
                    Matcher matchStrippedDir = patternStrippedDir.matcher(strippedNfo);
                    if (matchStrippedDir.find()) {
                        id = matchTitle.group(2);
                    }
                }
            }
        }
    } catch (Exception error) {
        LOG.error("Error locating the IMDb ID in the nfo file for {}", movie.getBaseFilename());
        LOG.error(error.getMessage());
    }

    return StringUtils.trim(id);
}

From source file:com.ikanow.infinit.e.harvest.enrichment.custom.UnstructuredAnalysisHarvester.java

private static String extractRegexFromXpath(String original_xpath) {
    Pattern addedRegex = Pattern.compile("regex\\(.*\\)\\s*$", Pattern.MULTILINE | Pattern.DOTALL);
    Matcher matcher = addedRegex.matcher(original_xpath);
    boolean matchFound = matcher.find();

    if (matchFound) {
        try {/*  w  ww.j a v  a2  s. c  o  m*/
            return matcher.group();
        } catch (Exception e) {
            return null;
        }
    }
    return null;

}

From source file:net.java.sip.communicator.impl.gui.main.chat.ChatConversationPanel.java

/**
 *
 * @param elementId/*w  ww  .j  a va  2  s  . c  o  m*/
 * @param message
 * @return
 */
private String getElementContent(String elementId, String message) {
    Pattern p = Pattern.compile(
            ".*(<div.*id=[\\\"']" + ChatHtmlUtils.MESSAGE_TEXT_ID + elementId + "[\\\"'].*?</div>)",
            Pattern.DOTALL);

    Matcher m = p.matcher(message);

    if (m.find()) {
        return m.group(1);
    }

    return null;
}

From source file:com.enonic.cms.web.portal.services.UserServicesProcessor.java

private static String replaceKeys(ExtendedMap formItems, String inText, String[] excludeKeys) {

    String outText = inText;/*w w w  .  jav a 2s  .  com*/

    for (Object o : formItems.keySet()) {
        String key = (String) o;

        Pattern p = Pattern.compile(".*%" + key + "%.*", Pattern.DOTALL);
        Matcher m = p.matcher(outText);

        if ((excludeKeys == null || !ArrayUtil.arrayContains(key, excludeKeys)) && m.matches()
                && formItems.containsKey(key)) {

            String regexp = "%" + key + "%";
            outText = outText.replaceAll(regexp, formItems.getString(key, ""));
        }
    }

    return outText;
}

From source file:com.startupbidder.dao.MockDataBuilder.java

private List<Listing> getStartuplyListings(int fromId, int toId) {
    List<Listing> listings = new ArrayList<Listing>();
    List<String> startuplyIds = getStartuplyIds();
    log.info("Loading " + startuplyIds.size() + " Startuply listings");

    try {//  w w w .j  ava  2  s . c  o  m
        Pattern namePattern = Pattern.compile("<h1 id=\"companyNameHeader\"[^>]*>([^<]*)", Pattern.MULTILINE);
        Pattern websitePattern = Pattern.compile("<a href=\"http://([^\"]*)\">\\1", Pattern.MULTILINE);
        Pattern addressPattern = Pattern.compile(
                "<table id=\"branchTable\".*<td class=\"SortedColumn\">[^<]*</td>\\s*<td[^>]*>([^<]*)",
                Pattern.MULTILINE | Pattern.DOTALL);
        Pattern logoPattern = Pattern.compile("<img\\s+src=\"([^\"]*)\"\\s+id=\"ctl00_Content_Logo\"",
                Pattern.MULTILINE);
        Pattern industriesPattern = Pattern.compile(
                "<div[^>]*>Industries</div>\\s*<div[^>]*>\\s*<a[^>]*>([^<]*)</a>\\s*", Pattern.MULTILINE);
        Pattern industries2Pattern = Pattern.compile("\\s*,\\s*<a[^>]*>([^<]*)</a>", Pattern.MULTILINE);
        Pattern missionPattern = Pattern.compile("<h1[^>]*>[^<]* Mission</h1>\\s*<div[^>]*>\\s*([^<]*)",
                Pattern.MULTILINE);
        Pattern mantraPattern = Pattern.compile("([^\\.]+)");
        Pattern productsPattern = Pattern.compile("<h1[^>]*>[^<]* Products</h1>\\s*<div[^>]*>\\s*([^<]*)",
                Pattern.MULTILINE);
        Pattern teamPattern = Pattern.compile("<h1[^>]*>[^<]* Team</h1>\\s*<div[^>]*>\\s*([^<]*)",
                Pattern.MULTILINE);
        Pattern lifePattern = Pattern.compile("<h1[^>]*>Life [^<]</h1>\\s*<div[^>]*>\\s*([^<]*)",
                Pattern.MULTILINE);
        int counter = 0;
        for (String startuplyId : startuplyIds) {
            counter++;
            if (counter <= fromId || counter >= toId) {
                continue;
            }

            String startuplyPath = STARTUPLY_ROOT + "/Companies/" + startuplyId + ".aspx";
            StartuplyCache startuplyCache = null;
            try {
                startuplyCache = getOfy().get(StartuplyCache.class, startuplyPath);
            } catch (NotFoundException e) {
                ;
            }
            if (startuplyCache == null) {
                try {
                    URL url = new URL(startuplyPath);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoOutput(true);
                    connection.setRequestMethod("GET");
                    StringWriter stringWriter = new StringWriter();
                    IOUtils.copy(connection.getInputStream(), stringWriter, "UTF-8");
                    String startuplyPage = stringWriter.toString();
                    connection.disconnect();
                    if (!StringUtils.isEmpty(startuplyPage)) {
                        startuplyPage = startuplyPage.replaceAll("<br />", ""); // messes up multiline desc if we don't do this
                        startuplyCache = new StartuplyCache(startuplyPath, startuplyPage);
                        getOfy().put(startuplyCache);
                    }
                } catch (Exception e) {
                    log.log(Level.WARNING, "Exception while importing Startuply startup: " + startuplyId, e);
                }
            }
            if (startuplyCache == null) {
                log.info("Could not load Startuply cache for id: " + startuplyId);
            } else if (StringUtils.isEmpty(startuplyCache.page)) {
                log.info("Unable to import, empty response for Startuply cache page for id: " + startuplyId);
            } else {
                try {
                    //log.info(startuplyCache.page);
                    String name = "";
                    Matcher nameMatcher = namePattern.matcher(startuplyCache.page);
                    if (nameMatcher.find()) {
                        name = nameMatcher.group(1);
                    }
                    String address = "";
                    Matcher addressMatcher = addressPattern.matcher(startuplyCache.page);
                    if (addressMatcher.find()) {
                        address = addressMatcher.group(1);
                    }
                    String website = "";
                    Matcher websiteMatcher = websitePattern.matcher(startuplyCache.page);
                    if (websiteMatcher.find()) {
                        website = "http://" + websiteMatcher.group(1);
                    }
                    String logo = "";
                    Matcher logoMatcher = logoPattern.matcher(startuplyCache.page);
                    if (logoMatcher.find()) {
                        logo = STARTUPLY_ROOT + logoMatcher.group(1);
                    }
                    String industries = "";
                    Matcher industriesMatcher = industriesPattern.matcher(startuplyCache.page);
                    if (industriesMatcher.find()) {
                        industries = industriesMatcher.group(1);
                        industriesMatcher.usePattern(industries2Pattern);
                        while (industriesMatcher.find()) {
                            industries += " " + industriesMatcher.group(1);
                        }
                    }
                    String description = "";
                    String mantra = "";
                    Matcher missionMatcher = missionPattern.matcher(startuplyCache.page);
                    if (missionMatcher.find()) {
                        description = missionMatcher.group(1);
                        Matcher mantraMatcher = mantraPattern.matcher(description);
                        if (mantraMatcher.find()) {
                            mantra = mantraMatcher.group(1);
                        }
                    }
                    Matcher productsMatcher = productsPattern.matcher(startuplyCache.page);
                    if (productsMatcher.find()) {
                        String products = productsMatcher.group(1);
                        description += " " + products;
                    }
                    Matcher teamMatcher = teamPattern.matcher(startuplyCache.page);
                    if (teamMatcher.find()) {
                        String team = teamMatcher.group(1);
                        description += " " + team;
                    }
                    Matcher lifeMatcher = lifePattern.matcher(startuplyCache.page);
                    if (lifeMatcher.find()) {
                        String life = lifeMatcher.group(1);
                        description += " " + life;
                    }

                    if (StringUtils.isEmpty(name)) {
                        log.info("Unable to import, couldn't find name for Startuply id: " + startuplyId);
                    } else {
                        String type = bestGuessListingType(name, industries, description);
                        //log.info("Matched name:[" + name + "] address:["+address + "] website:["+website + "] logo:["+logo + "] industries:["+industries+"] mantra:["+mantra+"] description:["+description+"]");
                        int askamt = 5 * new Random().nextInt(20) * 1000;
                        int askpct = 5 + 5 * new Random().nextInt(9);
                        if (askamt < 10000) {
                            askamt = 0;
                        }
                        if (StringUtils.isEmpty(mantra)) {
                            mantra = industries;
                        }
                        if (StringUtils.isEmpty(description)) {
                            description = "In summary, " + name + " is a great company in the " + industries
                                    + " space.";
                        }
                        Listing listing = prepareListing(STARTUPLY, // DtoToVoConverter.convert(user),
                                name, Listing.State.ACTIVE, type, askamt, askpct, mantra, description, website,
                                null, null, logo, address);
                        listings.add(listing);
                        //log.info("Added Startuply listing: "+listing);
                        log.info("Added Startuply listing " + counter + " of " + startuplyIds.size() + " name: "
                                + name);
                    }
                } catch (Exception e) {
                    log.log(Level.WARNING, "Exception while importing Startuply startup: " + startuplyId, e);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return listings;
}

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

/**
 * Creates elements for all embed media with in itembody element. Use this for question text
 * //  w  ww . ja  v a 2  s . com
 * @param zip
 * @param subFolder
 * @param text
 * @param itemBody
 * @param mediaFiles
 * @return
 */
private Element translateEmbedData(ZipOutputStream zip, String subFolder, String text, Element itemBody,
        List<String> mediaFiles, Document questionDocument) {
    if (text == null || text.length() == 0)
        return itemBody;

    Element media = null;
    try {
        Pattern pa = Pattern.compile("<(img|a|embed)\\s+.*?/*>",
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

        // TODO: write all attributes
        Pattern p_srcAttribute = Pattern.compile("(src|href)[\\s]*=[\\s]*\"([^#\"]*)([#\"])",
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
        Matcher m = pa.matcher(text);
        StringBuffer sb = new StringBuffer();
        subFolder = (subFolder == null || subFolder.length() == 0) ? "Resources/" : subFolder;
        String embedSubFolder = "Resources/";
        int start = 0;

        while (m.find()) {
            int startIdx = m.start();

            String img_content = m.group(0);
            Matcher m_src = p_srcAttribute.matcher(img_content);
            if (m_src.find()) {
                String ref = m_src.group(2);
                if (!ref.contains("/access/mneme/content/"))
                    continue;

                Element div = questionDocument.createElement("div");
                if (startIdx <= text.length()) {
                    String divText = text.substring(start, startIdx);
                    div.setTextContent(divText);
                    start = m.end();
                }
                ref = ref.replaceAll("%20", " ");
                String resource_id = ref.replace("/access/mneme", "");
                String embedFileName = ref.substring(ref.lastIndexOf("/") + 1);
                ref = subFolder + embedFileName;
                mediaFiles.add(ref);

                media = questionDocument.createElement(m.group(1));
                if ("a".equalsIgnoreCase(m.group(1)))
                    media.setAttribute("target", "_blank");
                media.setAttribute(m_src.group(1), embedSubFolder + embedFileName);
                m.appendReplacement(sb, "");

                String fileName = Validator.getFileName(resource_id);
                fileName = fileName.replaceAll("%20", " ");
                fileName = Validator.escapeResourceName(fileName);
                writeContentResourceToZip(zip, subFolder, resource_id, fileName);
                itemBody.appendChild(div);
                itemBody.appendChild(media);
            }
        }
        m.appendTail(sb);
        if (start > 0 && start < text.length()) {
            Element div = questionDocument.createElement("div");
            div.setTextContent(text.substring(start));
            itemBody.appendChild(div);
        }
        return itemBody;
    } catch (Exception e) {
        M_log.debug("error in translating embed up blank img tags:" + e.getMessage());
    }
    return itemBody;
}

From source file:org.openmrs.module.htmlformentry.WorkflowStateTagTest.java

@Test
public void checkboxShouldAppearCheckedIfCurrentlyInSpecifiedState() throws Exception {

    transitionToState(START_STATE, FURTHER_PAST_DATE);
    transitionToState(MIDDLE_STATE, PAST_DATE);

    new RegressionTestHelper() {

        @Override//  w  ww  . j a v  a 2  s .co m
        public String getFormName() {
            return XML_CHECKBOX_FORM_NAME;
        }

        @Override
        public void testBlankFormHtml(String html) {
            Pattern p = Pattern.compile(".*checked=\"true\"/><label for=\".*\">MIDDLE STATE.*",
                    Pattern.MULTILINE | Pattern.DOTALL);
            Assert.assertTrue("Checkbox should be checked: " + html, p.matcher(html).matches());
        }

    }.run();
}

From source file:com.ikanow.infinit.e.harvest.enrichment.custom.UnstructuredAnalysisHarvester.java

private static Pattern createRegex(String regEx, String flags) {
    int nflags = 0;

    if (null != flags) {
        for (int i = 0; i < flags.length(); ++i) {
            char c = flags.charAt(i);
            switch (c) {
            case 'm':
                nflags |= Pattern.MULTILINE;
                break;
            case 'i':
                nflags |= Pattern.CASE_INSENSITIVE;
                break;
            case 'd':
                nflags |= Pattern.DOTALL;
                break;
            case 'u':
                nflags |= Pattern.UNICODE_CASE;
                break;
            case 'n':
                nflags |= Pattern.UNIX_LINES;
                break;
            }/*from ww w . ja  v  a 2 s  .  com*/
        }
    }
    return Pattern.compile(regEx, nflags);
}

From source file:com.tao.realweb.util.StringUtil.java

/** 
* html //w  ww  .j a va2 s .c  om
*  
* @param htmlstr 
* @return 
*/
public static String removeHtmlTag(String htmlstr) {
    Pattern pat = Pattern.compile("\\s*<.*?>\\s*",
            Pattern.DOTALL | Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); // \\s?[s|Sc|Cr|Ri|Ip|Pt|T]  
    Matcher m = pat.matcher(htmlstr);
    String rs = m.replaceAll("");
    rs = rs.replaceAll(" ", " ");
    rs = rs.replaceAll("<", "<");
    rs = rs.replaceAll(">", ">");
    return rs;
}