Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Axiom: Introduced AxiomPrefixedName and stream APIs for it
Signed-off-by: Tony Tkacik <tony.tkacik@evolveum.com>
- Loading branch information
1 parent
e57826c
commit 9786482
Showing
7 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
infra/axiom/src/main/java/com/evolveum/axiom/api/AxiomPrefixedName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.evolveum.axiom.api; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.base.Strings; | ||
|
||
public class AxiomPrefixedName { | ||
|
||
private final @Nullable String prefix; | ||
private final @NotNull String localName; | ||
|
||
private AxiomPrefixedName(@Nullable String prefix, @NotNull String localName) { | ||
this.prefix = Strings.emptyToNull(prefix); | ||
this.localName = Preconditions.checkNotNull(localName, "localName"); | ||
} | ||
|
||
|
||
public static AxiomPrefixedName from(String prefix, String localName) { | ||
return new AxiomPrefixedName(prefix, localName); | ||
} | ||
|
||
public static final boolean isPrefixed(String maybePrefixed) { | ||
// FIXME: Add matching | ||
return maybePrefixed.contains(":"); | ||
} | ||
|
||
public String getPrefix() { | ||
return prefix; | ||
} | ||
|
||
public String getLocalName() { | ||
return localName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return prefix != null ? prefix + ":" + localName : localName; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((localName == null) ? 0 : localName.hashCode()); | ||
result = prime * result + ((prefix == null) ? 0 : prefix.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
AxiomPrefixedName other = (AxiomPrefixedName) obj; | ||
if (localName == null) { | ||
if (other.localName != null) | ||
return false; | ||
} else if (!localName.equals(other.localName)) | ||
return false; | ||
if (prefix == null) { | ||
if (other.prefix != null) | ||
return false; | ||
} else if (!prefix.equals(other.prefix)) | ||
return false; | ||
return true; | ||
} | ||
|
||
|
||
public static AxiomPrefixedName parse(String item) { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
infra/axiom/src/main/java/com/evolveum/axiom/api/stream/AxiomStreamTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.evolveum.axiom.api.stream; | ||
|
||
import com.evolveum.axiom.concepts.SourceLocation; | ||
|
||
public interface AxiomStreamTarget<N> { | ||
|
||
void startItem(N item, SourceLocation loc); | ||
void endItem(SourceLocation loc); | ||
|
||
void startValue(Object value, SourceLocation loc); | ||
void endValue(SourceLocation loc); | ||
|
||
default void startInfra(N item, SourceLocation loc) {}; | ||
default void endInfra(SourceLocation loc) {}; | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
infra/axiom/src/main/java/com/evolveum/axiom/api/stream/PrefixedToQNameTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.evolveum.axiom.api.stream; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import com.evolveum.axiom.api.AxiomName; | ||
import com.evolveum.axiom.api.AxiomPrefixedName; | ||
import com.evolveum.axiom.api.stream.AxiomItemStream.Target; | ||
import com.evolveum.axiom.concepts.SourceLocation; | ||
import com.evolveum.axiom.lang.spi.AxiomNameResolver; | ||
import com.evolveum.axiom.lang.spi.AxiomSemanticException; | ||
import com.google.common.base.Preconditions; | ||
|
||
public class PrefixedToQNameTarget implements AxiomStreamTarget<AxiomPrefixedName> { | ||
|
||
private final AxiomItemStream.Target target; | ||
|
||
private final Supplier<AxiomNameResolver> argumentResolver; | ||
private final Supplier<AxiomNameResolver> itemResolver; | ||
|
||
public PrefixedToQNameTarget(Target target, Supplier<AxiomNameResolver> itemResolver, | ||
Supplier<AxiomNameResolver> valueResolver) { | ||
super(); | ||
this.target = target; | ||
this.itemResolver = itemResolver; | ||
this.argumentResolver = valueResolver; | ||
} | ||
|
||
protected AxiomName convertItemName(AxiomPrefixedName prefixed, SourceLocation loc) { | ||
AxiomName result = itemResolver.get().resolve(prefixed); | ||
AxiomSemanticException.check(result != null, loc, "Unknown item '%s'.", prefixed); | ||
return result; | ||
} | ||
|
||
public void startItem(AxiomPrefixedName item, SourceLocation loc) { | ||
target.startItem(convertItemName(item, loc), loc); | ||
} | ||
public void endItem(SourceLocation loc) { | ||
target.endItem(loc); | ||
} | ||
public void startValue(Object value, SourceLocation loc) { | ||
// FIXME: Do we want to do this? | ||
if(value instanceof AxiomPrefixedName) { | ||
value = Preconditions.checkNotNull(argumentResolver.get().resolve((AxiomPrefixedName) value)); | ||
} | ||
target.startValue(value, loc); | ||
} | ||
public void endValue(SourceLocation loc) { | ||
target.endValue(loc); | ||
} | ||
public void startInfra(AxiomPrefixedName item, SourceLocation loc) { | ||
target.startInfra(convertItemName(item, loc), loc); | ||
} | ||
public void endInfra(SourceLocation loc) { | ||
target.endInfra(loc); | ||
} | ||
|
||
AxiomItemStream.Target qnameTarget() { | ||
return target; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
infra/axiom/src/main/java/com/evolveum/axiom/api/stream/StringToQNameTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.evolveum.axiom.api.stream; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import com.evolveum.axiom.api.AxiomName; | ||
import com.evolveum.axiom.api.AxiomPrefixedName; | ||
import com.evolveum.axiom.api.stream.AxiomItemStream.Target; | ||
import com.evolveum.axiom.concepts.SourceLocation; | ||
import com.evolveum.axiom.lang.spi.AxiomNameResolver; | ||
import com.evolveum.axiom.lang.spi.AxiomSemanticException; | ||
import com.google.common.base.Preconditions; | ||
|
||
public class StringToQNameTarget implements AxiomStreamTarget<String> { | ||
|
||
private PrefixedToQNameTarget target; | ||
|
||
public StringToQNameTarget(Target target, Supplier<AxiomNameResolver> itemResolver, | ||
Supplier<AxiomNameResolver> valueResolver) { | ||
super(); | ||
this.target = new PrefixedToQNameTarget(target, itemResolver, valueResolver); | ||
} | ||
|
||
public void endItem(SourceLocation loc) { | ||
target.endItem(loc); | ||
} | ||
public void startValue(Object value, SourceLocation loc) { | ||
// FIXME: Do we want to do this? | ||
/*if(value instanceof AxiomPrefixedName) { | ||
value = Preconditions.checkNotNull(argumentResolver.get().resolve((AxiomPrefixedName) value)); | ||
}*/ | ||
target.startValue(value, loc); | ||
} | ||
public void endValue(SourceLocation loc) { | ||
target.endValue(loc); | ||
} | ||
|
||
@Override | ||
public void startItem(String item, SourceLocation loc) { | ||
if(AxiomPrefixedName.isPrefixed(item)) { | ||
target.startItem(AxiomPrefixedName.parse(item), loc); | ||
} | ||
target.qnameTarget().startItem(AxiomName.parse(item), loc); | ||
} | ||
|
||
public void startInfra(String item, SourceLocation loc) { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters