Example usage for org.apache.commons.lang.text StrMatcher doubleQuoteMatcher

List of usage examples for org.apache.commons.lang.text StrMatcher doubleQuoteMatcher

Introduction

In this page you can find the example usage for org.apache.commons.lang.text StrMatcher doubleQuoteMatcher.

Prototype

public static StrMatcher doubleQuoteMatcher() 

Source Link

Document

Returns a matcher which matches the double quote character.

Usage

From source file:org.intermine.web.struts.BuildBagAction.java

/**
 * Action for creating a bag of InterMineObjects or Strings from identifiers in text field.
 *
 * @param mapping The ActionMapping used to select this instance
 * @param form The optional ActionForm bean for this request (if any)
 * @param request The HTTP request we are processing
 * @param response The HTTP response we are creating
 * @return an ActionForward object defining where control goes next
 * @exception Exception if the application business logic throws
 *  an exception/*from   w  w  w . j a v a2s .  c o  m*/
 */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession();
    final InterMineAPI im = SessionMethods.getInterMineAPI(session);
    ServletContext servletContext = request.getSession().getServletContext();
    Properties webProperties = (Properties) servletContext.getAttribute(Constants.WEB_PROPERTIES);
    BuildBagForm buildBagForm = (BuildBagForm) form;

    String type = buildBagForm.getType();

    if (StringUtils.isEmpty(type)) {
        recordError(new ActionMessage("bagBuild.typeNotSet"), request);
        return mapping.findForward("bags");
    }

    BagQueryRunner bagRunner = im.getBagQueryRunner();

    int maxBagSize = WebUtil.getIntSessionProperty(session, "max.bag.size", 100000);
    Profile profile = SessionMethods.getProfile(session);
    if (profile == null || profile.getUsername() == null) {
        int defaultMaxNotLoggedSize = 3;
        maxBagSize = WebUtil.getIntSessionProperty(session, "max.bag.size.notloggedin",
                defaultMaxNotLoggedSize);
    }
    BufferedReader reader = null;
    FormFile formFile = buildBagForm.getFormFile();

    /*
     * FormFile used from Struts works a bit strangely.
     * 1. Although the file does't exist formFile.getInputStream() doesn't
     * throw FileNotFoundException.
     * 2. When user specified empty file path or very invalid file path,
     * like file path not starting at '/' then formFile.getFileName() returns empty string.
     */
    if (formFile != null && formFile.getFileName() != null && formFile.getFileName().length() > 0) {
        // attach file name as the name of the bag
        String fileName = formFile.getFileName();
        // strip suffix
        Integer lastPos = new Integer(fileName.lastIndexOf('.'));
        if (lastPos.intValue() > 0) {
            fileName = fileName.substring(0, lastPos.intValue());
        }
        // replace underscores
        fileName = fileName.replaceAll("_", " ");
        // attach
        request.setAttribute("bagName", fileName);

        String mimetype = formFile.getContentType();
        if (!"application/octet-stream".equals(mimetype) && !mimetype.startsWith("text")) {
            recordError(new ActionMessage("bagBuild.notText", mimetype), request);
            return mapping.findForward("bags");
        }
        if (formFile.getFileSize() == 0) {
            recordError(new ActionMessage("bagBuild.noBagFileOrEmpty"), request);
            return mapping.findForward("bags");
        }
        reader = new BufferedReader(new InputStreamReader(formFile.getInputStream()));
    } else if (buildBagForm.getText() != null && buildBagForm.getText().length() != 0) {
        String trimmedText = buildBagForm.getText().trim();
        if (trimmedText.length() == 0) {
            recordError(new ActionMessage("bagBuild.noBagPaste"), request);
            return mapping.findForward("bags");
        }
        reader = new BufferedReader(new StringReader(trimmedText));
    } else {
        recordError(new ActionMessage("bagBuild.noBagFile"), request);
        return mapping.findForward("bags");
    }

    reader.mark(READ_AHEAD_CHARS);

    char[] buf = new char[READ_AHEAD_CHARS];

    int read = reader.read(buf, 0, READ_AHEAD_CHARS);

    for (int i = 0; i < read; i++) {
        if (buf[i] == 0) {
            recordError(new ActionMessage("bagBuild.notText", "binary"), request);
            return mapping.findForward("bags");
        }
    }

    reader.reset();

    String thisLine;
    List<String> list = new ArrayList<String>();
    int elementCount = 0;
    while ((thisLine = reader.readLine()) != null) {
        // append whitespace to valid delimiters
        String bagUploadDelims = (String) webProperties.get("list.upload.delimiters") + " ";
        StrMatcher matcher = StrMatcher.charSetMatcher(bagUploadDelims);
        StrTokenizer st = new StrTokenizer(thisLine, matcher, StrMatcher.doubleQuoteMatcher());
        while (st.hasNext()) {
            String token = st.nextToken();
            list.add(token);
            elementCount++;
            if (elementCount > maxBagSize) {
                ActionMessage actionMessage = null;
                if (profile == null || profile.getUsername() == null) {
                    actionMessage = new ActionMessage("bag.bigNotLoggedIn", new Integer(maxBagSize));
                } else {
                    actionMessage = new ActionMessage("bag.tooBig", new Integer(maxBagSize));
                }
                recordError(actionMessage, request);
                return mapping.findForward("bags");
            }
        }
    }
    BagQueryResult bagQueryResult = bagRunner.search(type, list, buildBagForm.getExtraFieldValue(), false,
            buildBagForm.getCaseSensitive());
    session.setAttribute("bagQueryResult", bagQueryResult);
    request.setAttribute("bagType", type);
    request.setAttribute("bagExtraFilter", buildBagForm.getExtraFieldValue());
    //buildNewBag used by jsp to set editable the bag name field
    request.setAttribute("buildNewBag", "true");
    return mapping.findForward("bagUploadConfirm");
}

