All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.helger.ubl.api.codegen.AbstractUBLCodeGen Maven / Gradle / Ivy

/*
 * Copyright (C) 2014-2024 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * 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.helger.ubl.api.codegen;

import java.io.File;
import java.net.URL;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.commons.collection.ArrayHelper;
import com.helger.commons.collection.CollectionHelper;
import com.helger.commons.io.file.FileSystemIterator;
import com.helger.commons.io.file.IFileFilter;
import com.helger.commons.regex.RegExHelper;
import com.helger.commons.string.StringHelper;
import com.helger.commons.url.URLHelper;
import com.helger.xml.CXML;
import com.helger.xml.microdom.IMicroDocument;

/**
 * Base class for internal code generation.
 *
 * @author Philip Helger
 */
public abstract class AbstractUBLCodeGen
{
  @Nonnull
  protected static String lcFirst (@Nonnull final String s)
  {
    return s.substring (0, 1).toLowerCase (Locale.ROOT) + s.substring (1);
  }

  @Nonnull
  protected static String toUpperCase (@Nonnull final String sCC)
  {
    final StringBuilder ret = new StringBuilder (sCC.length () * 2);
    for (int i = 0; i < sCC.length (); ++i)
    {
      final char cSrc = sCC.charAt (i);
      final char cUp = Character.toUpperCase (cSrc);
      if (cSrc == cUp && i > 0)
        ret.append ('_');
      ret.append (cUp);
    }
    return ret.toString ();
  }

  @Nonnull
  protected static String getDisplayNameFromType (@Nonnull final String s)
  {
    final StringBuilder ret = new StringBuilder ();
    for (final char c : s.toCharArray ())
    {
      if (Character.isUpperCase (c) && ret.length () > 0)
        ret.append (' ');
      ret.append (c);
    }
    return ret.toString ();
  }

  @Nonnull
  protected static Iterable  getXSDFileList (final String sPath)
  {
    return CollectionHelper.getSorted (new FileSystemIterator (sPath).withFilter (IFileFilter.filenameEndsWith (".xsd"))
                                                                     .withFilter (IFileFilter.filenameMatchNoRegEx (".*CCTS.*",
                                                                                                                    ".*xmldsig.*",
                                                                                                                    ".*XAdES.*")),
                                       Comparator.comparing (File::getName));
  }

  @Nullable
  protected static String getTargetNamespace (@Nonnull final IMicroDocument aDoc)
  {
    return aDoc.getDocumentElement ().getAttributeValue (CXML.XML_ATTR_XSD_TARGETNAMESPACE);
  }

  @Nonnull
  protected static String getAsPackageName (@Nonnull final String sNamespaceURI)
  {
    // Lowercase everything
    String s = sNamespaceURI.toLowerCase (Locale.ROOT);

    String [] aParts;
    final URL aURL = URLHelper.getAsURL (sNamespaceURI, false);
    if (aURL != null)
    {
      // Host
      String sHost = aURL.getHost ();

      // Kick static prefix: www.helger.com -> helger.com
      sHost = StringHelper.trimStart (sHost, "www.");

      // Reverse domain: helger.com -> com.helger
      final List  x = CollectionHelper.getReverseList (StringHelper.getExploded ('.', sHost));

      // Path in regular order:
      final String sPath = StringHelper.trimStart (aURL.getPath (), '/');
      x.addAll (StringHelper.getExploded ('/', sPath));

      // Convert to array
      aParts = ArrayHelper.newArray (x, String.class);
    }
    else
    {
      // Kick known prefixes
      for (final String sPrefix : new String [] { "urn:", "http://" })
        if (s.startsWith (sPrefix))
        {
          s = s.substring (sPrefix.length ());
          break;
        }

      // Replace all illegal characters
      s = StringHelper.replaceAll (s, ':', '.');
      s = StringHelper.replaceAll (s, '-', '_');
      aParts = StringHelper.getExplodedArray ('.', s);
    }

    // Split into pieces and replace all illegal package parts (e.g. only
    // numeric) with valid ones
    for (int i = 0; i < aParts.length; ++i)
      aParts[i] = RegExHelper.getAsIdentifier (aParts[i]);

    return StringHelper.imploder ().source (aParts).separator ('.').build ();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy