Search in sources :

Example 81 with InputMismatchException

use of java.util.InputMismatchException in project j2objc by google.

the class InputMismatchExceptionTest method test_ConstructorLjava_lang_String.

/**
 * java.util.InputMismatchException#InputMismatchException(String)
 */
public void test_ConstructorLjava_lang_String() {
    InputMismatchException exception = new InputMismatchException(ERROR_MESSAGE);
    assertNotNull(exception);
    assertEquals(ERROR_MESSAGE, exception.getMessage());
}
Also used : InputMismatchException(java.util.InputMismatchException)

Example 82 with InputMismatchException

use of java.util.InputMismatchException in project orientdb by orientechnologies.

the class OServerCommandPostImportRecords method execute.

@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    final String[] urlParts = checkSyntax(iRequest.url, 4, "Syntax error: importRecords/<database>/<format>/<class>[/<separator>][/<string-delimiter>][/<locale>]");
    final long start = System.currentTimeMillis();
    iRequest.data.commandInfo = "Import records";
    ODatabaseDocument db = getProfiledDatabaseInstance(iRequest);
    try {
        final OClass cls = db.getMetadata().getSchema().getClass(urlParts[3]);
        if (cls == null)
            throw new IllegalArgumentException("Class '" + urlParts[3] + " is not defined");
        if (iRequest.content == null)
            throw new IllegalArgumentException("Empty content");
        if (urlParts[2].equalsIgnoreCase("csv")) {
            final char separator = urlParts.length > 4 ? urlParts[4].charAt(0) : CSV_SEPARATOR;
            final char stringDelimiter = urlParts.length > 5 ? urlParts[5].charAt(0) : CSV_STR_DELIMITER;
            final Locale locale = urlParts.length > 6 ? new Locale(urlParts[6]) : Locale.getDefault();
            final BufferedReader reader = new BufferedReader(new StringReader(iRequest.content));
            String header = reader.readLine();
            if (header == null || (header = header.trim()).length() == 0)
                throw new InputMismatchException("Missing CSV file header");
            final List<String> columns = OStringSerializerHelper.smartSplit(header, separator);
            for (int i = 0; i < columns.size(); ++i) columns.set(i, OIOUtils.getStringContent(columns.get(i)));
            int imported = 0;
            int errors = 0;
            final StringBuilder output = new StringBuilder(1024);
            int line = 0;
            int col = 0;
            String column = "?";
            String parsedCell = "?";
            final NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
            for (line = 2; reader.ready(); line++) {
                try {
                    final String parsedRow = reader.readLine();
                    if (parsedRow == null)
                        break;
                    final ODocument doc = new ODocument(cls);
                    final String row = parsedRow.trim();
                    final List<String> cells = OStringSerializerHelper.smartSplit(row, CSV_SEPARATOR);
                    for (col = 0; col < columns.size(); ++col) {
                        parsedCell = cells.get(col);
                        column = columns.get(col);
                        String cellValue = parsedCell.trim();
                        if (cellValue.length() == 0 || cellValue.equalsIgnoreCase("null"))
                            continue;
                        Object value;
                        if (cellValue.length() >= 2 && cellValue.charAt(0) == stringDelimiter && cellValue.charAt(cellValue.length() - 1) == stringDelimiter)
                            value = OIOUtils.getStringContent(cellValue);
                        else {
                            try {
                                value = numberFormat.parse(cellValue);
                            } catch (Exception e) {
                                value = ORecordSerializerCSVAbstract.getTypeValue(cellValue);
                            }
                        }
                        doc.field(columns.get(col), value);
                    }
                    doc.save();
                    imported++;
                } catch (Exception e) {
                    errors++;
                    output.append(String.format("#%d: line %d column %s (%d) value '%s': '%s'\n", errors, line, column, col, parsedCell, e.toString()));
                }
            }
            final float elapsed = (System.currentTimeMillis() - start) / 1000;
            String message = String.format("Import of records of class '%s' completed in %5.3f seconds. Line parsed: %d, imported: %d, error: %d\nDetailed messages:\n%s", cls.getName(), elapsed, line, imported, errors, output);
            iResponse.send(OHttpUtils.STATUS_CREATED_CODE, OHttpUtils.STATUS_CREATED_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, message, null);
            return false;
        } else
            throw new UnsupportedOperationException("Unsupported format on importing record. Available formats are: csv");
    } finally {
        if (db != null)
            db.close();
    }
}
Also used : Locale(java.util.Locale) InputMismatchException(java.util.InputMismatchException) InputMismatchException(java.util.InputMismatchException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) NumberFormat(java.text.NumberFormat) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 83 with InputMismatchException