From source file:org.intermine.webservice.server.lists.ListUploadService.java

@Override
protected void makeList(final ListInput input, final String type, final Profile profile,
        final Set<String> temporaryBagNamesAccumulator) throws Exception {

    if (StringUtils.isBlank(type)) {
        throw new BadRequestException("No list type provided");
    }//from   w ww .j a v a 2 s.co  m

    if (input.doReplace()) {
        ListServiceUtils.ensureBagIsDeleted(profile, input.getListName());
    }
    if (profile.getCurrentSavedBags().containsKey(input.getListName())) {
        throw new BadRequestException(
                "Attempt to overwrite an existing bag - name: '" + input.getListName() + "'");
    }

    final StrMatcher matcher = getMatcher();

    final BufferedReader r = getReader(request);
    final Set<String> ids = new LinkedHashSet<String>();
    final Set<String> unmatchedIds = new HashSet<String>();

    final InterMineBag tempBag = profile.createBag(input.getTemporaryListName(), type, input.getDescription(),
            im.getClassKeys());
    String line;
    while ((line = r.readLine()) != null) {
        final StrTokenizer st = new StrTokenizer(line, matcher, StrMatcher.doubleQuoteMatcher());
        while (st.hasNext()) {
            final String token = st.nextToken();
            ids.add(token);
        }
        if (ids.size() >= BAG_QUERY_MAX_BATCH_SIZE) {
            addIdsToList(ids, tempBag, type, input.getExtraValue(), unmatchedIds);
            ids.clear();
        }
    }
    if (ids.size() > 0) {
        addIdsToList(ids, tempBag, type, input.getExtraValue(), unmatchedIds);
    }

    setListSize(tempBag.size());

    for (final Iterator<String> i = unmatchedIds.iterator(); i.hasNext();) {
        final List<String> row = new ArrayList<String>(Arrays.asList(i.next()));
        if (i.hasNext()) {
            row.add("");
        }
        output.addResultItem(row);
    }

    if (!input.getTags().isEmpty()) {
        im.getBagManager().addTagsToBag(input.getTags(), tempBag, profile);
    }
    profile.renameBag(input.getTemporaryListName(), input.getListName());
}