org.eclipse.collections.api.tuple.Pair Java Examples

The following examples show how to use org.eclipse.collections.api.tuple.Pair. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Db2SqlExecutor.java    From obevo with Apache License 2.0 6 votes vote down vote up
/**
 * Finds the table name from SQL Exception. Based on the documentation as defined at
 * http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/ad/tjvjcerr.htm
 *
 * @param exception An instance of SQL Exception
 * TODO handle the hardcoding of these errorCodes a little bit better
 * @return The table name from SQL Exception
 */
Pair<PhysicalSchema, String> findTableNameFromException(Exception exception, int errorCode) {
    String sqlErrorMC = exception.getMessage();

    Matcher matcher;
    switch (errorCode) {
    case -20054:
        matcher = PATTERN_20054.matcher(sqlErrorMC);
        break;
    case -668:
        matcher = PATTERN_668.matcher(sqlErrorMC);
        break;
    default:
        throw new IllegalArgumentException("Unhandled error code for reorg message parsing: " + errorCode);
    }

    String schemaName;
    String tableName;
    if (matcher.find()) {
        schemaName = matcher.group(1);
        tableName = matcher.group(2);
    } else {
        throw new IllegalArgumentException("Could not parse the schema/table names for error code " + errorCode + " and message: " + sqlErrorMC);
    }
    return Tuples.pair(PhysicalSchema.parseFromString(schemaName), tableName);
}
 
Example #2
Source File: TextMarkupDocumentReaderTest.java    From obevo with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuotedSplitWithEqualSign() {
    String input = "attr=1234 attr2=\"56=78\" mytog1";
    if (legacyMode) {
        try {
            textMarkupDocumentReader.parseAttrsAndToggles(input);
            fail("Should have failed here");
        } catch (IllegalArgumentException e) {
            assertThat(e.getMessage(), containsString("Cannot mark = multiple times"));
        }
    } else {
        Pair<ImmutableMap<String, String>, ImmutableSet<String>> results = textMarkupDocumentReader.parseAttrsAndToggles(input);

        assertEquals(Maps.mutable.of("attr", "1234", "attr2", "56=78"), results.getOne());
        assertEquals(Sets.mutable.of("mytog1"), results.getTwo());
    }
}
 
Example #3
Source File: TextMarkupDocumentReaderTest.java    From obevo with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuotedSplitWithEqualSignAndSpace() {
    String input = "attr=1234 attr2=\"56 = 78\" mytog1";
    if (legacyMode) {
        try {
            textMarkupDocumentReader.parseAttrsAndToggles(input);
            fail("Should have failed here");
        } catch (ArrayIndexOutOfBoundsException e) {
            assertThat(e.getMessage(), notNullValue());
        }
    } else {
        Pair<ImmutableMap<String, String>, ImmutableSet<String>> results = textMarkupDocumentReader.parseAttrsAndToggles(input);

        assertEquals(Maps.mutable.of("attr", "1234", "attr2", "56 = 78"), results.getOne());
        assertEquals(Sets.mutable.of("mytog1"), results.getTwo());
    }
}
 
Example #4
Source File: TextMarkupDocumentReaderTest.java    From obevo with Apache License 2.0 6 votes vote down vote up
@Test
public void testWordEndingInEqualSignWillFailInLegacy() {
    String input = "   attr= abc";
    if (legacyMode) {
        try {
            textMarkupDocumentReader.parseAttrsAndToggles(input);
            fail("Should have failed here");
        } catch (ArrayIndexOutOfBoundsException expected) {
        }
    } else {
        Pair<ImmutableMap<String, String>, ImmutableSet<String>> results = textMarkupDocumentReader.parseAttrsAndToggles(input);

        assertEquals(Maps.mutable.of("attr", ""), results.getOne());
        assertEquals(Sets.mutable.of("abc"), results.getTwo());
    }
}
 
Example #5
Source File: AquaRevengMain.java    From obevo with Apache License 2.0 6 votes vote down vote up
private ChangeTypeInfo determineChangeType(final String wholeFileString) {
    RichIterable<ChangeTypeInfo> changeTypeInfos = this.patternMap.keyValuesView().collect(
            new Function<Pair<ChangeType, Pattern>, ChangeTypeInfo>() {
                @Override
                public ChangeTypeInfo valueOf(Pair<ChangeType, Pattern> object) {
                    Pair<Integer, String> contentInfo = getStartIndex(wholeFileString, object.getTwo());
                    return new ChangeTypeInfo(object.getOne()
                            , contentInfo.getOne()
                            , contentInfo.getTwo()
                    );
                }
            });
    ChangeTypeInfo chosenChangeTypeInfo = changeTypeInfos.minBy(ChangeTypeInfo.TO_START_INDEX);

    if (chosenChangeTypeInfo.getStartIndex() == Integer.MAX_VALUE) {
        return new ChangeTypeInfo(UnclassifiedChangeType.INSTANCE, Integer.MAX_VALUE, null);
    } else {
        return chosenChangeTypeInfo;
    }
}
 
