Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
     Copyright (c) 2012 LinkedIn Corp.
  
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
         http://www.apache.org/licenses/LICENSE-2.0
  
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */
 
 package com.linkedin.restli.docgen;
 
 
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
A collection of ResourceSchema, supporting visitor-style iteration. Each ResourceSchema (and sub-resource) is identified by a dot-delimited path e.g. "groups" or "groups.contacts"

Author(s):
dellamag
 
 {
  
For each given com.linkedin.restli.internal.server.model.ResourceModel, the classpath is checked for a .restspec.json matching the name of the com.linkedin.restli.internal.server.model.ResourceModel, if found it is loaded. If a .restspec.json file is not found, one is created ResourceSchemaCollection from specified root com.linkedin.restli.internal.server.model.ResourceModel. All resources will be recursively traversed to discover subresources. Root resources not specified are excluded.

Parameters:
rootResources root resources in ResourceModel type
Returns:
constructed ResourceSchemaCollection
 
   {
     final ResourceModelEncoder encoder = new ResourceModelEncoder(new NullDocsProvider());
     final Map<StringResourceSchemaschemaMap = new TreeMap<StringResourceSchema>();
     for (ResourceModel resource : rootResources.values())
     {
       schemaMap.put(resource.getName(), encoder.loadOrBuildResourceSchema(resource));
     }
 
     return new ResourceSchemaCollection(schemaMap);
   }

  
Create ResourceSchemaCollection from idl files.

Parameters:
restspecSearchPaths file system paths to search for idl files
Returns:
constructed ResourceSchemaCollection
 
   public static ResourceSchemaCollection createFromIdls(String[] restspecSearchPaths)
   {
     final RestSpecCodec codec = new RestSpecCodec();
     final Map<StringResourceSchemaresourceSchemaMap = new HashMap<StringResourceSchema>();
 
     for (String path : restspecSearchPaths)
     {
       final File dir = new File(path);
       if (! dir.isDirectory())
       {
        throw new IllegalArgumentException(String.format("path '%s' is not a directory"dir.getAbsolutePath()));
      }
      final File[] idlFiles = dir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname)
        {
          return pathname.getName().endsWith(.);
        }
      });
      for (File idlFile : idlFiles)
      {
        try
        {
          final FileInputStream is = new FileInputStream(idlFile);
          final ResourceSchema resourceSchema = codec.readResourceSchema(is);
          resourceSchemaMap.put(resourceSchema.getName(), resourceSchema);
        }
        catch (IOException e)
        {
          throw new RestLiInternalException(String.format("Error loading restspec IDL file '%s'"idlFile.getName()), e);
        }
      }
    }
    return new ResourceSchemaCollection(resourceSchemaMap);
  }

  

Parameters:
visitor ResourceSchemaVisitior to visit all resource schemas with the specified visitor
  public static void visitResources(Collection<ResourceSchemaresourcesResourceSchemaVisitior visitor)
  {
    for (ResourceSchema schema : resources)
    {
      processResourceSchema(visitornew ArrayList<ResourceSchema>(), schema);
    }
  }

  
Store the specified root resources plus the discover subresources

Parameters:
rootResources root resources in com.linkedin.restli.restspec.ResourceSchema type
  public ResourceSchemaCollection(Map<StringResourceSchemarootResources)
  {
     = new TreeMap<StringResourceSchema>(rootResources);
    final Map<StringResourceSchemaflattenSubResources = new TreeMap<StringResourceSchema>();
    final ResourceSchemaVisitior visitor = new BaseResourceSchemaVisitor()
    {
      @Override
      public void visitResourceSchema(VisitContext context,
                                      ResourceSchema resourceSchema)
      {
        final String qualifiedResourceName = context.getResourcePath();
        if (!.containsKey(qualifiedResourceName))
        {
          flattenSubResources.put(qualifiedResourceNameresourceSchema);
          final List<ResourceSchemahierarchy = context.getResourceSchemaHierarchy();
          final ResourceSchema parent = hierarchy.get(hierarchy.size() - 2);
          List<ResourceSchemasubList = .get(parent);
          if (subList == null)
          {
            subList = new ArrayList<ResourceSchema>();
            .put(parentsubList);
          }
          subList.add(resourceSchema);
        }
      }
    };
    visitResources(.values(), visitor);
    .putAll(flattenSubResources);
  }

  
Retrieve the resource schema for the specified path.

Parameters:
resourcePath for root resources, the path is the name of the resource; for subresource, the path is the fully-qualitied resource name, delimited with "."
Returns:
schema of the resource
  public ResourceSchema getResource(String resourcePath)
  {
    return .get(resourcePath);
  }

  

Returns:
map from the resource path to both root resources and all discovered subresources
  {
    return ;
  }

  

Parameters:
parentSchema a parent resource schema
Returns:
schema of direct subresources of the specified resource
  {
    return .get(parentSchema);
  }

  

