001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau;
014
015/**
016 * Converts property names to dashed-lower-case format.
017 *
018 * <h5 class='section'>Example:</h5>
019 * <ul>
020 *    <li><js>"fooBar"</js> -&gt; <js>"foo-bar"</js>
021 *    <li><js>"fooBarURL"</js> -&gt; <js>"foo-bar-url"</js>
022 *    <li><js>"FooBarURL"</js> -&gt; <js>"foo-bar-url"</js>
023 * </ul>
024 */
025public final class PropertyNamerDLC implements PropertyNamer {
026
027   @Override /* PropertyNamer */
028   public String getPropertyName(String name) {
029      if (name == null || name.isEmpty())
030         return name;
031
032      int numUCs = 0;
033      boolean isPrevUC = Character.isUpperCase(name.charAt(0));
034      for (int i = 1; i < name.length(); i++) {
035         char c = name.charAt(i);
036         if (Character.isUpperCase(c)) {
037            if (! isPrevUC)
038               numUCs++;
039            isPrevUC = true;
040         } else {
041            isPrevUC = false;
042         }
043      }
044
045      char[] name2 = new char[name.length() + numUCs];
046      isPrevUC = Character.isUpperCase(name.charAt(0));
047      name2[0] = Character.toLowerCase(name.charAt(0));
048      int ni = 0;
049      for (int i = 0; i < name.length(); i++) {
050         char c = name.charAt(i);
051         if (Character.isUpperCase(c)) {
052            if (! isPrevUC)
053               name2[ni++] = '-';
054            isPrevUC = true;
055            name2[ni++] = Character.toLowerCase(c);
056         } else {
057            isPrevUC = false;
058            name2[ni++] = c;
059         }
060      }
061
062      return new String(name2);
063   }
064}