Example usage for org.apache.commons.lang3 StringUtils substringBefore

List of usage examples for org.apache.commons.lang3 StringUtils substringBefore

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils substringBefore.

Prototype

public static String substringBefore(final String str, final String separator) 

Source Link

Document

Gets the substring before the first occurrence of a separator.

Usage

From source file:org.mayocat.rest.Reference.java

public static Reference valueOf(String serialized) {
    if (serialized.indexOf('@') < 1 || serialized.indexOf('@') >= (serialized.length() - 1)) {
        throw new WebApplicationException(
                Response.status(Response.Status.BAD_REQUEST).entity("Invalid reference").build());
    }/* ww w  .  ja  v a2 s  .c  om*/

    String entitySlug = StringUtils.substringBefore(serialized, "@");
    String tenantSlug = StringUtils.substringAfter(serialized, "@");

    return new Reference(entitySlug, tenantSlug);
}

From source file:org.noroomattheinn.visibletesla.data.ChargeStore.java

@Override
protected String filterSubmissionData(String jsonRep) {
    // Strip the closing curly to prepare to add more fields
    jsonRep = StringUtils.substringBefore(jsonRep, "}");
    // Concatenate the extra fields and put back the closing curly
    return String.format("%s, \"battery\": \"%s\", \"uuid\": \"%s\" }", jsonRep, batteryType, uuid);
}

From source file:org.noroomattheinn.visibletesla.data.RestStore.java

@Override
protected String filterSubmissionData(String jsonRep) {
    // Strip the closing curly to prepare to add more fields
    jsonRep = StringUtils.substringBefore(jsonRep, "}");
    // Concatenate the extra fields and put back the closing curly
    return String.format("%s, \"uuid\": \"%s\" }", jsonRep, uuid);
}

From source file:org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter.java

@Override
public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
    Preconditions.checkNotNull(storage, "Storage file is null");

    String content = Files.toString(storage, ENCODING);
    if (numberOfStoredBackups == Integer.MAX_VALUE) {
        resetLastConfig(content);//from w  w w  .  j a  va  2  s .c  om
        persistLastConfig(holder);
    } else {
        if (numberOfStoredBackups == 1) {
            Files.write("", storage, ENCODING);
            persistLastConfig(holder);
        } else {
            int count = StringUtils.countMatches(content, SEPARATOR_S);
            if ((count + 1) < numberOfStoredBackups) {
                resetLastConfig(content);
                persistLastConfig(holder);
            } else {
                String contentSubString = StringUtils.substringBefore(content, SEPARATOR_E);
                contentSubString = contentSubString.concat(SEPARATOR_E_PURE);
                content = StringUtils.substringAfter(content, contentSubString);
                resetLastConfig(content);
                persistLastConfig(holder);
            }
        }
    }
}

From source file:org.openhab.binding.ACDBCommon.db.DBManager.java

/**
 * update//from  w  ww  .  j  a  va 2s  . com
 *
 * @param updateSql
 * @param value
 * @throws Exception
 */
public static void update(String updateSql, String dateValue) throws Exception {
    SqlResult sqlResult = sqlParse(updateSql);
    String sql = sqlResult.getSql();
    ServerInfo server = sqlResult.getServer();

    Pattern STATE_CONFIG_PATTERN = Pattern.compile("(.*?)\\=(.*?)\\&(.*?)\\=(.*?)");
    Matcher matcher = STATE_CONFIG_PATTERN.matcher(dateValue);

    if (matcher.find()) {
        String[] values = StringUtils.split(dateValue, "&");
        HashMap<String, String> sqlParam = new HashMap<>();
        for (String subValue : values) {
            sqlParam.put(StringUtils.substringBefore(subValue, "="), StringUtils.substringAfter(subValue, "="));
        }
        update(server, sql, sqlParam);

    } else {
        try (PreparedStatement stmt = server.getConnection().prepareStatement(sql)) {
            logger.debug("DB update with:{} ", dateValue);
            stmt.setString(1, dateValue);
            stmt.executeUpdate();
        }
    }
}

From source file:org.openhab.binding.ACDBCommon.db.DBManager.java

/**
 * insert data//from w w w .  j a v a 2  s. c o  m
 *
 * @param insertSql
 * @param cmdParam
 * @throws Exception
 */
public static void insert(String insertSql, String dateValue) throws Exception {
    SqlResult sqlResult = sqlParse(insertSql);
    String sql = sqlResult.getSql();
    ServerInfo server = sqlResult.getServer();

    Pattern STATE_CONFIG_PATTERN = Pattern.compile("(.*?)\\=(.*?)\\&(.*?)\\=(.*?)");
    Matcher matcher = STATE_CONFIG_PATTERN.matcher(dateValue);

    if (matcher.find()) {
        String[] values = StringUtils.split(dateValue, "&");
        HashMap<String, String> sqlParam = new HashMap<>();
        for (String subValue : values) {
            sqlParam.put(StringUtils.substringBefore(subValue, "="), StringUtils.substringAfter(subValue, "="));
        }
        insert(server, sql, sqlParam);

    } else {
        try (PreparedStatement stmt = server.getConnection().prepareStatement(sql)) {
            stmt.setString(1, dateValue);
            stmt.executeUpdate();
        }
    }
}