Parameters:
ancestorSchema a root resource schema
Returns:
schema of all nested subresources that are the descendants of the specified resource
  {
    return getAllSubResourcesRecursive(ancestorSchemanew ArrayList<ResourceSchema>());
  }
                                                           List<ResourceSchemaaccumulator)
  {
    final List<ResourceSchemasubResources = getSubResources(parentSchema);
    if (subResources == null)
    {
      return null;
    }
    accumulator.addAll(subResources);
    for (ResourceSchema sub : subResources)
    {
      getAllSubResourcesRecursive(subaccumulator);
    }
    return accumulator;
  }
  private static void processResourceSchema(ResourceSchemaVisitior visitor,
                                            List<ResourceSchemahierarchy,
                                            ResourceSchema resourceSchema)
  {
    hierarchy.add(resourceSchema);
    final ResourceSchemaVisitior.VisitContext context = buildContext(hierarchy);
    visitor.visitResourceSchema(contextresourceSchema);
    if (resourceSchema.hasCollection())
    {
      final CollectionSchema collectionSchema = resourceSchema.getCollection();
      visitor.visitCollectionResource(contextcollectionSchema);
      processRestMethods(visitorcontextcollectionSchemacollectionSchema.getMethods());
      processFinders(visitorcontextcollectionSchemacollectionSchema.getFinders());
      processActions(visitorcontextcollectionSchemacollectionSchema.getActions());
      processEntitySchema(visitorcontextcollectionSchema.getEntity());
    }
    else if (resourceSchema.hasAssociation())
    {
      final AssociationSchema associationSchema = resourceSchema.getAssociation();
      visitor.visitAssociationResource(contextassociationSchema);
      processRestMethods(visitorcontextassociationSchemaassociationSchema.getMethods());
      processFinders(visitorcontextassociationSchemaassociationSchema.getFinders());
      processActions(visitorcontextassociationSchemaassociationSchema.getActions());
      processEntitySchema(visitorcontextassociationSchema.getEntity());
    }
    else if (resourceSchema.hasSimple())
    {
      final SimpleSchema simpleSchema = resourceSchema.getSimple();
      visitor.visitSimpleResource(contextsimpleSchema);
      processRestMethods(visitorcontextsimpleSchemasimpleSchema.getMethods());
      processActions(visitorcontextsimpleSchemasimpleSchema.getActions());
      processEntitySchema(visitorcontextsimpleSchema.getEntity());
    }
    else if (resourceSchema.hasActionsSet())
    {
      final ActionsSetSchema actionsSet = resourceSchema.getActionsSet();
      visitor.visitActionSetResource(contextactionsSet);
      processActions(visitorcontextactionsSetactionsSet.getActions());
    }
    hierarchy.remove(hierarchy.size() - 1);
  }
  private static void processEntitySchema(ResourceSchemaVisitior visitor,
                                          ResourceSchemaVisitior.VisitContext context,
                                          EntitySchema entitySchema)
  {
    visitor.visitEntityResource(contextentitySchema);
    processActions(visitorcontextentitySchemaentitySchema.getActions());
    if (entitySchema.hasSubresources())
    {
      for (ResourceSchema resourceSchema : entitySchema.getSubresources())
      {
        processResourceSchema(visitorcontext.getResourceSchemaHierarchy(), resourceSchema);
      }
    }
  }
  private static void processRestMethods(ResourceSchemaVisitior visitor,
                                         ResourceSchemaVisitior.VisitContext context,
                                         RecordTemplate containingResourceType,
                                         RestMethodSchemaArray methods)
  {
    if (methods != null)
    {
      for (RestMethodSchema restMethodSchema : methods)
      {
        visitor.visitRestMethod(contextcontainingResourceTyperestMethodSchema);
        if (restMethodSchema.hasParameters())
        {
          for (ParameterSchema parameterSchema : restMethodSchema.getParameters())
          {
            visitor.visitParameter(context,
                                   containingResourceType,
                                   restMethodSchema,
                                   parameterSchema);
          }
        }
      }
    }
  }
  private static void processFinders(ResourceSchemaVisitior visitor,
                                     ResourceSchemaVisitior.VisitContext context,
                                     RecordTemplate containingResourceType,
                                     FinderSchemaArray finders)
  {
    if (finders != null)
    {
      for (FinderSchema finderSchema : finders)
      {
        visitor.visitFinder(contextcontainingResourceTypefinderSchema);
        if (finderSchema.hasParameters())
        {
          for (ParameterSchema parameterSchema : finderSchema.getParameters())
          {
            visitor.visitParameter(context,
                                   containingResourceType,
                                   finderSchema,
                                   parameterSchema);
          }
        }
      }
    }
  }
  private static void processActions(ResourceSchemaVisitior visitor,
                                     ResourceSchemaVisitior.VisitContext context,
                                     RecordTemplate containingResourceType,
                                     ActionSchemaArray actions)
  {
    if (actions != null)
    {
      final ResourceLevel resourceLevel = ((EntitySchema.class.equals(containingResourceType.getClass()) ||
                                           SimpleSchema.class.equals(containingResourceType.getClass())) ?
                                           . :
                                           .);
      for (ActionSchema actionSchema : actions)
      {
        visitor.visitAction(contextcontainingResourceTyperesourceLevelactionSchema);
        if (actionSchema.hasParameters())
        {
          for (ParameterSchema parameterSchema : actionSchema.getParameters())
          {
            visitor.visitParameter(context,
                                   containingResourceType,
                                   actionSchema,
                                   parameterSchema);
          }
        }
      }
    }
  }
  {
    final StringBuilder resourcePath = new StringBuilder();
    for (ResourceSchema resourceSchema : hierarchy)
    {
      resourcePath.append(resourceSchema.getName()).append(".");
    }
    resourcePath.deleteCharAt(resourcePath.length() - 1);
    return new ResourceSchemaVisitior.VisitContext(hierarchyresourcePath.toString());
  }
  private final Map<StringResourceSchema_allResources;
New to GrepCode? Check out our FAQ X