code: public void addResourceToCategory(CmsObject cms, String resourceName, CmsCategory category) throws CmsException {
  
          if (readResourceCategories(cms, cms.readResource(resourceName, CmsResourceFilter.IGNORE_EXPIRATION)).contains(
              category)) {
              return;
          }
          String sitePath = cms.getRequestContext().removeSiteRoot(category.getRootPath());
          cms.addRelationToResource(resourceName, sitePath, CmsRelationType.CATEGORY.getName());
  
          String parentCatPath = category.getPath();
          // recursively add to higher level categories
          if (parentCatPath.endsWith("/")) {
              parentCatPath = parentCatPath.substring(0, parentCatPath.length() - 1);
          }
          if (parentCatPath.lastIndexOf('/') > 0) {
              addResourceToCategory(cms, resourceName, parentCatPath.substring(0, parentCatPath.lastIndexOf('/') + 1));
          }
      }
docs: Adds a resource identified by the given resource name to the given category.<p>
  
  The resource has to be locked.<p>
  
  @param cms the current cms context
  @param resourceName the site relative path to the resource to add
  @param category the category to add the resource to
  
  @throws CmsException if something goes wrong