Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2013, Francis Galiegue <fgaliegue@gmail.com>
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the Lesser GNU General Public License as
   * published by the Free Software Foundation, either version 3 of the
   * License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * Lesser GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package com.github.fge.jsonschema.processing.ref;
 
 
 import java.util.Set;
 
 public final class RefResolverProcessor
 {
     private final SchemaLoader loader;
 
     public RefResolverProcessor(final SchemaLoader loader)
     {
         this. = loader;
     }

    
Resolve JSON Reference for the current schema context

All errors encountered at this level are fatal.

Parameters:
context the context
Returns:
a new schema tree
Throws:
com.github.fge.jsonschema.processing.ProcessingException ref loop, unresolvable ref, not JSON, etc
 
     @Override
     public JsonSchemaTree process(final JsonSchemaContext context)
         throws ProcessingException
     {
         /*
          * Start by grabbing a new message
          */
         final ProcessingMessage msg = context.newMessage();
 
         /*
          * The set of refs we see during ref resolution, necessary to detect ref
          * loops. We make it linked since we want the ref path reported in the
          * order where refs have been encountered.
          */
         final Set<JsonRefrefs = Sets.newLinkedHashSet();
 
         /*
          * Make a copy of the original tree which will be our initial return
          * value if no ref is found.
          */
         JsonSchemaTree tree = context.getSchemaTree().copy();
 
         JsonPointer ptr;
         JsonRef ref;
         JsonNode node;
 
         while(true) {
             /*
              * See if the current node is a JSON Reference.
              */
             node = tree.getCurrentNode();
             /*
              * If it isn't, we are done
              */
             ref = nodeAsRef(node);
             if (ref == null)
                 break;
             /*
              * Resolve the reference against the current tree.
              */
             ref = tree.resolve(ref);
             /*
              * If we have seen this ref already, this is a ref loop.
              */
             if (!refs.add(ref)) {
                 msg.msg("JSON Reference loop detected").put("ref"ref)
                     .put("path"refs);
                 throw new ProcessingException(msg);
            }
            /*
             * Check whether ref is resolvable within the current tree. If not,
             * fetch the new tree.
             *
             * This may fail, in which case we exit here since SchemaLoader's
             * .get() throws a ProcessingException if it fails.
             */
            if (!tree.containsRef(ref))
                tree = .get(ref.getLocator());
            /*
             * Get the appropriate pointer into the tree. If none, this means
             * a dangling reference.
             */
            ptr = tree.matchingPointer(ref);
            if (ptr == null) {
                msg.msg("unresolvable JSON Reference").put("ref"ref);
                throw new ProcessingException(msg);
            }
            tree.setPointer(ptr);
        }
        return tree;
    }
    private static JsonRef nodeAsRef(final JsonNode node)
    {
        final JsonNode refNode = node.path("$ref");
        if (!refNode.isTextual())
            return null;
        try {
            return JsonRef.fromString(refNode.textValue());
        } catch (JsonSchemaException ignored) {
            return null;
        }
    }
New to GrepCode? Check out our FAQ X