/** * {@inheritDoc} */ public Path getNormalizedPath() { if (this.isNormalized()) return this; LinkedList<Segment> newSegments = new LinkedList<Segment>(); for (Segment segment : this) { if (segment.isSelfReference()) continue; if (segment.isParentReference()) { if (newSegments.isEmpty()) { if (this.isAbsolute()) { throw new InvalidPathException(CommonI18n.pathCannotBeNormalized.text(this)); } } else if (!newSegments.getLast().isParentReference()) { newSegments.removeLast(); continue; } } newSegments.add(segment); } if (newSegments.isEmpty()) { if (this.isAbsolute()) return RootPath.INSTANCE; // Otherwise relative and it had contained nothing but self references ... return SELF_PATH; } return new BasicPath(newSegments, this.isAbsolute()); }
@Test public void shouldConsiderEqualTwoSegmentsWithSameNameAndIndex() { segment = new BasicPathSegment(validName); segment2 = new BasicPathSegment(validName); assertThat(segment.equals(segment2), is(true)); assertThat(segment.isSelfReference(), is(false)); assertThat(segment.isParentReference(), is(false)); assertThat(segment.getName(), is(validName)); }
@Test public void shouldNotHaveIndexByDefault() { segment = new BasicPathSegment(validName); assertThat(segment.getIndex(), is(Path.DEFAULT_INDEX)); assertThat(segment.hasIndex(), is(false)); assertThat(segment.isSelfReference(), is(false)); assertThat(segment.isParentReference(), is(false)); assertThat(segment.getName(), is(validName)); }
try { if (segment.isSelfReference()) continue; if (segment.isParentReference()) { node = node.getParent(); assert node != null; // since the relative path is well-formed Path.Segment redoSegment = redoIter.next(); if (redoSegment.isSelfReference()) continue; if (redoSegment.isParentReference()) { redoNode = redoNode.getParent(); assert redoNode != null; // since the relative path is well-formed
/** * {@inheritDoc} */ public Path resolve( Path relativePath ) { CheckArg.isNotNull(relativePath, "relative path"); if (!this.isAbsolute()) { String msg = GraphI18n.pathIsNotAbsolute.text(this); throw new InvalidPathException(msg); } if (relativePath.isAbsolute()) { String msg = GraphI18n.pathIsNotRelative.text(relativePath); throw new InvalidPathException(msg); } // If the relative path is the self or parent reference ... relativePath = relativePath.getNormalizedPath(); if (relativePath.size() == 1) { Segment onlySegment = relativePath.getSegment(0); if (onlySegment.isSelfReference()) return this; if (onlySegment.isParentReference()) return this.getParent(); } List<Segment> segments = new ArrayList<Segment>(this.size() + relativePath.size()); for (Segment segment : this) { segments.add(segment); } for (Segment segment : relativePath) { segments.add(segment); } return new BasicPath(segments, true).getNormalizedPath(); }
@Test public void shouldCreateParentReferenceSegmentIfPassedParentReferenceStringRegardlessOfIndex() { segment = factory.createSegment(Path.PARENT); assertThat(segment.getIndex(), is(Path.DEFAULT_INDEX)); assertThat(segment.hasIndex(), is(false)); assertThat(segment.isSelfReference(), is(false)); assertThat(segment.isParentReference(), is(true)); assertThat(segment.getName().getLocalName(), is(Path.PARENT)); assertThat(segment.getName().getNamespaceUri().length(), is(0)); segment = factory.createSegment(Path.PARENT, 1); assertThat(segment.getIndex(), is(Path.DEFAULT_INDEX)); assertThat(segment.hasIndex(), is(false)); assertThat(segment.isSelfReference(), is(false)); assertThat(segment.isParentReference(), is(true)); assertThat(segment.getName().getLocalName(), is(Path.PARENT)); assertThat(segment.getName().getNamespaceUri().length(), is(0)); }
@Test public void shouldCreateSelfReferenceSegmentIfPassedSelfReferenceStringRegardlessOfIndex() { segment = factory.createSegment(Path.SELF); assertThat(segment.getIndex(), is(Path.DEFAULT_INDEX)); assertThat(segment.hasIndex(), is(false)); assertThat(segment.isSelfReference(), is(true)); assertThat(segment.isParentReference(), is(false)); assertThat(segment.getName().getLocalName(), is(Path.SELF)); assertThat(segment.getName().getNamespaceUri().length(), is(0)); segment = factory.createSegment(Path.SELF, 1); assertThat(segment.getIndex(), is(Path.DEFAULT_INDEX)); assertThat(segment.hasIndex(), is(false)); assertThat(segment.isSelfReference(), is(true)); assertThat(segment.isParentReference(), is(false)); assertThat(segment.getName().getLocalName(), is(Path.SELF)); assertThat(segment.getName().getNamespaceUri().length(), is(0)); }
/** * {@inheritDoc} * * @see org.modeshape.graph.property.Path#isNormalized() */ public boolean isNormalized() { if (child.isSelfReference()) return false; if (!parent.isNormalized()) return false; // Otherwise, the parent is normalized, so this child will be normalized if this child is not a parent reference ... if (!child.isParentReference()) return true; // The path ends with a parent reference. It is normalized only if all other path segments are parent references ... for (Path.Segment segment : parent) { if (!segment.isParentReference()) return false; } return true; }
/** * {@inheritDoc} * * @see org.modeshape.graph.property.Path#isRoot() */ public boolean isRoot() { if (child.isParentReference()) return parent.isRoot(); return false; }
@Test public void shouldHaveIndexSpecifiedInConstructor() { segment = new BasicPathSegment(validName, 3); assertThat(segment.getIndex(), is(3)); assertThat(segment.hasIndex(), is(true)); assertThat(segment.isSelfReference(), is(false)); assertThat(segment.isParentReference(), is(false)); assertThat(segment.getName(), is(validName)); }
protected boolean isNormalized( List<Segment> segments ) { boolean nonParentReference = false; boolean first = isAbsolute(); // only care about first one when it's absolute for (Segment segment : segments) { if (segment.isSelfReference()) return false; if (segment.isParentReference()) { if (nonParentReference || first) return false; } else { nonParentReference = true; } first = false; } return true; }