Example #6
Source File: Main.java    From obevo with Apache License 2.0 6 votes vote down vote up
private Pair<String, Procedure<String[]>> getDeployCommand(String[] args, Runnable exitFailureMethod) {
    if (args.length == 0) {
        usage();
        exitFailureMethod.run();
    }

    Procedure<String[]> command = commandMap.get(args[0]);
    if (command == null) {
        System.out.println("No command w/ name " + args[0] + " has been defined in this distribution: " + commandMap.keysView().makeString("[", ", ", "]"));
        System.out.println("See the usage for more details");
        usage();
        exitFailureMethod.run();
    }

    return Tuples.pair(args[0], command);
}
 
Example #7
Source File: PostgreSqlFunctionChangeTypeBehavior.java    From obevo with Apache License 2.0 6 votes vote down vote up
/**
 * Functions need to be referred by their signatures for drops and grants in postgresql
 * For functions query info:
 * https://www.postgresql.org/docs/9.5/static/functions-info.html
 * https://www.postgresql.org/docs/9.5/static/catalog-pg-proc.html
 * https://www.postgresql.org/docs/9.5/static/catalog-pg-namespace.html
 */
@Override
protected Pair<Boolean, RichIterable<String>> getQualifiedObjectNames(Connection conn, PhysicalSchema physicalSchema, String objectName) {
    String schemaName = getDbPlatform().convertDbObjectName().valueOf(physicalSchema.getPhysicalName());
    String functionNameWithCase = getDbPlatform().convertDbObjectName().valueOf(objectName);

    String sql = "select format('%s.%s(%s)',n.nspname, p.proname, pg_get_function_identity_arguments(p.oid)) " +
            "as functionname\n" +
            "FROM   pg_proc p\n" +
            "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n" +
            "WHERE n.nspname = '" + schemaName + "' and p.proname = '" + functionNameWithCase + "'";

    List<Map<String, Object>> funcNameResults = getSqlExecutor().getJdbcTemplate().queryForList(conn, sql);

    MutableList<String> names = Lists.mutable.empty();
    for (Map<String, Object> funcNameResult : funcNameResults) {
        names.add((String) funcNameResult.get("functionname"));
    }

    return Tuples.<Boolean, RichIterable<String>>pair(false, names);
}
 
Example #8
Source File: JdbcHelper.java    From obevo with Apache License 2.0 6 votes vote down vote up
private Pair<Statement, ResultSet> queryAndLeaveStatementOpenInternal(Connection conn, int retryCount, String sql) {
    Statement statement = null;
    try {
        statement = conn.createStatement();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Executing query on {}: {}", displayConnection(conn), sql);
        }
        return Tuples.pair(statement, statement.executeQuery(sql));
    } catch (SQLException e) {
        DbUtils.closeQuietly(statement);  // on an exception, close the existing statement (on success, we'd leave it open)
        DataAccessException dataAccessException = new DataAccessException(e);
        boolean retry = this.jdbcHandler.handleException(this, conn, retryCount, dataAccessException);
        if (retry) {
            return this.queryAndLeaveStatementOpenInternal(conn, retryCount + 1, sql);
        } else {
            throw dataAccessException;
        }
    }
}
 
