PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/netbeans-7.3/php.project/src/org/netbeans/modules/php/project/annotations/UserAnnotations.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 167 lines | 97 code | 21 blank | 49 comment | 9 complexity | 69e50bdb44e69e4c06cfbcbefc19d67a MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2012 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.php.project.annotations;
  43. import java.util.ArrayList;
  44. import java.util.EnumSet;
  45. import java.util.LinkedList;
  46. import java.util.List;
  47. import java.util.prefs.Preferences;
  48. import org.netbeans.modules.php.api.util.StringUtils;
  49. import org.openide.util.NbPreferences;
  50. public final class UserAnnotations {
  51. // Do not change arbitrary - consult with layer's folder OptionsExport
  52. // Path to Preferences node for storing these preferences
  53. private static final String PREFERENCES_PATH = "annotations"; // NOI18N
  54. private static final UserAnnotations INSTANCE = new UserAnnotations();
  55. // common tag key - tag.<index>.<attribute>
  56. private static final String TAG_KEY = "tag.%d.%s"; // NOI18N
  57. // tag attributes
  58. private static final String ATTR_TYPES = "types"; // NOI18N
  59. private static final String ATTR_NAME = "name"; // NOI18N
  60. private static final String ATTR_INSERT_TEMPLATE = "insertTemplate"; // NOI18N
  61. private static final String ATTR_DOCUMENTATION = "documentation"; // NOI18N
  62. // value delimiter
  63. private static final String DELIMITER = ","; // NOI18N
  64. public static UserAnnotations getInstance() {
  65. return INSTANCE;
  66. }
  67. public List<UserAnnotationTag> getAnnotations() {
  68. List<UserAnnotationTag> annotations = new LinkedList<UserAnnotationTag>();
  69. Preferences preferences = getPreferences();
  70. int i = 0;
  71. for (;;) {
  72. String types = preferences.get(getTypesKey(i), null);
  73. if (types == null) {
  74. return annotations;
  75. }
  76. String name = preferences.get(getNameKey(i), null);
  77. String insertTemplate = preferences.get(getInsertTemplateKey(i), null);
  78. String documentation = preferences.get(getDocumentationKey(i), null);
  79. annotations.add(new UserAnnotationTag(unmarshallTypes(types), name, insertTemplate, documentation));
  80. i++;
  81. }
  82. }
  83. public void setAnnotations(List<UserAnnotationTag> annotations) {
  84. clearAnnotations();
  85. Preferences preferences = getPreferences();
  86. int i = 0;
  87. for (UserAnnotationTag annotation : annotations) {
  88. preferences.put(getTypesKey(i), marshallTypes(annotation.getTypes()));
  89. preferences.put(getNameKey(i), annotation.getName());
  90. preferences.put(getInsertTemplateKey(i), annotation.getInsertTemplate());
  91. preferences.put(getDocumentationKey(i), annotation.getDocumentation());
  92. i++;
  93. }
  94. }
  95. // for unit tests
  96. void clearAnnotations() {
  97. Preferences preferences = getPreferences();
  98. int i = 0;
  99. for (;;) {
  100. String type = preferences.get(getTypesKey(i), null);
  101. if (type == null) {
  102. return;
  103. }
  104. preferences.remove(getTypesKey(i));
  105. preferences.remove(getNameKey(i));
  106. preferences.remove(getInsertTemplateKey(i));
  107. preferences.remove(getDocumentationKey(i));
  108. i++;
  109. }
  110. }
  111. private String getTypesKey(int i) {
  112. return getKey(i, ATTR_TYPES);
  113. }
  114. private String getNameKey(int i) {
  115. return getKey(i, ATTR_NAME);
  116. }
  117. private String getInsertTemplateKey(int i) {
  118. return getKey(i, ATTR_INSERT_TEMPLATE);
  119. }
  120. private String getDocumentationKey(int i) {
  121. return getKey(i, ATTR_DOCUMENTATION);
  122. }
  123. private String getKey(int i, String attr) {
  124. return String.format(TAG_KEY, i, attr);
  125. }
  126. // for unit tests
  127. String marshallTypes(EnumSet<UserAnnotationTag.Type> types) {
  128. ArrayList<String> list = new ArrayList<String>(types.size());
  129. for (UserAnnotationTag.Type type : types) {
  130. list.add(type.name());
  131. }
  132. return StringUtils.implode(list, DELIMITER);
  133. }
  134. // for unit tests
  135. EnumSet<UserAnnotationTag.Type> unmarshallTypes(String types) {
  136. List<String> list = StringUtils.explode(types, DELIMITER);
  137. EnumSet<UserAnnotationTag.Type> result = EnumSet.noneOf(UserAnnotationTag.Type.class);
  138. for (String type : list) {
  139. result.add(UserAnnotationTag.Type.valueOf(type));
  140. }
  141. return result;
  142. }
  143. private Preferences getPreferences() {
  144. return NbPreferences.forModule(UserAnnotations.class).node(PREFERENCES_PATH);
  145. }
  146. }