use of java.util.InputMismatchException in project open-kilda by telstra.

the class TraffExamServiceImpl method initializePools.

@PostConstruct
void initializePools() {
    baseUrl = labEndpoint + "/api/" + topology.getLabId() + "/traffgen/";
    hostsPool = new ConcurrentHashMap<>();
    for (TraffGen traffGen : topology.getActiveTraffGens()) {
        URI controlEndpoint;
        try {
            controlEndpoint = new URI(traffGen.getControlEndpoint());
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(String.format("Invalid traffGen(%s) REST endpoint address \"%s\": %s", traffGen.getName(), traffGen.getControlEndpoint(), e.getMessage()), e);
        }
        UUID id = UUID.randomUUID();
        Host host = new Host(id, traffGen.getIfaceName(), controlEndpoint, traffGen.getName());
        try {
            restTemplate.headForHeaders(makeHostUri(host).path("endpoint").build());
        } catch (RestClientException ex) {
            throw new IllegalArgumentException(String.format("The traffGen(%s) REST endpoint address \"%s\" can't be reached: %s", traffGen.getName(), traffGen.getControlEndpoint(), ex.getMessage()), ex);
        }
        hostsPool.put(id, host);
    }
    TraffGenConfig config = topology.getTraffGenConfig();
    Inet4Network network;
    try {
        network = new Inet4Network((Inet4Address) Inet4Address.getByName(config.getAddressPoolBase()), config.getAddressPoolPrefixLen());
    } catch (Inet4ValueException | UnknownHostException e) {
        throw new InputMismatchException(String.format("Invalid traffGen address pool \"%s:%s\": %s", config.getAddressPoolBase(), config.getAddressPoolPrefixLen(), e));
    }
    addressPool = new Inet4NetworkPool(network, 30);
}
Also used : Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) Host(org.openkilda.testing.service.traffexam.model.Host) URISyntaxException(java.net.URISyntaxException) InputMismatchException(java.util.InputMismatchException) URI(java.net.URI) TraffGen(org.openkilda.testing.model.topology.TopologyDefinition.TraffGen) Inet4Network(org.openkilda.testing.service.traffexam.networkpool.Inet4Network) Inet4ValueException(org.openkilda.testing.service.traffexam.networkpool.Inet4ValueException) RestClientException(org.springframework.web.client.RestClientException) TraffGenConfig(org.openkilda.testing.model.topology.TopologyDefinition.TraffGenConfig) Inet4NetworkPool(org.openkilda.testing.service.traffexam.networkpool.Inet4NetworkPool) UUID(java.util.UUID) PostConstruct(javax.annotation.PostConstruct)

Aggregations

InputMismatchException (java.util.InputMismatchException)83 Scanner (java.util.Scanner)56 NoSuchElementException (java.util.NoSuchElementException)47 Locale (java.util.Locale)23 BufferUnderflowException (java.nio.BufferUnderflowException)5 Pattern (java.util.regex.Pattern)5 BufferedReader (java.io.BufferedReader)4 BigInteger (java.math.BigInteger)4 UnicodeReader (gdsc.core.utils.UnicodeReader)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 TodoList (de.djuelg.neuronizer.domain.model.preview.TodoList)2 TodoListHeader (de.djuelg.neuronizer.domain.model.todolist.TodoListHeader)2 Point (java.awt.Point)2 Serializable (java.io.Serializable)2 BigDecimal (java.math.BigDecimal)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1