/** * Gets the content string starting at the first position (inclusive) and continuing up to the end position (exclusive). * * @param starting the position marking the beginning of the desired content string. * @param end the position located directly after the returned content string; can be null, which means end of content * @return the content string; never null */ public String getContentBetween( Position starting, Position end ) { CheckArg.isNotNull(starting, "starting"); int startIndex = starting.getIndexInContent(); int endIndex = inputString.length(); if (end != null) { endIndex = end.getIndexInContent(); } if (startIndex >= endIndex) { throw new IllegalArgumentException(CommonI18n.endPositionMustBeGreaterThanStartingPosition.text(startIndex, endIndex)); } return inputString.substring(startIndex, endIndex); }
/** * Gets the content string starting at the first position (inclusive) and continuing up to the end position (exclusive). * * @param starting the position marking the beginning of the desired content string. * @param end the position located directly after the returned content string; can be null, which means end of content * @return the content string; never null */ public String getContentBetween( Position starting, Position end ) { CheckArg.isNotNull(starting, "starting"); int startIndex = starting.getIndexInContent(); int endIndex = inputString.length(); if (end != null) { endIndex = end.getIndexInContent(); } if (startIndex >= endIndex) { throw new IllegalArgumentException(CommonI18n.endPositionMustBeGreaterThanStartingPosition.text(startIndex, endIndex)); } return inputString.substring(startIndex, endIndex); }
/** * Gets the content string starting at the first position (inclusive) and continuing up to the end position (exclusive). * * @param starting the position marking the beginning of the desired content string. * @param end the position located directly after the returned content string; can be null, which means end of content * @return the content string; never null */ public String getContentBetween( Position starting, Position end ) { CheckArg.isNotNull(starting, "starting"); int startIndex = starting.getIndexInContent(); int endIndex = inputString.length(); if (end != null) { endIndex = end.getIndexInContent(); } if (startIndex >= endIndex) { throw new IllegalArgumentException(CommonI18n.endPositionMustBeGreaterThanStartingPosition.text(startIndex, endIndex)); } return inputString.substring(startIndex, endIndex); }
/** * Return a new position that is the addition of this position and that supplied. * * @param position the position to add to this object; may not be null * @return the combined position */ public Position add( Position position ) { if (this.getIndexInContent() < 0) { return position.getIndexInContent() < 0 ? EMPTY_CONTENT_POSITION : position; } if (position.getIndexInContent() < 0) { return this; } int index = this.getIndexInContent() + position.getIndexInContent(); int line = position.getLine() + this.getLine() - 1; int column = this.getLine() == 1 ? this.getColumn() + position.getColumn() : this.getColumn(); return new Position(index, line, column); } }
/** * Return a new position that is the addition of this position and that supplied. * * @param position the position to add to this object; may not be null * @return the combined position */ public Position add( Position position ) { if (this.getIndexInContent() < 0) { return position.getIndexInContent() < 0 ? EMPTY_CONTENT_POSITION : position; } if (position.getIndexInContent() < 0) { return this; } int index = this.getIndexInContent() + position.getIndexInContent(); int line = position.getLine() + this.getLine() - 1; int column = this.getLine() == 1 ? this.getColumn() + position.getColumn() : this.getColumn(); return new Position(index, line, column); } }
/** * Return a new position that is the addition of this position and that supplied. * * @param position the position to add to this object; may not be null * @return the combined position */ public Position add( Position position ) { if (this.getIndexInContent() < 0) { return position.getIndexInContent() < 0 ? EMPTY_CONTENT_POSITION : position; } if (position.getIndexInContent() < 0) { return this; } int index = this.getIndexInContent() + position.getIndexInContent(); int line = position.getLine() + this.getLine() - 1; int column = this.getLine() == 1 ? this.getColumn() + position.getColumn() : this.getColumn(); return new Position(index, line, column); } }
/** * Returns the string content for characters bounded by the previous marked position and the position of the currentToken * (inclusive). Method also marks() the new position the the currentToken. * * @return the string content for characters bounded by the previous marked position and the position of the currentToken * (inclusive). */ public String getMarkedContent() { Position startPosition = new Position(currentMarkedPosition.getIndexInContent(), currentMarkedPosition.getLine(), currentMarkedPosition.getColumn()); mark(); return getContentBetween(startPosition, currentMarkedPosition); }
/** * Returns the string content for characters bounded by the previous marked position and the position of the currentToken * (inclusive). Method also marks() the new position the the currentToken. * * @return the string content for characters bounded by the previous marked position and the position of the currentToken * (inclusive). */ public String getMarkedContent() { Position startPosition = new Position(currentMarkedPosition.getIndexInContent(), currentMarkedPosition.getLine(), currentMarkedPosition.getColumn()); mark(); return getContentBetween(startPosition, currentMarkedPosition); }
private int combinedIndex( int firstIndex, int secondIndex ) { Position first = new Position(firstIndex, 1, 0); Position second = new Position(secondIndex, 1, 0); int firstPlusSecond = first.add(second).getIndexInContent(); int secondPlusFirst = second.add(first).getIndexInContent(); assertThat(firstPlusSecond, is(secondPlusFirst)); return firstPlusSecond; }
/** * {@inheritDoc} * * @see org.modeshape.sequencer.ddl.StandardDdlParser#consumeIdentifier(org.modeshape.sequencer.ddl.DdlTokenStream) */ @Override protected String consumeIdentifier( DdlTokenStream tokens ) throws ParsingException { Position startPosition = tokens.nextPosition(); String id = super.consumeIdentifier(tokens); if (!tokens.hasNext()) return id; Position nextPosition = tokens.nextPosition(); while (((nextPosition.getIndexInContent() - startPosition.getIndexInContent() - id.length()) == 0)) { // allowed symbols in an identifier: underscore, dollar sign, pound sign, at sign if (tokens.matches(DdlTokenizer.SYMBOL)) { if (tokens.matches('$') || tokens.matches('#') || tokens.matches('@')) { id += tokens.consume(); // consume symbol } else { break; // not a valid ID symbol } } else { id += tokens.consume(); // consume text } nextPosition = tokens.nextPosition(); } return id; }
/** * {@inheritDoc} * * @see org.modeshape.sequencer.ddl.StandardDdlParser#consumeIdentifier(org.modeshape.sequencer.ddl.DdlTokenStream) */ @Override protected String consumeIdentifier( DdlTokenStream tokens ) throws ParsingException { Position startPosition = tokens.nextPosition(); String id = super.consumeIdentifier(tokens); if (!tokens.hasNext()) return id; Position nextPosition = tokens.nextPosition(); while (((nextPosition.getIndexInContent() - startPosition.getIndexInContent() - id.length()) == 0)) { // allowed symbols in an identifier: underscore, dollar sign, pound sign, at sign if (tokens.matches(DdlTokenizer.SYMBOL)) { if (tokens.matches('$') || tokens.matches('#') || tokens.matches('@')) { id += tokens.consume(); // consume symbol } else { break; // not a valid ID symbol } } else { id += tokens.consume(); // consume text } nextPosition = tokens.nextPosition(); } return id; }
@Test public void shouldFindNextPositionStartIndex() { makeCaseInsensitive(); // "Select all columns from this table"; tokens.consume(); // Next position should be line 1, column 8 assertThat(tokens.nextPosition().getIndexInContent(), is(7)); assertThat(tokens.nextPosition().getColumn(), is(8)); assertThat(tokens.nextPosition().getLine(), is(1)); }
@Test public void shouldConvertPositionWhenUnableToParseFullTextSearchExpression() { try { parser.parseFullTextSearchExpression("", new Position(500, 100, 13)); fail("Should have thrown an exception"); } catch (ParsingException e) { assertThat(e.getPosition().getLine(), is(100)); assertThat(e.getPosition().getColumn(), is(13)); assertThat(e.getPosition().getIndexInContent(), is(500)); } }
@Test public void shouldConvertPositionWhenUnableToParseFullTextSearchExpression() { try { parser.parseFullTextSearchExpression("", new Position(500, 100, 13)); fail("Should have thrown an exception"); } catch (ParsingException e) { assertThat(e.getPosition().getLine(), is(100)); assertThat(e.getPosition().getColumn(), is(13)); assertThat(e.getPosition().getIndexInContent(), is(500)); } }
@Test public void shouldFindPreviousPositionStartIndex() { makeCaseInsensitive(); // "Select all columns from this table"; tokens.consume(); tokens.consume(); // previous position should be line 1, column 8 assertThat(tokens.previousPosition().getIndexInContent(), is(7)); assertThat(tokens.previousPosition().getColumn(), is(8)); assertThat(tokens.previousPosition().getLine(), is(1)); }
statementNode.setProperty(DDL_LENGTH, source.length()); statementNode.setProperty(DDL_START_LINE_NUMBER, currentMarkedPosition.getLine()); statementNode.setProperty(DDL_START_CHAR_INDEX, currentMarkedPosition.getIndexInContent()); statementNode.setProperty(DDL_START_COLUMN_NUMBER, currentMarkedPosition.getColumn());
@Test public void shouldFindNextPositionStartIndex() { printTest("shouldFindNextPositionStartIndex()"); String content = "Select all columns from this table"; DdlTokenStream tokens = getTokens(content); // Do the work tokens.start(); // "Select all columns from this table"; assertThat(tokens.nextPosition().getIndexInContent(), is(0)); assertThat(tokens.nextPosition().getColumn(), is(1)); assertThat(tokens.nextPosition().getLine(), is(1)); tokens.consume(); // Next position should be line 1, column 8 assertThat(tokens.nextPosition().getIndexInContent(), is(7)); assertThat(tokens.nextPosition().getColumn(), is(8)); assertThat(tokens.nextPosition().getLine(), is(1)); }
@Test public void shouldParseMultiLineString() { printTest("shouldParseMultiLineString()"); String content = "ALTER DATABASE \n" + "DO SOMETHING; \n" + "ALTER DATABASE \n" + " SET DEFAULT BIGFILE TABLESPACE;"; DdlTokenStream tokens = getTokens(content); // Do the work tokens.start(); tokens.consume(); // LINE tokens.consume(); // ONE tokens.consume(); // DO tokens.consume(); // SOMETHING tokens.consume(); // ; assertThat(tokens.nextPosition().getIndexInContent(), is(31)); assertThat(tokens.nextPosition().getColumn(), is(1)); tokens.consume(); // ALTER assertThat(tokens.nextPosition().getIndexInContent(), is(37)); assertThat(tokens.nextPosition().getColumn(), is(7)); }
@Test public void shouldParseMultiLineString() { makeCaseInsensitive(); String content = "ALTER DATABASE \n" + "DO SOMETHING; \n" + "ALTER DATABASE \n" + " SET DEFAULT BIGFILE TABLESPACE;"; tokens = new TokenStream(content, tokenizer, true); tokens.start(); tokens.consume(); // LINE tokens.consume(); // ONE tokens.consume(); // DO tokens.consume(); // SOMETHING tokens.consume(); // ; assertThat(tokens.nextPosition().getIndexInContent(), is(31)); assertThat(tokens.nextPosition().getColumn(), is(1)); tokens.consume(); // ALTER assertThat(tokens.nextPosition().getIndexInContent(), is(37)); assertThat(tokens.nextPosition().getColumn(), is(7)); } }
@Test public void shouldRegisterKeyWords() { printTest("shouldRegisterKeyWords()"); String content = "CREATE FOOBAR with NO WORDS"; DdlTokenStream tokens = getTokens(content); tokens.registerKeyWords(CUSTOM_KEYWORDS); // Do the work tokens.start(); // Check results assertTrue(tokens.isKeyWord(CREATE)); assertTrue(tokens.isKeyWord(ALTER)); assertTrue(tokens.isKeyWord(DROP)); assertTrue(tokens.isKeyWord(PUNT)); assertTrue(tokens.isKeyWord(FOOBAR)); assertFalse(tokens.isKeyWord("BARNYARD")); assertTrue(tokens.matches(CREATE, FOOBAR)); assertFalse(tokens.matches(PUNT, FOOBAR)); assertTrue(tokens.matches(KEYWORD)); assertTrue(tokens.matches(WORD)); assertTrue(tokens.isNextKeyWord()); assertTrue(tokens.canConsume(CREATE)); // CREATE assertTrue(tokens.isNextKeyWord()); assertTrue(tokens.canConsume(FOOBAR)); // FOOBAR assertThat(tokens.nextPosition().getIndexInContent(), is(14)); assertFalse(tokens.isNextKeyWord()); // "with" }