From source file:org.openhab.binding.IEEE1888.server.FIAPServer.java

private Point buidPoint(URI id, String valueStr) throws ParseException {
    // analyze value
    HashMap<String, String> keyValueMap = new HashMap<>();
    if (valueStr != null) {
        String[] values = StringUtils.split(valueStr, "&");
        if (values != null) {
            for (String value : values) {
                keyValueMap.put(StringUtils.substringBefore(value, "="),
                        StringUtils.substringAfter(value, "="));
            }//from   w w w.ja v a2 s  .  c  om
        }
    }
    // [point]
    Point point = new Point();
    point.setId(id);

    Value value = new Value();
    // [time]
    Calendar timeValue = Calendar.getInstance();
    if (StringUtils.isNotBlank(keyValueMap.get("time"))) {
        Date dateTime = DateUtils.parseDate(keyValueMap.get("time"), TIME_FORMAT);
        timeValue.setTime(dateTime);
    }
    value.setTime(timeValue);

    // [value]
    String v;
    if (keyValueMap.containsKey("value")) {
        v = keyValueMap.get("value");
    } else {
        v = valueStr;
    }
    if (v == null) {
        v = "";
    }
    value.setString(v);

    point.addValue(value);
    return point;
}

From source file:org.openhab.binding.IEEE1888.trap.TrapManager.java

/**
 * If there are any updates, data were sened.
 *
 * @param itemName/*from  w  ww  . j  a va  2  s  . c  om*/
 * @param newState
 */
public static void putTrapData(String itemName, State newState) throws ParseException {
    if (!trapMap.containsKey(itemName)) {
        // If there is not trap, trap is never processed.
        return;
    }

    TrapInfo trapInfo = trapMap.get(itemName);
    Value pointValue = trapInfo.getPoint().getValue()[0];
    String newValue = newState.toString();

    Pattern STATE_CONFIG_PATTERN = Pattern.compile("(.*?)\\=(.*?)\\&(.*?)\\=(.*?)");
    // analyze state
    Matcher matcher = STATE_CONFIG_PATTERN.matcher(newValue);
    if (matcher.find()) {
        String[] values = StringUtils.split(newValue, "&");
        HashMap<String, String> keyValueMap = new HashMap<>();
        for (String value : values) {
            keyValueMap.put(StringUtils.substringBefore(value, "="), StringUtils.substringAfter(value, "="));
        }

        Calendar timeValue = Calendar.getInstance();
        if (StringUtils.isNotBlank(keyValueMap.get("time"))) {
            Date dateTime = DateUtils.parseDate(keyValueMap.get("time"), TIME_FORMAT);
            timeValue.setTime(dateTime);
        }

        if ((timeValue.compareTo(pointValue.getTime()) == 0)
                && StringUtils.equals(keyValueMap.get("value"), pointValue.getString())) {
            // If state was not changed, trap is not processed.
            return;
        }
        pointValue.setString(keyValueMap.get("value"));
        pointValue.setTime(timeValue);
    } else {
        if (StringUtils.equals(newValue, pointValue.getString())) {
            // If state was not changed, trap is not processed.
            return;
        }
        pointValue.setString(newValue);
        pointValue.setTime(Calendar.getInstance());
    }

    // send data
    Map<String, Date> CallbackURLMap = trapInfo.getCallbackURLMap();
    CallbackURLMap.keySet().forEach(key -> new Thread(() -> {
        sendData(key, trapInfo);
    }).start());
}

From source file:org.paxml.web.PaxmlServlet.java

@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse rsp = (HttpServletResponse) response;
    String uri = StringUtils.substringBefore(req.getRequestURI(), "?");
    String fn = FilenameUtils.getName(uri);
    String bn = FilenameUtils.getBaseName(fn);

    boolean found = false;
    for (PaxmlResource res : config.getResources()) {
        if (bn.equals(res.getName())) {
            found = true;/*from   w  w w  . j a  va2s  . c  o m*/
            break;
        }
    }
    if (found) {
        Object result = PaxmlRunner.run(bn, req.getParameterMap(), config);
        if (result != null) {
            rsp.setHeader("Content-Type", "text/html; charset=UTF-8");
            rsp.getWriter().print(new HtmlBuilder(result).build());
        }
        return;
    }

    for (File dir : resources) {
        File f = new File(dir, uri);
        if (f.isFile()) {
            FileInputStream in = new FileInputStream(f);
            try {
                IOUtils.copy(in, rsp.getOutputStream());
                return;
            } finally {
                IOUtils.closeQuietly(in);
            }
        }
    }
    rsp.sendError(404);
}

From source file:org.pegdown.DocumentrParser.java

String getSimpleMacroName(String macroName) {
    return StringUtils.substringBefore(macroName, ":"); //$NON-NLS-1$
}