Example #9
Source File: DbEnvironmentCleaner.java    From obevo with Apache License 2.0 6 votes vote down vote up
@Override
public void cleanEnvironment(final boolean noPrompt) {
    Validate.isTrue(env.isCleanBuildAllowed(), "Clean build not allowed for this environment [" + env.getName()
            + "] ! Exiting...");

    // some schemas have complex dependencies that we currently aren't handling w/ the drop code. To work
    // around it, we just retry the drop if we have progress in dropping objects.
    // Note that regular forward deploys can handle dependencies properly; we just need the logic to extract
    // the object definitions out for all object types to enable this.
    int tryCount = 0;
    while (true) {
        tryCount++;
        LOG.info("Attempting to clean objects from environment");
        final Pair<Boolean, MutableList<Exception>> clearResults = clearEnvironmentInternal(noPrompt);
        if (!clearResults.getOne()) {
            throw new DeployerRuntimeException("Could not clean schema; remaining exceptions: " + clearResults.getTwo().collect(TO_EXCEPTION_STACK_TRACE));
        } else if (clearResults.getTwo().isEmpty()) {
            return;
        } else if (tryCount <= 10) {
            LOG.info("Failed to clean up schema on try #" + tryCount + " but able to make progress, will continue to try");
        } else {
            throw new DeployerRuntimeException("Could not clean schema after max " + tryCount + " tries; will exit with remaining exceptions: " + clearResults.getTwo().collect(TO_EXCEPTION_STACK_TRACE));
        }
    }
}
 
Example #10
Source File: ExercisesRelationships.java    From reladomo-kata with Apache License 2.0 6 votes vote down vote up
@Test
public void testQ4()
{
    CustomerAccountList accountsBefore = CustomerAccountFinder.findMany(CustomerAccountFinder.all());
    CustomerList customersBefore = CustomerFinder.findMany(CustomerFinder.all());
    accountsBefore.forceResolve(); //to get this list resolved before we add the new customer.
    customersBefore.forceResolve();

    MutableList<Pair<String, String>> accountDescriptionAndTypePairs = FastList.newListWith(
            Tuples.pair("Tom's saving Account", "Savings"),
            Tuples.pair("Tom's running Account", "Running")
    );

    this.addCustomerAccounts("Tom Jones", "UK", accountDescriptionAndTypePairs);

    CustomerAccountList accountsAfter = CustomerAccountFinder.findMany(CustomerAccountFinder.all());
    CustomerList customersAfter = CustomerFinder.findMany(CustomerFinder.all());

    Assert.assertEquals(1, customersAfter.size() - customersBefore.size());
    Assert.assertEquals(2, accountsAfter.size() - accountsBefore.size());

    Customer tom = CustomerFinder.findOne(CustomerFinder.name().eq("Tom Jones"));
    CustomerAccountList tomsAccounts = new CustomerAccountList(CustomerAccountFinder.customerId().eq(tom.getCustomerId()));
    Verify.assertSize(2, tomsAccounts);
}
 
Example #11
Source File: AbstractIqDataSourceFactory.java    From obevo with Apache License 2.0 6 votes vote down vote up
@Override
public final DataSource createDataSource(DbEnvironment env, Credential credential, String schema, int numThreads) {
    String url;
    String password;
    if (env.getJdbcUrl() != null) {
        url = env.getJdbcUrl();
        password = credential.getPassword();
    } else {
        Pair<String, String> urlPasswordPair = this.getUrl(env, schema, credential);
        url = urlPasswordPair.getOne();
        password = urlPasswordPair.getTwo() != null ? urlPasswordPair.getTwo() : credential.getPassword();
    }

    LOG.info("Connecting using URL: {}", url);
    Credential schemaCredential = new Credential(schema, password);
    return JdbcDataSourceFactory.createFromJdbcUrl(env.getDriverClass(), url, schemaCredential, numThreads);
}
 
Example #12
Source File: ForEachPatternUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void whenInstantiateAndChangeValues_thenCorrect() {
    Pair<Integer, String> pair1 = Tuples.pair(1, "One");
    Pair<Integer, String> pair2 = Tuples.pair(2, "Two");
    Pair<Integer, String> pair3 = Tuples.pair(3, "Three");

    UnifiedMap<Integer, String> map = UnifiedMap.newMapWith(pair1, pair2, pair3);

    for (int i = 0; i < map.size(); i++) {
        map.put(i + 1, "New Value");
    }

    for (int i = 0; i < map.size(); i++) {
        Assert.assertEquals("New Value", map.get(i + 1));
    }
}
 
Example #13
Source File: ZipWithIndexUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Before
public void setup() {
    Pair<String, Integer> pair1 = Tuples.pair("Porsche", 0);
    Pair<String, Integer> pair2 = Tuples.pair("Volvo", 1);
    Pair<String, Integer> pair3 = Tuples.pair("Toyota", 2);
    expectedPairs = Lists.mutable.of(pair1, pair2, pair3);
}
 
