private static void toXml(XmlWriter w, Resource resource) throws IOException {
    w.element(RepositoryParser.RESOURCE)
        .attribute(Resource.ID, resource.getId())
        .attribute(Resource.SYMBOLIC_NAME, resource.getSymbolicName())
        .attribute(Resource.PRESENTATION_NAME, resource.getPresentationName())
        .attribute(Resource.URI, getRelativeUri(resource, Resource.URI))
        .attribute(Resource.VERSION, resource.getVersion().toString());

    w.textElement(Resource.DESCRIPTION, resource.getProperties().get(Resource.DESCRIPTION))
        .textElement(Resource.SIZE, resource.getProperties().get(Resource.SIZE))
        .textElement(
            Resource.DOCUMENTATION_URI, getRelativeUri(resource, Resource.DOCUMENTATION_URI))
        .textElement(Resource.SOURCE_URI, getRelativeUri(resource, Resource.SOURCE_URI))
        .textElement(Resource.JAVADOC_URI, getRelativeUri(resource, Resource.JAVADOC_URI))
        .textElement(Resource.LICENSE_URI, getRelativeUri(resource, Resource.LICENSE_URI));

    String[] categories = resource.getCategories();
    for (int i = 0; categories != null && i < categories.length; i++) {
      w.element(RepositoryParser.CATEGORY).attribute(RepositoryParser.ID, categories[i]).end();
    }
    Capability[] capabilities = resource.getCapabilities();
    for (int i = 0; capabilities != null && i < capabilities.length; i++) {
      toXml(w, capabilities[i]);
    }
    Requirement[] requirements = resource.getRequirements();
    for (int i = 0; requirements != null && i < requirements.length; i++) {
      toXml(w, requirements[i]);
    }
    w.end();
  }
 private static String getRelativeUri(Resource resource, String name) {
   String uri = (String) resource.getProperties().get(name);
   if (resource instanceof ResourceImpl) {
     try {
       uri =
           URI.create(((ResourceImpl) resource).getRepository().getURI())
               .relativize(URI.create(uri))
               .toASCIIString();
     } catch (Throwable t) {
     }
   }
   return uri;
 }