com.bazaarvoice.jolt.exception.SpecException Java Examples
The following examples show how to use
com.bazaarvoice.jolt.exception.SpecException.
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: ModifierSpec.java From jolt with Apache License 2.0 | 6 votes |
/** * Builds LHS pathElement and validates to specification */ protected ModifierSpec( String rawJsonKey, OpMode opMode ) { String prefix = rawJsonKey.substring( 0, 1 ); String suffix = rawJsonKey.length() > 1 ? rawJsonKey.substring( rawJsonKey.length() - 1 ) : null; if(OpMode.isValid( prefix )) { this.opMode = OpMode.from( prefix ); rawJsonKey = rawJsonKey.substring( 1 ); } else { this.opMode = opMode; } if ( suffix != null && suffix.equals( "?" ) && !( rawJsonKey.endsWith( "\\?" ) ) ) { checkValue = true; rawJsonKey = rawJsonKey.substring( 0, rawJsonKey.length() - 1 ); } else { checkValue = false; } this.pathElement = buildMatchablePathElement( rawJsonKey ); if ( !( pathElement instanceof StarPathElement ) && !( pathElement instanceof LiteralPathElement ) && !( pathElement instanceof ArrayPathElement ) ) { throw new SpecException( opMode.name() + " cannot have " + pathElement.getClass().getSimpleName() + " RHS" ); } }
Example #2
Source File: Modifier.java From jolt with Apache License 2.0 | 6 votes |
@SuppressWarnings( "unchecked" ) private Modifier( Object spec, OpMode opMode, Map<String, Function> functionsMap ) { if ( spec == null ){ throw new SpecException( opMode.name() + " expected a spec of Map type, got 'null'." ); } if ( ! ( spec instanceof Map ) ) { throw new SpecException( opMode.name() + " expected a spec of Map type, got " + spec.getClass().getSimpleName() ); } if(functionsMap == null || functionsMap.isEmpty()) { throw new SpecException( opMode.name() + " expected a populated functions' map type, got " + (functionsMap == null?"null":"empty") ); } functionsMap = Collections.unmodifiableMap( functionsMap ); TemplatrSpecBuilder templatrSpecBuilder = new TemplatrSpecBuilder( opMode, functionsMap ); rootSpec = new ModifierCompositeSpec( ROOT_KEY, (Map<String, Object>) spec, opMode, templatrSpecBuilder ); }
Example #3
Source File: RemovrCompositeSpec.java From jolt with Apache License 2.0 | 6 votes |
public RemovrCompositeSpec(String rawKey, Map<String, Object> spec ) { super( rawKey ); List<RemovrSpec> all = new ArrayList<>(); for ( String rawLhsStr : spec.keySet() ) { Object rawRhs = spec.get( rawLhsStr ); String[] keyStrings = rawLhsStr.split( "\\|" ); for ( String keyString : keyStrings ) { RemovrSpec childSpec; if( rawRhs instanceof Map ) { childSpec = new RemovrCompositeSpec(keyString, (Map<String, Object>) rawRhs ); } else if (rawRhs instanceof String && ((String)rawRhs).trim().length() == 0) { childSpec = new RemovrLeafSpec(keyString); } else{ throw new SpecException("Invalid Removr spec RHS. Should be an empty string or Map"); } all.add(childSpec); } } allChildNodes = Collections.unmodifiableList( all ); }
Example #4
Source File: ChainrEntry.java From jolt with Apache License 2.0 | 6 votes |
private Class<? extends JoltTransform> loadJoltTransformClass(ClassLoader classLoader) { try { Class opClass = classLoader.loadClass( operationClassName ); if ( Chainr.class.isAssignableFrom( opClass ) ) { throw new SpecException( "Attempt to nest Chainr inside itself" + getErrorMessageIndexSuffix() ); } if ( ! JoltTransform.class.isAssignableFrom( opClass ) ) { throw new SpecException( "JOLT Chainr class:" + operationClassName + " does not implement the JoltTransform interface" + getErrorMessageIndexSuffix() ); } @SuppressWarnings( "unchecked" ) // We know it is some type of Transform due to the check above Class<? extends JoltTransform> transformClass = (Class<? extends JoltTransform>) opClass; return transformClass; } catch ( ClassNotFoundException e ) { throw new SpecException( "JOLT Chainr could not find transform class:" + operationClassName + getErrorMessageIndexSuffix(), e ); } }
Example #5
Source File: ChainrEntry.java From jolt with Apache License 2.0 | 6 votes |
private String extractOperationString( Map<String, Object> chainrEntryMap ) { Object operationNameObj = chainrEntryMap.get( ChainrEntry.OPERATION_KEY ); if ( operationNameObj == null ) { return null; } else if ( operationNameObj instanceof String) { if ( StringTools.isBlank((String) operationNameObj) ) { throw new SpecException( "JOLT Chainr '" + ChainrEntry.OPERATION_KEY + "' should not be blank" + getErrorMessageIndexSuffix() ); } return (String) operationNameObj; } else { throw new SpecException( "JOLT Chainr needs a '" + ChainrEntry.OPERATION_KEY + "' of type String" + getErrorMessageIndexSuffix() ); } }
Example #6
Source File: TraversalBuilder.java From jolt with Apache License 2.0 | 6 votes |
public <T extends PathEvaluatingTraversal> T build( Object rawObj ) { if ( ! ( rawObj instanceof String ) ) { throw new SpecException( "Invalid spec, RHS should be a String or array of Strings. Value in question : " + rawObj ); } // Prepend "root" to each output path. // This is needed for the "identity" transform, eg if we are just supposed to put the input into the output // what key do we put it under? String outputPathStr = (String) rawObj; if ( StringTools.isBlank( outputPathStr ) ) { outputPathStr = SpecDriven.ROOT_KEY; } else { outputPathStr = SpecDriven.ROOT_KEY + "." + outputPathStr; } return buildFromPath( outputPathStr ); }
Example #7
Source File: BasePathReference.java From jolt with Apache License 2.0 | 6 votes |
public BasePathReference( String refStr ) { if ( refStr == null || refStr.length() == 0 || getToken() != refStr.charAt( 0 ) ) { throw new SpecException( "Invalid reference key=" + refStr + " either blank or doesn't start with correct character=" + getToken() ); } int pathIndex = 0; try { if ( refStr.length() > 1 ) { String meat = refStr.substring( 1 ); pathIndex = Integer.parseInt( meat ); } } catch( NumberFormatException nfe ) { throw new SpecException( "Unable to parse '" + getToken() + "' reference key:" + refStr, nfe ); } if ( pathIndex < 0 ) { throw new SpecException( "Reference:" + refStr + " can not have a negative value." ); } this.pathIndex = pathIndex; }
Example #8
Source File: GuicedChainrTest.java From jolt with Apache License 2.0 | 6 votes |
@Test( expectedExceptions = SpecException.class ) public void itBlowsUpForMissingProviderStockTransform() throws IOException { String testPath = "/json/chainr/guice_spec.json"; Map<String, Object> testUnit = JsonUtils.classpathToMap( testPath ); Object spec = testUnit.get( "spec" ); Module parentModule = new AbstractModule() { @Override protected void configure() { } @Provides public GuiceSpecDrivenTransform.GuiceConfig getConfigD() { return new GuiceSpecDrivenTransform.GuiceConfig( "dd" ); } }; Chainr.fromSpec( spec, new GuiceChainrInstantiator( parentModule ) ); }
Example #9
Source File: GuicedChainrTest.java From jolt with Apache License 2.0 | 6 votes |
@Test( expectedExceptions = SpecException.class ) public void itBlowsUpForMissingProviderSpecTransform() throws IOException { String testPath = "/json/chainr/guice_spec.json"; Map<String, Object> testUnit = JsonUtils.classpathToMap( testPath ); Object spec = testUnit.get( "spec" ); Module parentModule = new AbstractModule() { @Override protected void configure() { } @Provides public GuiceTransform.GuiceConfig getConfigC() { return new GuiceTransform.GuiceConfig( "c", "cc" ); } }; Chainr.fromSpec( spec, new GuiceChainrInstantiator( parentModule ) ); }
Example #10
Source File: CardinalityTransformTest.java From jolt with Apache License 2.0 | 5 votes |
@Test(expectedExceptions=SpecException.class) public void testSpecExceptions() throws IOException { String testPath = "/json/cardinality/failCardinalityType"; Map<String, Object> testUnit = JsonUtils.classpathToMap( testPath + ".json" ); Object spec = testUnit.get( "spec" ); // Should throw exception new CardinalityTransform( spec ); }
Example #11
Source File: TransposePathElement.java From jolt with Apache License 2.0 | 5 votes |
/** * Parse the core of the TransposePathElement key, once basic errors have been checked and * syntax has been handled. * * @param originalKey The original text for reference. * @param meat The string to actually parse into a TransposePathElement * @return TransposePathElement */ private static TransposePathElement innerParse( String originalKey, String meat ) { char first = meat.charAt( 0 ); if ( Character.isDigit( first ) ) { // loop until we find a comma or end of string StringBuilder sb = new StringBuilder().append( first ); for ( int index = 1; index < meat.length(); index++ ) { char c = meat.charAt( index ); // when we find a / the first comma, stop looking for integers, and just assume the rest is a String path if( ',' == c ) { int upLevel; try { upLevel = Integer.valueOf( sb.toString() ); } catch ( NumberFormatException nfe ) { // I don't know how this exception would get thrown, as all the chars were checked by isDigit, but oh well throw new SpecException( "@ path element with non/mixed numeric key is not valid, key=" + originalKey ); } return new TransposePathElement( originalKey, upLevel, meat.substring( index + 1 ) ); } else if ( Character.isDigit( c ) ) { sb.append( c ); } else { throw new SpecException( "@ path element with non/mixed numeric key is not valid, key=" + originalKey ); } } // if we got out of the for loop, then the whole thing was a number. return new TransposePathElement( originalKey, Integer.valueOf( sb.toString() ), null ); } else { return new TransposePathElement( originalKey, 0, meat ); } }
Example #12
Source File: RemovrSpec.java From jolt with Apache License 2.0 | 5 votes |
public RemovrSpec(String rawJsonKey) { PathElement pathElement = parse( rawJsonKey ); if (!(pathElement instanceof MatchablePathElement)) { throw new SpecException("Spec LHS key=" + rawJsonKey + " is not a valid LHS key."); } this.pathElement = (MatchablePathElement) pathElement; }
Example #13
Source File: Chainr.java From jolt with Apache License 2.0 | 5 votes |
public Chainr( List<JoltTransform> joltTransforms ) { if ( joltTransforms == null ) { throw new IllegalArgumentException( "Chainr requires a list of JoltTransforms." ); } transformsList = new ArrayList<>( joltTransforms.size() ); List<ContextualTransform> realContextualTransforms = new LinkedList<>(); for ( JoltTransform joltTransform : joltTransforms ) { // Do one pass of "instanceof" checks at construction time, rather than repeatedly at "runtime". boolean isTransform = joltTransform instanceof Transform; boolean isContextual = joltTransform instanceof ContextualTransform; if ( isContextual && isTransform ) { throw new SpecException( "JOLT Chainr - JoltTransform className:" + joltTransform.getClass().getCanonicalName() + " implements both Transform and ContextualTransform, should only implement one of those interfaces." ); } if ( ! isContextual && ! isTransform ) { throw new SpecException( "JOLT Chainr - Transform className:" + joltTransform.getClass().getCanonicalName() + " should implement Transform or ContextualTransform." ); } // We are optimizing given the assumption that Chainr objects will be built and then reused many times. // We want to have a single list of "transforms" that we can just blindly march through. // In order to accomplish this, we adapt Transforms to look like ContextualTransforms and just maintain // a list of type ContextualTransform. if ( isContextual ) { transformsList.add( (ContextualTransform) joltTransform ); realContextualTransforms.add( (ContextualTransform) joltTransform ); } else { transformsList.add( new ContextualTransformAdapter( (Transform) joltTransform ) ); } } actualContextualTransforms = Collections.unmodifiableList( realContextualTransforms ); }
Example #14
Source File: AtPathElement.java From jolt with Apache License 2.0 | 5 votes |
public AtPathElement( String key ) { super(key); if ( ! "@".equals( key ) ) { throw new SpecException( "'References Input' key '@', can only be a single '@'. Offending key : " + key ); } }
Example #15
Source File: Removr.java From jolt with Apache License 2.0 | 5 votes |
@Inject public Removr( Object spec ) { if ( spec == null ){ throw new SpecException( "Removr expected a spec of Map type, got 'null'." ); } if ( ! ( spec instanceof Map ) ) { throw new SpecException( "Removr expected a spec of Map type, got " + spec.getClass().getSimpleName() ); } rootSpec = new RemovrCompositeSpec( ROOT_KEY, (Map<String, Object>) spec ); }
Example #16
Source File: ChainrSpecLoadingTest.java From jolt with Apache License 2.0 | 5 votes |
@Test(dataProvider = "badFormatSpecs", expectedExceptions = SpecException.class ) public void testBadSpecs( Object chainrSpecObj ) { ChainrSpec chainrSpec = new ChainrSpec( chainrSpecObj ); ChainrEntry chainrEntry = chainrSpec.getChainrEntries().get( 0 ); DefaultChainrInstantiator instantiator = new DefaultChainrInstantiator(); // This should fail instantiator.hydrateTransform( chainrEntry ); }
Example #17
Source File: PathElementBuilder.java From jolt with Apache License 2.0 | 5 votes |
/** * Create a path element and ensures it is a Matchable Path Element */ public static MatchablePathElement buildMatchablePathElement(String rawJsonKey) { PathElement pe = PathElementBuilder.parseSingleKeyLHS( rawJsonKey ); if ( ! ( pe instanceof MatchablePathElement ) ) { throw new SpecException( "Spec LHS key=" + rawJsonKey + " is not a valid LHS key." ); } return (MatchablePathElement) pe; }
Example #18
Source File: ChainrInitializationTest.java From jolt with Apache License 2.0 | 5 votes |
@Test( expectedExceptions = SpecException.class ) public void failsOnOverEagerTransform() { List<JoltTransform> badSpec = Lists.newArrayList(); // Stupid JoltTransform that implements both "real" interfaces badSpec.add( new OverEagerTransform() ); new Chainr( badSpec ); }
Example #19
Source File: TransformFactory.java From nifi with Apache License 2.0 | 5 votes |
protected static List<JoltTransform> getChainrJoltTransformations(ClassLoader classLoader, Object specJson) throws Exception{ if(!(specJson instanceof List)) { throw new SpecException("JOLT Chainr expects a JSON array of objects - Malformed spec."); } else { List operations = (List)specJson; if(operations.isEmpty()) { throw new SpecException("JOLT Chainr passed an empty JSON array."); } else { ArrayList<JoltTransform> entries = new ArrayList<>(operations.size()); for(Object chainrEntryObj : operations) { if(!(chainrEntryObj instanceof Map)) { throw new SpecException("JOLT ChainrEntry expects a JSON map - Malformed spec"); } else { Map chainrEntryMap = (Map)chainrEntryObj; String opString = (String) chainrEntryMap.get("operation"); String operationClassName; if(opString == null) { throw new SpecException("JOLT Chainr \'operation\' must implement Transform or ContextualTransform"); } else { operationClassName = ChainrEntry.STOCK_TRANSFORMS.getOrDefault(opString, opString); entries.add(getCustomTransform(classLoader,operationClassName,chainrEntryMap.get("spec"))); } } } return entries; } } }
Example #20
Source File: PathElementBuilder.java From jolt with Apache License 2.0 | 5 votes |
/** * @param refDotNotation the original dotNotation string used for error messages * @return List of PathElements based on the provided List<String> keys */ public static List<PathElement> parseList( List<String> keys, String refDotNotation ) { ArrayList<PathElement> paths = new ArrayList<>(); for( String key: keys ) { PathElement path = parseSingleKeyLHS( key ); if ( path instanceof AtPathElement ) { throw new SpecException( "'.@.' is not valid on the RHS: " + refDotNotation ); } paths.add( path ); } return paths; }
Example #21
Source File: ChainrInitializationTest.java From jolt with Apache License 2.0 | 5 votes |
@Test( expectedExceptions = SpecException.class ) public void failsOnStupidTransform() { List<JoltTransform> badSpec = Lists.newArrayList(); // Stupid JoltTransform that implements the base interface, and not one of the useful ones badSpec.add( new JoltTransform() {} ); new Chainr( badSpec ); }
Example #22
Source File: HashPathElement.java From jolt with Apache License 2.0 | 5 votes |
public HashPathElement( String key ) { super(key); if ( StringTools.isBlank( key ) ) { throw new SpecException( "HashPathElement cannot have empty String as input." ); } if ( ! key.startsWith( "#" ) ) { throw new SpecException( "LHS # should start with a # : " + key ); } if ( key.length() <= 1 ) { throw new SpecException( "HashPathElement input is too short : " + key ); } if ( key.charAt( 1 ) == '(' ) { if ( key.charAt( key.length() -1 ) == ')' ) { keyValue = key.substring( 2, key.length() -1 ); } else { throw new SpecException( "HashPathElement, mismatched parens : " + key ); } } else { keyValue = key.substring( 1 ); } }
Example #23
Source File: TransposePathElement.java From jolt with Apache License 2.0 | 5 votes |
/** * Parse a text value from a Spec, into a TransposePathElement. * * @param key rawKey from a Jolt Spec file * @return a TransposePathElement */ public static TransposePathElement parse( String key ) { if ( key == null || key.length() < 2 ) { throw new SpecException( "'Transpose Input' key '@', can not be null or of length 1. Offending key : " + key ); } if ( '@' != key.charAt( 0 ) ) { throw new SpecException( "'Transpose Input' key must start with an '@'. Offending key : " + key ); } // Strip off the leading '@' as we don't need it anymore. String meat = key.substring( 1 ); if ( meat.contains( "@" ) ) { throw new SpecException( "@ pathElement can not contain a nested @. Was: " + meat ); } if ( meat.contains( "*" ) || meat.contains( "[]" ) ) { throw new SpecException( "'Transpose Input' can not contain expansion wildcards (* and []). Offending key : " + key ); } // Check to see if the key is wrapped by parens if ( meat.startsWith( "(" ) ) { if ( meat.endsWith( ")" ) ) { meat = meat.substring( 1, meat.length() - 1 ); } else { throw new SpecException( "@ path element that starts with '(' must have a matching ')'. Offending key : " + key ); } } return innerParse( key, meat ); }
Example #24
Source File: DefaultChainrInstantiator.java From jolt with Apache License 2.0 | 5 votes |
@Override public JoltTransform hydrateTransform( ChainrEntry entry ) { Object spec = entry.getSpec(); Class<? extends JoltTransform> transformClass = entry.getJoltTransformClass(); try { // If the transform class is a SpecTransform, we try to construct it with the provided spec. if ( entry.isSpecDriven() ) { try { // Lookup a Constructor with a Single "Object" arg. Constructor constructor = transformClass.getConstructor( Object.class ); return (JoltTransform) constructor.newInstance( spec ); } catch ( NoSuchMethodException nsme ) { // This means the transform class "violated" the SpecTransform marker interface throw new SpecException( "JOLT Chainr encountered an exception constructing SpecTransform className:" + transformClass.getCanonicalName() + ". Specifically, no single arg constructor found" + entry.getErrorMessageIndexSuffix(), nsme ); } } else { // The opClass is just a Transform, so just create a newInstance of it. return transformClass.newInstance(); } } catch ( Exception e ) { // FYI 3 exceptions are known to be thrown here // IllegalAccessException, InvocationTargetException, InstantiationException throw new SpecException( "JOLT Chainr encountered an exception constructing Transform className:" + transformClass.getCanonicalName() + entry.getErrorMessageIndexSuffix(), e ); } }
Example #25
Source File: Shiftr.java From jolt with Apache License 2.0 | 5 votes |
/** * Initialize a Shiftr transform with a Spec. * * @throws com.bazaarvoice.jolt.exception.SpecException for a malformed spec */ @Inject public Shiftr( Object spec ) { if ( spec == null ){ throw new SpecException( "Shiftr expected a spec of Map type, got 'null'." ); } if ( ! ( spec instanceof Map ) ) { throw new SpecException( "Shiftr expected a spec of Map type, got " + spec.getClass().getSimpleName() ); } rootSpec = new ShiftrCompositeSpec( ROOT_KEY, (Map<String, Object>) spec ); }
Example #26
Source File: ChainrEntry.java From jolt with Apache License 2.0 | 5 votes |
/** * Process an element from the Chainr Spec into a ChainrEntry class. * This method tries to validate the syntax of the Chainr spec, whereas * the ChainrInstantiator deals with loading the Transform classes. * * @param chainrEntryObj the unknown Object from the Chainr list * @param index the index of the chainrEntryObj, used in reporting errors */ public ChainrEntry( int index, Object chainrEntryObj, ClassLoader classLoader ) { if ( ! (chainrEntryObj instanceof Map ) ) { throw new SpecException( "JOLT ChainrEntry expects a JSON map - Malformed spec" + getErrorMessageIndexSuffix() ); } @SuppressWarnings( "unchecked" ) // We know it is a Map due to the check above Map<String,Object> chainrEntryMap = (Map<String, Object>) chainrEntryObj; this.index = index; String opString = extractOperationString( chainrEntryMap ); if ( opString == null ) { throw new SpecException( "JOLT Chainr 'operation' must implement Transform or ContextualTransform" + getErrorMessageIndexSuffix() ); } if ( STOCK_TRANSFORMS.containsKey( opString ) ) { operationClassName = STOCK_TRANSFORMS.get( opString ); } else { operationClassName = opString; } joltTransformClass = loadJoltTransformClass( classLoader ); spec = chainrEntryMap.get( ChainrEntry.SPEC_KEY ); isSpecDriven = SpecDriven.class.isAssignableFrom( joltTransformClass ); if ( isSpecDriven && ! chainrEntryMap.containsKey( SPEC_KEY ) ) { throw new SpecException( "JOLT Chainr - Transform className:" + joltTransformClass.getName() + " requires a spec" + getErrorMessageIndexSuffix() ); } }
Example #27
Source File: CardinalitySpec.java From jolt with Apache License 2.0 | 5 votes |
public CardinalitySpec( String rawJsonKey ) { List<PathElement> pathElements = parse( rawJsonKey ); if ( pathElements.size() != 1 ){ throw new SpecException( "CardinalityTransform invalid LHS:" + rawJsonKey + " can not contain '.'" ); } PathElement pe = pathElements.get( 0 ); if ( ! ( pe instanceof MatchablePathElement ) ) { throw new SpecException( "Spec LHS key=" + rawJsonKey + " is not a valid LHS key." ); } this.pathElement = (MatchablePathElement) pe; }
Example #28
Source File: RemovrTest.java From jolt with Apache License 2.0 | 5 votes |
@Test(dataProvider = "getNegativeTestCaseNames", expectedExceptions = SpecException.class) public void runNegativeTestCases(String testCaseName) throws IOException { String testPath = "/json/removr/" + testCaseName; Map<String, Object> testUnit = JsonUtils.classpathToMap( testPath + ".json" ); Object spec = testUnit.get( "spec" ); new Removr( spec ); }
Example #29
Source File: CardinalityLeafSpec.java From jolt with Apache License 2.0 | 5 votes |
public CardinalityLeafSpec( String rawKey, Object rhs ) { super( rawKey ); try { cardinalityRelationship = CardinalityRelationship.valueOf( rhs.toString() ); } catch( Exception e ) { throw new SpecException( "Invalid Cardinality type :" + rhs.toString(), e ); } }
Example #30
Source File: FunctionArg.java From jolt with Apache License 2.0 | 5 votes |
private SelfLookupArg( PathEvaluatingTraversal traversal ) { PathElement pathElement = traversal.get( traversal.size() - 1 ); if(pathElement instanceof TransposePathElement ) { this.pathElement = (TransposePathElement) pathElement; } else { throw new SpecException( "Expected @ path element here" ); } }