Example #14
Source File: TextMarkupDocumentReaderTest.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Test
public void testEqualSignAloneWillFailInLegacy() {
    String input = " a = b ";
    if (legacyMode) {
        try {
            textMarkupDocumentReader.parseAttrsAndToggles(input);
            fail("Should throw exception here");
        } catch (ArrayIndexOutOfBoundsException expected) {
        }
    } else {
        Pair<ImmutableMap<String, String>, ImmutableSet<String>> results = textMarkupDocumentReader.parseAttrsAndToggles(input);
        assertEquals(Maps.mutable.empty(), results.getOne());
        assertEquals(Sets.mutable.of("a", "b", "="), results.getTwo());
    }
}
 
Example #15
Source File: ZipWithIndexUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenZip_thenCorrect() {
    MutableList<String> cars = FastList.newListWith("Porsche", "Volvo", "Toyota");
    MutableList<Pair<String, Integer>> pairs = cars.zipWithIndex();

    Assertions.assertThat(pairs).containsExactlyElementsOf(this.expectedPairs);
}
 
Example #16
Source File: ExercisesCrud.java    From reladomo-kata with Apache License 2.0 5 votes vote down vote up
@Test
public void testQ8()
{
    int sizeBefore = CustomerFinder.findMany(CustomerFinder.all()).size();
    Pair<String, String> customer1 = Tuples.pair("John Courage", "UK");
    Pair<String, String> customer2 = Tuples.pair("Tony Jackson", "USA");
    this.addNewCustomers(FastList.newListWith(customer1, customer2));
    int sizeAfter = CustomerFinder.findMany(CustomerFinder.all()).size();
    Assert.assertEquals(2, sizeAfter - sizeBefore);

    Assert.assertNotNull(CustomerFinder.findOne(CustomerFinder.name().eq("John Courage")));
    Assert.assertNotNull(CustomerFinder.findMany(CustomerFinder.name().eq("Tony Jackson")));
}
 
Example #17
Source File: ZipUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Before
public void setup() {
    Pair<String, String> pair1 = Tuples.pair("1", "Porsche");
    Pair<String, String> pair2 = Tuples.pair("2", "Volvo");
    Pair<String, String> pair3 = Tuples.pair("3", "Toyota");
    expectedPairs = Lists.mutable.of(pair1, pair2, pair3);
}
 
Example #18
Source File: TextMarkupDocumentReaderTest.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Test
public void testSlashWithQuoteNoEscaping() {
    Pair<ImmutableMap<String, String>, ImmutableSet<String>> results = textMarkupDocumentReader.parseAttrsAndToggles(
            " 123  attr=\"abc\\\"d ef\"  \\  456=789"
    );

    if (legacyMode) {
        assertEquals(Maps.mutable.of("attr", "\"abc\\\"d", "456", "789"), results.getOne());
        assertEquals(Sets.mutable.of("123", "\\", "ef\""), results.getTwo());
    } else {
        assertEquals(Maps.mutable.of("attr", "abc\"d ef", "456", "789"), results.getOne());
        assertEquals(Sets.mutable.of("123", "\\"), results.getTwo());
    }
}
 
Example #19
Source File: ZipUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenZip_thenCorrect() {
    MutableList<String> numbers = Lists.mutable.with("1", "2", "3", "Ignored");
    MutableList<String> cars = Lists.mutable.with("Porsche", "Volvo", "Toyota");
    MutableList<Pair<String, String>> pairs = numbers.zip(cars);

    Assertions.assertThat(pairs).containsExactlyElementsOf(this.expectedPairs);
}
 
Example #20
Source File: TextMarkupDocumentReaderTest.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Test
public void testQuoteWithinStringIsStillPreservedEvenIfStringIsntClosedForBackwardsCompatibility() {
    Pair<ImmutableMap<String, String>, ImmutableSet<String>> results = textMarkupDocumentReader.parseAttrsAndToggles(
            "   =attr  1234 attr2=\"56\"78 mytog1  "
    );

    if (legacyMode) {
        assertEquals(Maps.mutable.of("", "attr", "attr2", "\"56\"78"), results.getOne());
        assertEquals(Sets.mutable.of("1234", "mytog1"), results.getTwo());
    } else {
        assertEquals(Maps.mutable.of("attr2", "\"56\"78"), results.getOne());
        assertEquals(Sets.mutable.of("=", "attr", "1234", "mytog1"), results.getTwo());
    }
}
 
Example #21
Source File: ObjectTypeAndNamePredicateBuilder.java    From obevo with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the predicate on object type and name based on the input functions passed in.
 */
public <T> Predicates<? super T> build(final Function<? super T, String> objectTypeFunction, final Function<? super T, String> objectNameFunction) {
    if (objectNamesByType.isEmpty()) {
        if (filterType == null || filterType.isEmptyInputResult()) {
            return Predicates.alwaysTrue();
        } else {
            return Predicates.alwaysFalse();
        }
    }

    RichIterable<Predicate<? super T>> typePredicates = objectNamesByType.keyMultiValuePairsView().toList().collect(new Function<Pair<String, RichIterable<String>>, Predicate<? super T>>() {
        @Override
        public Predicate<? super T> valueOf(Pair<String, RichIterable<String>> pair) {
            String objectType = pair.getOne();
            RichIterable<String> objectPatterns = pair.getTwo();
            boolean negatePredicate = filterType == FilterType.EXCLUDE;
            if (objectType.startsWith("-")) {
                objectType = objectType.substring(1);
                negatePredicate = true;
            }

            Predicate<T> objectTypeAndNamePredicate = getObjectTypeAndNamePredicate(
                    objectTypeFunction, Lists.immutable.with(objectType),
                    negatePredicate, objectNameFunction, objectPatterns.toList().toImmutable()
            );

            return objectTypeAndNamePredicate;
        }
    });

    if (filterType == null || filterType == FilterType.EXCLUDE) {
        return Predicates.and(typePredicates);
    } else {
        return Predicates.or(typePredicates);
    }
}
 
Example #22
Source File: TextMarkupDocumentReaderTest.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Test
public void testSlashWithQuoteNoEscaping2() {
    Pair<ImmutableMap<String, String>, ImmutableSet<String>> results = textMarkupDocumentReader.parseAttrsAndToggles(
            " 123  attr=\"abc\\d ef\"  \\  456=789"
    );

    if (legacyMode) {
        assertEquals(Maps.mutable.of("attr", "\"abc\\d", "456", "789"), results.getOne());
        assertEquals(Sets.mutable.of("123", "\\", "ef\""), results.getTwo());
    } else {
        assertEquals(Maps.mutable.of("attr", "abc\\d ef", "456", "789"), results.getOne());
        assertEquals(Sets.mutable.of("123", "\\"), results.getTwo());
    }
}
 
Example #23
Source File: TextMarkupDocumentReaderTest.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleLineParsing() {
    Pair<ImmutableMap<String, String>, ImmutableSet<String>> results = textMarkupDocumentReader.parseAttrsAndToggles(
            "attr=1234 attr2=\"5678\" mytog1"
    );

    assertEquals(Maps.mutable.of("attr", "1234", "attr2", "5678"), results.getOne());
    assertEquals(Sets.mutable.of("mytog1"), results.getTwo());
}
 
Example #24
Source File: ObjectTypeAndNamePredicateBuilderTest.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringParsingWithExclusionDefault() {
    ObjectTypeAndNamePredicateBuilder parse = ObjectTypeAndNamePredicateBuilder.parse("TABLE~tab1,tab2;VIEW~view1", ObjectTypeAndNamePredicateBuilder.FilterType.EXCLUDE);
    Predicates<? super Pair<String, String>> predicate = parse.build(Functions.<String>firstOfPair(), (Function<Pair<String, String>, String>) (Function) Functions.<String>secondOfPair());

    assertTrue(predicate.accept(Tuples.pair("OTHER", "otherInclude")));
    assertFalse(predicate.accept(Tuples.pair("TABLE", "tab1")));
    assertFalse(predicate.accept(Tuples.pair("TABLE", "tab2")));
    assertTrue(predicate.accept(Tuples.pair("TABLE", "tabNo")));
    assertFalse(predicate.accept(Tuples.pair("VIEW", "view1")));
    assertTrue(predicate.accept(Tuples.pair("VIEW", "viewInclude")));
}
 
Example #25
Source File: ObjectTypeAndNamePredicateBuilderTest.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringParsing() {
    ObjectTypeAndNamePredicateBuilder parse = ObjectTypeAndNamePredicateBuilder.parse("TABLE~tab1,tab2;-VIEW~view1", null);
    Predicates<? super Pair<String, String>> predicate = parse.build(Functions.<String>firstOfPair(), (Function<Pair<String, String>, String>) (Function) Functions.<String>secondOfPair());

    assertTrue(predicate.accept(Tuples.pair("OTHER", "otherInclude")));
    assertTrue(predicate.accept(Tuples.pair("TABLE", "tab1")));
    assertTrue(predicate.accept(Tuples.pair("TABLE", "tab2")));
    assertFalse(predicate.accept(Tuples.pair("TABLE", "tabNo")));
    assertFalse(predicate.accept(Tuples.pair("VIEW", "view1")));
    assertTrue(predicate.accept(Tuples.pair("VIEW", "viewInclude")));
}
 
