Skip to content

Commit

Permalink
Merge pull request #208 from milosimpson/escaping5
Browse files Browse the repository at this point in the history
Enabled all Jolt special chars to be escaped.
  • Loading branch information
Milo Simpson committed May 9, 2016
2 parents 1a2531d + c8e6ce6 commit 5670588
Show file tree
Hide file tree
Showing 37 changed files with 625 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.bazaarvoice.jolt.cardinality.CardinalityCompositeSpec;
import com.bazaarvoice.jolt.exception.SpecException;
import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.tree.WalkedPath;

import javax.inject.Inject;
import java.util.Map;
Expand Down
6 changes: 3 additions & 3 deletions jolt-core/src/main/java/com/bazaarvoice/jolt/Shiftr.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.bazaarvoice.jolt;

import com.bazaarvoice.jolt.exception.SpecException;
import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.pathelement.LiteralPathElement;
import com.bazaarvoice.jolt.common.tree.WalkedPath;
import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.shiftr.spec.ShiftrCompositeSpec;

import javax.inject.Inject;
Expand Down Expand Up @@ -498,7 +498,7 @@ public Object transform( Object input ) {
Map<String,Object> output = new HashMap<>();

// Create a root LiteralPathElement so that # is useful at the root level
LiteralPathElement rootLpe = new LiteralPathElement( ROOT_KEY );
MatchedElement rootLpe = new MatchedElement( ROOT_KEY );
WalkedPath walkedPath = new WalkedPath();
walkedPath.add( input, rootLpe );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
package com.bazaarvoice.jolt.cardinality;

import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.tree.WalkedPath;
import com.bazaarvoice.jolt.common.pathelement.AmpPathElement;
import com.bazaarvoice.jolt.common.pathelement.AtPathElement;
import com.bazaarvoice.jolt.common.pathelement.LiteralPathElement;
import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.common.pathelement.PathElement;
import com.bazaarvoice.jolt.common.pathelement.LiteralPathElement;
import com.bazaarvoice.jolt.common.pathelement.StarPathElement;
import com.bazaarvoice.jolt.exception.SpecException;

Expand Down Expand Up @@ -136,7 +137,7 @@ private static List<CardinalitySpec> createChildren( Map<String, Object> rawSpec
*/
@Override
public boolean apply( String inputKey, Object input, WalkedPath walkedPath, Object parentContainer ) {
LiteralPathElement thisLevel = pathElement.match( inputKey, walkedPath );
MatchedElement thisLevel = pathElement.match( inputKey, walkedPath );
if ( thisLevel == null ) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package com.bazaarvoice.jolt.cardinality;

import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.pathelement.LiteralPathElement;
import com.bazaarvoice.jolt.common.tree.WalkedPath;
import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.exception.SpecException;

import java.util.ArrayList;
Expand Down Expand Up @@ -58,7 +58,7 @@ public CardinalityLeafSpec( String rawKey, Object rhs ) {
@Override
public boolean apply( String inputKey, Object input, WalkedPath walkedPath, Object parentContainer ) {

LiteralPathElement thisLevel = getMatch( inputKey, walkedPath );
MatchedElement thisLevel = getMatch( inputKey, walkedPath );
if ( thisLevel == null ) {
return false;
}
Expand All @@ -73,7 +73,7 @@ public boolean apply( String inputKey, Object input, WalkedPath walkedPath, Obje
*/
public Object applyToParentContainer ( String inputKey, Object input, WalkedPath walkedPath, Object parentContainer ) {

LiteralPathElement thisLevel = getMatch( inputKey, walkedPath );
MatchedElement thisLevel = getMatch( inputKey, walkedPath );
if ( thisLevel == null ) {
return null;
}
Expand All @@ -84,7 +84,7 @@ public Object applyToParentContainer ( String inputKey, Object input, WalkedPath
*
* @return null if no work was done, otherwise returns the re-parented data
*/
private Object performCardinalityAdjustment( String inputKey, Object input, WalkedPath walkedPath, Map parentContainer, LiteralPathElement thisLevel ) {
private Object performCardinalityAdjustment( String inputKey, Object input, WalkedPath walkedPath, Map parentContainer, MatchedElement thisLevel ) {

// Add our the LiteralPathElement for this level, so that write path References can use it as &(0,0)
walkedPath.add( input, thisLevel );
Expand Down Expand Up @@ -120,7 +120,7 @@ else if ( cardinalityRelationship == CardinalityRelationship.ONE ) {
return returnValue;
}

private LiteralPathElement getMatch( String inputKey, WalkedPath walkedPath ) {
private MatchedElement getMatch( String inputKey, WalkedPath walkedPath ) {
return pathElement.match( inputKey, walkedPath );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package com.bazaarvoice.jolt.cardinality;

import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.tree.WalkedPath;
import com.bazaarvoice.jolt.common.pathelement.AtPathElement;
import com.bazaarvoice.jolt.common.pathelement.LiteralPathElement;
import com.bazaarvoice.jolt.common.pathelement.MatchablePathElement;
import com.bazaarvoice.jolt.common.pathelement.PathElement;
import com.bazaarvoice.jolt.common.pathelement.LiteralPathElement;
import com.bazaarvoice.jolt.common.pathelement.StarAllPathElement;
import com.bazaarvoice.jolt.common.pathelement.StarRegexPathElement;
import com.bazaarvoice.jolt.common.pathelement.StarSinglePathElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package com.bazaarvoice.jolt.common.pathelement;

import com.bazaarvoice.jolt.common.reference.AmpReference;
import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.common.tree.WalkedPath;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -114,8 +115,8 @@ public String evaluate( WalkedPath walkedPath ) {
}
else {
AmpReference ref = (AmpReference) token;
LiteralPathElement literalPathElement = walkedPath.elementFromEnd( ref.getPathIndex() ).getLiteralPathElement();
String value = literalPathElement.getSubKeyRef( ref.getKeyGroup() );
MatchedElement matchedElement = walkedPath.elementFromEnd( ref.getPathIndex() ).getMatchedElement();
String value = matchedElement.getSubKeyRef( ref.getKeyGroup() );
output.append( value );
}
}
Expand All @@ -124,10 +125,10 @@ public String evaluate( WalkedPath walkedPath ) {
}

@Override
public LiteralPathElement match( String dataKey, WalkedPath walkedPath ) {
public MatchedElement match( String dataKey, WalkedPath walkedPath ) {
String evaled = evaluate( walkedPath );
if ( evaled.equals( dataKey ) ) {
return new LiteralPathElement( evaled );
return new MatchedElement( evaled );
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.bazaarvoice.jolt.common.pathelement;

import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.exception.SpecException;
import com.bazaarvoice.jolt.common.reference.AmpReference;
import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.tree.WalkedPath;
import com.bazaarvoice.jolt.common.reference.HashReference;
import com.bazaarvoice.jolt.common.reference.PathAndGroupReference;
import com.bazaarvoice.jolt.common.reference.PathReference;
Expand Down Expand Up @@ -106,7 +107,7 @@ public String evaluate( WalkedPath walkedPath ) {
return arrayIndex;

case HASH:
LiteralPathElement element = walkedPath.elementFromEnd( ref.getPathIndex() ).getLiteralPathElement();
MatchedElement element = walkedPath.elementFromEnd( ref.getPathIndex() ).getMatchedElement();
Integer index = element.getHashCount();
return index.toString();

Expand All @@ -115,7 +116,7 @@ public String evaluate( WalkedPath walkedPath ) {
return verifyStringIsNonNegativeInteger( key );

case REFERENCE:
LiteralPathElement lpe = walkedPath.elementFromEnd( ref.getPathIndex() ).getLiteralPathElement();
MatchedElement lpe = walkedPath.elementFromEnd( ref.getPathIndex() ).getMatchedElement();
String keyPart;

if ( ref instanceof PathAndGroupReference ) {
Expand Down Expand Up @@ -153,10 +154,10 @@ private static String verifyStringIsNonNegativeInteger( String key ) {
}

@Override
public LiteralPathElement match( String dataKey, WalkedPath walkedPath ) {
public MatchedElement match( String dataKey, WalkedPath walkedPath ) {
String evaled = evaluate( walkedPath );
if ( evaled.equals( dataKey ) ) {
return new LiteralPathElement( evaled );
return new MatchedElement( evaled );
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/
package com.bazaarvoice.jolt.common.pathelement;

import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.exception.SpecException;
import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.tree.WalkedPath;

public class AtPathElement extends BasePathElement implements MatchablePathElement {
public AtPathElement( String key ) {
Expand All @@ -27,8 +28,8 @@ public AtPathElement( String key ) {
}
}

public LiteralPathElement match( String dataKey, WalkedPath walkedPath ) {
return walkedPath.lastElement().getLiteralPathElement(); // copy what our parent was so that write keys of &0 and &1 both work.
public MatchedElement match( String dataKey, WalkedPath walkedPath ) {
return walkedPath.lastElement().getMatchedElement(); // copy what our parent was so that write keys of &0 and &1 both work.
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package com.bazaarvoice.jolt.common.pathelement;

import com.bazaarvoice.jolt.common.reference.DollarReference;
import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.common.tree.WalkedPath;

public class DollarPathElement extends BasePathElement implements MatchablePathElement, EvaluatablePathElement {

Expand All @@ -35,13 +36,13 @@ public String getCanonicalForm() {

@Override
public String evaluate( WalkedPath walkedPath ) {
LiteralPathElement pe = walkedPath.elementFromEnd( dRef.getPathIndex() ).getLiteralPathElement();
MatchedElement pe = walkedPath.elementFromEnd( dRef.getPathIndex() ).getMatchedElement();
return pe.getSubKeyRef( dRef.getKeyGroup() );
}

@Override
public LiteralPathElement match( String dataKey, WalkedPath walkedPath ) {
public MatchedElement match( String dataKey, WalkedPath walkedPath ) {
String evaled = evaluate( walkedPath );
return new LiteralPathElement( evaled );
return new MatchedElement( evaled );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.bazaarvoice.jolt.common.pathelement;

import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.tree.WalkedPath;

public interface EvaluatablePathElement extends PathElement {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package com.bazaarvoice.jolt.common.pathelement;

import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.common.tree.WalkedPath;
import com.bazaarvoice.jolt.exception.SpecException;
import com.bazaarvoice.jolt.utils.StringTools;

Expand Down Expand Up @@ -62,7 +63,7 @@ public String getCanonicalForm() {
}

@Override
public LiteralPathElement match( String dataKey, WalkedPath walkedPath ) {
return new LiteralPathElement( keyValue );
public MatchedElement match( String dataKey, WalkedPath walkedPath ) {
return new MatchedElement( keyValue );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,21 @@
*/
package com.bazaarvoice.jolt.common.pathelement;

import com.bazaarvoice.jolt.common.WalkedPath;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.common.tree.WalkedPath;

/**
* Meant to be an immutable PathElement from a Spec, and therefore shareable across
* threads running multiple transforms using the same spec.
*/
public class LiteralPathElement extends BasePathElement implements MatchablePathElement, EvaluatablePathElement {

private final List<String> subKeys;

private int hashCount = 0;
private final String canonicalForm;

public LiteralPathElement( String key ) {
super(key);

List<String> keys = new ArrayList<>(1);
keys.add( key ); // always add the full key to index 0

this.subKeys = Collections.unmodifiableList( keys );
}

public LiteralPathElement( String key, List<String> subKeys ) {
super(key);

if ( subKeys == null ) {
throw new IllegalArgumentException( "LiteralPathElement for key:" + key + " got null list of subKeys" );
}

List<String> keys = new ArrayList<>( 1 + subKeys.size() );
keys.add( key ); // always add the full key to index 0
keys.addAll( subKeys );

this.subKeys = Collections.unmodifiableList( keys );
this.canonicalForm = key.replace( ".", "\\." );
}

@Override
Expand All @@ -56,34 +38,15 @@ public String evaluate( WalkedPath walkedPath ) {
}

@Override
public LiteralPathElement match( String dataKey, WalkedPath walkedPath ) {
public MatchedElement match( String dataKey, WalkedPath walkedPath ) {
if ( getRawKey().equals( dataKey ) ) {
return new LiteralPathElement( getRawKey(), subKeys );
return new MatchedElement( getRawKey() );
}
return null;
}

@Override
public String getCanonicalForm() {
return getRawKey();
}

public String getSubKeyRef( int index ) {
if ((index < 0) || (index >= this.subKeys.size())) {
throw new IndexOutOfBoundsException( "LiteralPathElement "+ this.subKeys +" cannot be indexed with index "+index );
}
return subKeys.get( index );
}

public int getSubKeyCount(){
return subKeys.size();
}

public int getHashCount() {
return hashCount;
}

public void incrementHashCount() {
hashCount++;
return canonicalForm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package com.bazaarvoice.jolt.common.pathelement;

import com.bazaarvoice.jolt.common.WalkedPath;
import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.common.tree.WalkedPath;

public interface MatchablePathElement extends PathElement {

Expand All @@ -28,5 +29,5 @@ public interface MatchablePathElement extends PathElement {
* @param walkedPath "up the tree" list of LiteralPathElements, that may be used by this key as it is computing its match
* @return null or a matched LiteralPathElement
*/
LiteralPathElement match( String dataKey, WalkedPath walkedPath );
MatchedElement match( String dataKey, WalkedPath walkedPath );
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package com.bazaarvoice.jolt.common.pathelement;

import com.bazaarvoice.jolt.common.WalkedPath;

import java.util.Collections;
import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.common.tree.WalkedPath;

/**
* PathElement for the lone "*" wildcard. In this case we can avoid doing any
Expand All @@ -41,8 +40,8 @@ public boolean stringMatch( String literal ) {
}

@Override
public LiteralPathElement match( String dataKey, WalkedPath walkedPath ) {
return new LiteralPathElement(dataKey, Collections.<String>emptyList() );
public MatchedElement match( String dataKey, WalkedPath walkedPath ) {
return new MatchedElement( dataKey );
}

@Override
Expand Down
Loading

0 comments on commit 5670588

Please sign in to comment.