private String cleanKey(String s) {
    s = s.replaceAll("#", "");
    s = s.replaceAll("\\.", "_");
    s = s.replaceAll("-", "_");

    if (reservedWords.contains(s)) {
      s = "_" + s;
    }

    return s;
  }
  public VocabBuilder(String filename, RDFFormat format) throws IOException, RDFParseException {
    Path file = Paths.get(filename);
    if (!Files.exists(file)) throw new FileNotFoundException(filename);

    if (format == null) {
      format = Rio.getParserFormatForFileName(filename);
      log.trace("detected input format from filename {}: {}", filename, format);
    }

    try (final InputStream inputStream = Files.newInputStream(file)) {
      log.trace("Loading input file");
      model = Rio.parse(inputStream, "", format);
    }

    // import
    Set<Resource> owlOntologies = model.filter(null, RDF.TYPE, OWL.ONTOLOGY).subjects();
    if (!owlOntologies.isEmpty()) {
      setPrefix(owlOntologies.iterator().next().stringValue());
    }
  }
 private void checkField(String className, String fieldName) throws GenerationException {
   if (!createdFields.add(fieldName)) {
     throw new GenerationException(
         String.format("field %s.%s is defined twice", className, fieldName));
   }
 }
  public void generate(String className, PrintWriter out)
      throws IOException, GraphUtilException, GenerationException {
    // be sure to have at least the uri constants generated
    if (stringGeneration == null && uriGeneration == null) {
      uriGeneration = GenerationSetting.createDefault(caseFormat, "", "");
      // throw new GenerationException("no generation settings present, please set explicitly");
    }
    log.trace("classname: {}", className);
    if (StringUtils.isBlank(name)) {
      name = className;
    }
    if (StringUtils.isBlank(prefix)) {
      throw new GenerationException("could not detect prefix, please set explicitly");
    } else {
      log.debug("prefix: {}", prefix);
    }

    Pattern pattern = Pattern.compile(Pattern.quote(getPrefix()) + "(.+)");
    ConcurrentMap<String, URI> splitUris = new ConcurrentHashMap<>();
    for (Resource nextSubject : model.subjects()) {
      if (nextSubject instanceof URI) {
        Matcher matcher = pattern.matcher(nextSubject.stringValue());
        if (matcher.find()) {
          String k = matcher.group(1);
          URI putIfAbsent = splitUris.putIfAbsent(k, (URI) nextSubject);
          if (putIfAbsent != null) {
            log.warn(
                "Conflicting keys found: uri={} key={} existing={}",
                nextSubject.stringValue(),
                k,
                putIfAbsent);
          }
        }
      }
    }

    // print

    // package is optional
    if (StringUtils.isNotBlank(packageName)) {
      out.printf("package %s;%n%n", getPackageName());
    }
    // imports
    out.println("import org.openrdf.model.URI;");
    out.println("import org.openrdf.model.ValueFactory;");
    out.println("import org.openrdf.model.impl.ValueFactoryImpl;");
    out.println();

    final URI pfx = new URIImpl(prefix);
    Literal oTitle =
        getFirstExistingObjectLiteral(model, pfx, getPreferredLanguage(), LABEL_PROPERTIES);
    Literal oDescr =
        getFirstExistingObjectLiteral(model, pfx, getPreferredLanguage(), COMMENT_PROPERTIES);
    Set<Value> oSeeAlso = model.filter(pfx, RDFS.SEEALSO, null).objects();

    // class JavaDoc
    out.println("/**");
    if (oTitle != null) {
      out.printf(
          " * %s.%n",
          WordUtils.wrap(oTitle.getLabel().replaceAll("\\s+", " "), 70, "\n * ", false));
      out.println(" * <p>");
    }
    if (oDescr != null) {
      out.printf(
          " * %s.%n",
          WordUtils.wrap(oDescr.getLabel().replaceAll("\\s+", " "), 70, "\n * ", false));
      out.println(" * <p>");
    }
    out.printf(" * Namespace %s.%n", name);
    out.printf(" * Prefix: {@code <%s>}%n", prefix);
    if (!oSeeAlso.isEmpty()) {
      out.println(" *");
      for (Value s : oSeeAlso) {
        if (s instanceof URI) {
          out.printf(" * @see <a href=\"%s\">%s</a>%n", s.stringValue(), s.stringValue());
        }
      }
    }
    out.println(" */");
    // class Definition
    out.printf("public class %s {%n", className);
    out.println();

    // constants
    out.printf(getIndent(1) + "/** {@code %s} **/%n", prefix);
    out.printf(getIndent(1) + "public static final String NAMESPACE = \"%s\";%n", prefix);
    out.println();
    out.printf(getIndent(1) + "/** {@code %s} **/%n", name.toLowerCase());
    out.printf(getIndent(1) + "public static final String PREFIX = \"%s\";%n", name.toLowerCase());
    out.println();

    List<String> keys = new ArrayList<>();
    keys.addAll(splitUris.keySet());
    Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);
    // do the string constant generation (if set)
    if (stringGeneration != null) {
      for (String key : keys) {
        final Literal comment =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), COMMENT_PROPERTIES);
        final Literal label =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), LABEL_PROPERTIES);

        out.println(getIndent(1) + "/**");
        if (label != null) {
          out.printf(getIndent(1) + " * %s%n", label.getLabel());
          out.println(getIndent(1) + " * <p>");
        }
        out.printf(getIndent(1) + " * {@code %s}.%n", splitUris.get(key).stringValue());
        if (comment != null) {
          out.println(getIndent(1) + " * <p>");
          out.printf(
              getIndent(1) + " * %s%n",
              WordUtils.wrap(
                  comment.getLabel().replaceAll("\\s+", " "),
                  70,
                  "\n" + getIndent(1) + " * ",
                  false));
        }
        out.println(getIndent(1) + " *");
        out.printf(getIndent(1) + " * @see <a href=\"%s\">%s</a>%n", splitUris.get(key), key);
        out.println(getIndent(1) + " */");

        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(stringGeneration.getConstantPrefix()),
                    doCaseFormatting(key, stringGeneration.getCaseFormat()),
                    StringUtils.defaultString(stringGeneration.getConstantSuffix())));
        checkField(className, nextKey);
        out.printf(
            getIndent(1) + "public static final String %s = %s.NAMESPACE + \"%s\";%n",
            nextKey,
            className,
            key);
        out.println();
      }
    }
    // do the entire uri constant generation
    if (uriGeneration != null) {
      for (String key : keys) {
        Literal comment =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), COMMENT_PROPERTIES);
        Literal label =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), LABEL_PROPERTIES);

        out.println(getIndent(1) + "/**");
        if (label != null) {
          out.printf(getIndent(1) + " * %s%n", label.getLabel());
          out.println(getIndent(1) + " * <p>");
        }
        out.printf(getIndent(1) + " * {@code %s}.%n", splitUris.get(key).stringValue());
        if (comment != null) {
          out.println(getIndent(1) + " * <p>");
          out.printf(
              getIndent(1) + " * %s%n",
              WordUtils.wrap(
                  comment.getLabel().replaceAll("\\s+", " "),
                  70,
                  "\n" + getIndent(1) + " * ",
                  false));
        }
        out.println(getIndent(1) + " *");
        out.printf(getIndent(1) + " * @see <a href=\"%s\">%s</a>%n", splitUris.get(key), key);
        out.println(getIndent(1) + " */");
        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(uriGeneration.getConstantPrefix()),
                    doCaseFormatting(key, uriGeneration.getCaseFormat()),
                    StringUtils.defaultString(uriGeneration.getConstantSuffix())));

        // String nextKey = cleanKey(doCaseFormatting(key, uriGeneration.getCaseFormat()));
        checkField(className, nextKey);
        out.printf(getIndent(1) + "public static final URI %s;%n", nextKey);
        out.println();
      }
      // static init
      out.println(getIndent(1) + "static {");
      out.printf(getIndent(2) + "ValueFactory factory = ValueFactoryImpl.getInstance();%n");
      out.println();
      for (String key : keys) {
        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(uriGeneration.getConstantPrefix()),
                    doCaseFormatting(key, uriGeneration.getCaseFormat()),
                    StringUtils.defaultString(uriGeneration.getConstantSuffix())));
        out.printf(
            getIndent(2) + "%s = factory.createURI(%s.NAMESPACE, \"%s\");%n",
            nextKey,
            className,
            key);
      }
      out.println(getIndent(1) + "}");
      out.println();
    }

    // private contructor to avoid instances
    out.printf(getIndent(1) + "private %s() {%n", className);
    out.println(getIndent(2) + "//static access only");
    out.println(getIndent(1) + "}");
    out.println();

    // class end
    out.println("}");
    out.flush();
  }