Example #26
Source File: AbstractReveng.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public Pair<String, RevengPatternOutput> valueOf(String sqlSnippet) {
    for (RevengPattern revengPattern : revengPatterns) {
        RevengPatternOutput patternMatch = revengPattern.evaluate(sqlSnippet);
        if (patternMatch != null) {
            return Tuples.pair(sqlSnippet, patternMatch);
        }
    }
    return Tuples.pair(sqlSnippet, null);
}
 
Example #27
Source File: DbFileMerger.java    From obevo with Apache License 2.0 5 votes vote down vote up
void addFilePair(Pair<String, FileObject> filePair) {
            this.filePairs.add(filePair);
            String fileContent = filePair.getTwo().getStringContent();
            String normalizedContent = DAStringUtil.normalizeWhiteSpaceFromStringOld(fileContent);
            contentToEnvsMap.put(normalizedContent, filePair);
            // modify the content here if needed
//            addContentValues(fileContent);
//            addDistinctValue(normalizedContent);
        }
 
Example #28
Source File: TextMarkupDocumentReader.java    From obevo with Apache License 2.0 5 votes vote down vote up
private ImmutableList<TextMarkupDocumentSection> parseString(String text, ImmutableList<String> elementsToCheck, final boolean recurse,
        final String elementPrefix) {
    MutableList<Pair<String, String>> outerSections = splitIntoMainSections(text, elementsToCheck, elementPrefix);

    MutableList<TextMarkupDocumentSection> sections = outerSections.flatCollect(new ConvertOuterSectionToTextSection(recurse, elementPrefix));

    // remove any blank sections
    return sections.toImmutable().reject(each -> recurse
            && each.getName() == null
            && (StringUtils.isBlank(each.getContent()) || StringUtils.isBlank(CommentRemover.removeComments(each.getContent(), "removing on markup document reader"))  // need comments in a separate clause as CommentRemover returns a "null" string on null; will fix eventually
    ));
}
 
Example #29
Source File: Tokenizer.java    From obevo with Apache License 2.0 5 votes vote down vote up
public String tokenizeString(String input) {
    String output = input;
    for (Pair<String, String> entry : params.keyValuesView()) {
        output = output.replace(paramPrefix + entry.getOne() + paramSuffix, entry.getTwo());
    }

    return output;
}
 
Example #30
Source File: TextMarkupDocumentReader.java    From obevo with Apache License 2.0 5 votes vote down vote up
private MutableList<Pair<String, String>> splitIntoMainSections(String text, ImmutableList<String> elementsToCheck, String elementPrefix) {
    MutableList<Pair<String, String>> outerSections = Lists.mutable.empty();
    String nextSectionName = null;
    boolean startOfSearch = true;

    // here, we go in a loop searching for the next referenc of "[elementPrefix] [sectionName]", e.g. //// CHANGE
    // By each of those points, we split those into separate text sections and return back to the client.
    // We aim to preserve the line breaks found when parsing the sections
    while (text != null) {
        String currentSectionName = nextSectionName;
        String currentSectionText;

        int earliestIndex = Integer.MAX_VALUE;

        for (String firstLevelElement : elementsToCheck) {
            // on the first search, the text may start w/ the section; hence, we set the search fromIndex param to 0.
            // Subsequently, the index picks up at the beginning of the next section; hence, we must start
            // the search at the next character, so the fromIndex param is 1
            int index = text.indexOf(elementPrefix + " " + firstLevelElement, startOfSearch ? 0 : 1);

            if (index != -1 && index < earliestIndex) {
                earliestIndex = index;
                nextSectionName = firstLevelElement;
            }
        }

        startOfSearch = false;

        if (earliestIndex == Integer.MAX_VALUE) {
            currentSectionText = StringUtils.chomp(text);
            text = null;
        } else {
            currentSectionText = StringUtils.chomp(text.substring(0, earliestIndex));
            text = text.substring(earliestIndex);
        }

        outerSections.add(Tuples.pair(currentSectionName, currentSectionText));
    }
    return outerSections;
}