View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.apt.processor;
21  
22  import org.apache.commons.io.IOUtils;
23  import org.apache.myfaces.tobago.apt.annotation.Converter;
24  import org.apache.myfaces.tobago.apt.annotation.Facet;
25  import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
26  import org.apache.myfaces.tobago.apt.annotation.UIComponentTag;
27  import org.apache.myfaces.tobago.apt.annotation.UIComponentTagAttribute;
28  import org.apache.myfaces.tobago.apt.annotation.Validator;
29  import org.apache.myfaces.tobago.apt.generate.ComponentInfo;
30  import org.codehaus.plexus.util.FileUtils;
31  import org.jdom.Attribute;
32  import org.jdom.Comment;
33  import org.jdom.Document;
34  import org.jdom.Namespace;
35  import org.jdom.filter.ContentFilter;
36  import org.jdom.input.SAXBuilder;
37  import org.jdom.output.Format;
38  import org.jdom.output.XMLOutputter;
39  
40  import javax.annotation.processing.SupportedAnnotationTypes;
41  import javax.annotation.processing.SupportedOptions;
42  import javax.lang.model.element.ExecutableElement;
43  import javax.lang.model.element.TypeElement;
44  import javax.tools.FileObject;
45  import javax.tools.StandardLocation;
46  import java.io.IOException;
47  import java.io.StringReader;
48  import java.io.StringWriter;
49  import java.io.Writer;
50  import java.util.ArrayList;
51  import java.util.Arrays;
52  import java.util.Collections;
53  import java.util.Comparator;
54  import java.util.HashSet;
55  import java.util.Iterator;
56  import java.util.List;
57  import java.util.Locale;
58  import java.util.Map;
59  import java.util.Set;
60  
61  @SupportedAnnotationTypes({
62      "org.apache.myfaces.tobago.apt.annotation.Tag",
63      "org.apache.myfaces.tobago.apt.annotation.TagAttribute",
64      "org.apache.myfaces.tobago.apt.annotation.Taglib",
65      "org.apache.myfaces.tobago.apt.annotation.UIComponentTagAttribute",
66      "org.apache.myfaces.tobago.apt.annotation.UIComponentTag",
67      "org.apache.myfaces.tobago.apt.annotation.Facet",
68      "org.apache.myfaces.tobago.apt.annotation.Preliminary",
69      "org.apache.myfaces.tobago.apt.annotation.Converter",
70      "org.apache.myfaces.tobago.apt.annotation.Validator"})
71  @SupportedOptions({
72      FacesConfigGenerator.SOURCE_FACES_CONFIG,
73      FacesConfigGenerator.TARGET_FACES_CONFIG,
74      FacesConfigGenerator.JSF_VERSION})
75  public class FacesConfigGenerator extends AbstractGenerator {
76  
77    static final String SOURCE_FACES_CONFIG = "sourceFacesConfig";
78    static final String TARGET_FACES_CONFIG = "targetFacesConfig";
79  
80    private static final String SEPARATOR = System.getProperty("line.separator");
81    private static final String COMPONENT = "component";
82    private static final String COMPONENT_TYPE = "component-type";
83    private static final String COMPONENT_CLASS = "component-class";
84    private static final String COMPONENT_EXTENSION = "component-extension";
85    private static final String ALLOWED_CHILD_COMPONENTS = "allowed-child-components";
86    private static final String CATEGORY = "category";
87    private static final String DEPRECATED = "deprecated";
88    private static final String HIDDEN = "hidden";
89    private static final String FACET = "facet";
90    private static final String DISPLAY_NAME = "display-name";
91    private static final String DESCRIPTION = "description";
92    private static final String FACET_NAME = "facet-name";
93    private static final String FACET_EXTENSION = "facet-extension";
94    private static final String PROPERTY = "property";
95    private static final String PROPERTY_NAME = "property-name";
96    private static final String PROPERTY_CLASS = "property-class";
97    private static final String PROPERTY_EXTENSION = "property-extension";
98    private static final String VALUE_EXPRESSION = "value-expression"; //UIComponentTagAttribute.valueExpression()
99    private static final String PROPERTY_VALUES = "property-values"; //UIComponentTagAttribute.allowedValues()
100   private static final String READONLY = "read-only";
101   private static final String REQUIRED = "required"; //UITagAttribute.required()
102   private static final String DEFAULT_VALUE = "default-value";
103   private static final String ATTRIBUTE = "attribute";
104   private static final String ATTRIBUTE_NAME = "attribute-name";
105   private static final String ATTRIBUTE_CLASS = "attribute-class";
106   private static final String ATTRIBUTE_EXTENSION = "attribute-extension";
107   private static final String APPLICATION = "application";
108   private static final String FACTORY = "factory";
109   private static final String CONVERTER = "converter";
110   private static final String CONVERTER_ID = "converter-id";
111   private static final String CONVERTER_FOR_CLASS = "converter-for-class";
112   private static final String CONVERTER_CLASS = "converter-class";
113   private static final String VALIDATOR = "validator";
114   private static final String VALIDATOR_ID = "validator-id";
115   private static final String VALIDATOR_FOR_CLASS = "validator-for-class";
116   private static final String VALIDATOR_CLASS = "validator-class";
117   private static final String RENDERER = "renderer";
118   private static final String COMPONENT_FAMILY = "component-family";
119   private static final String RENDER_KIT = "render-kit";
120   private static final String RENDER_KIT_ID = "render-kit-id";
121   private static final String RENDER_KIT_CLASS = "render-kit-class";
122   private static final String RENDERER_TYPE = "renderer-type";
123   private static final String RENDERER_CLASS = "renderer-class";
124 
125   private static final Set<String> IGNORED_PROPERTIES = new HashSet<String>(Arrays.asList("binding"));
126 
127   private String jsfVersion;
128   private String sourceFacesConfigFile;
129   private String targetFacesConfigFile;
130 
131   @Override
132   public void configure() {
133     final Map<String, String> options = processingEnv.getOptions();
134     jsfVersion = options.get(JSF_VERSION);
135     sourceFacesConfigFile = options.get(SOURCE_FACES_CONFIG);
136     targetFacesConfigFile = options.get(TARGET_FACES_CONFIG);
137 
138     info("Generating the faces-config.xml");
139     info("Options:");
140     info(SOURCE_FACES_CONFIG + ": " + sourceFacesConfigFile);
141     info(TARGET_FACES_CONFIG + ": " + targetFacesConfigFile);
142     info(JSF_VERSION + ": " + jsfVersion);
143   }
144 
145   @Override
146   protected void generate() throws Exception {
147     final Document document;
148     Writer writer = null;
149     try {
150       final String content = FileUtils.fileRead(sourceFacesConfigFile);
151       final SAXBuilder builder = new SAXBuilder();
152       document = builder.build(new StringReader(content));
153 
154       // Normalise line endings. For some reason, JDOM replaces \r\n inside a comment with \n.
155       normaliseLineEndings(document);
156 
157       // rewrite DOM as a string to find differences, since text outside the root element is not tracked
158 
159       final org.jdom.Element rootElement = document.getRootElement();
160 
161       rootElement.setNamespace(Namespace.getNamespace("http://java.sun.com/xml/ns/javaee"));
162       final Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
163       rootElement.addNamespaceDeclaration(Namespace.getNamespace("xi", "http://www.w3.org/2001/XInclude"));
164       rootElement.setAttribute(new Attribute("schemaLocation",
165           "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd", xsi));
166       rootElement.setAttribute("version", "2.0");
167 
168       final Namespace namespace = rootElement.getNamespace();
169       applyNamespace(rootElement, namespace);
170       final List<org.jdom.Element> components = rootElement.getChildren(COMPONENT, namespace);
171 
172       final List<org.jdom.Element> newComponents = new ArrayList<org.jdom.Element>();
173       final List<org.jdom.Element> newRenderer = new ArrayList<org.jdom.Element>();
174       final List<org.jdom.Element> newConverters = new ArrayList<org.jdom.Element>();
175       final List<org.jdom.Element> newValidators = new ArrayList<org.jdom.Element>();
176 
177       for (final TypeElement element : getTypes()) {
178         if (element.getAnnotation(UIComponentTag.class) != null) {
179           addElement(element, newComponents, newRenderer, namespace);
180         } else if (element.getAnnotation(Converter.class) != null) {
181           addConverter(element, newConverters, namespace);
182         } else if (element.getAnnotation(Validator.class) != null) {
183           addValidator(element, newValidators, namespace);
184         }
185       }
186 
187       final List<org.jdom.Element> elementsToAdd = new ArrayList<org.jdom.Element>();
188       // sort out duplicates
189       for (final org.jdom.Element newElement : newComponents) {
190         final boolean found = containsElement(components, newElement);
191         if (!found) {
192           elementsToAdd.add(newElement);
193         }
194       }
195       if (!elementsToAdd.isEmpty()) {
196         // if faces-config contains no component section add the components after factory or application
197         final int lastIndex = getIndexAfter(rootElement, COMPONENT, FACTORY, APPLICATION);
198         rootElement.addContent(lastIndex, elementsToAdd);
199       }
200       if (!newRenderer.isEmpty()) {
201         final org.jdom.Element renderKit = new org.jdom.Element(RENDER_KIT, namespace);
202         final org.jdom.Element renderKitId = new org.jdom.Element(RENDER_KIT_ID, namespace);
203         renderKitId.setText("tobago");
204         renderKit.addContent(renderKitId);
205         final org.jdom.Element renderKitClass = new org.jdom.Element(RENDER_KIT_CLASS, namespace);
206         renderKitClass.setText("org.apache.myfaces.tobago.renderkit.TobagoRenderKit");
207         renderKit.addContent(renderKitClass);
208         renderKit.addContent(newRenderer);
209         final int lastIndex = getIndexAfter(rootElement, CONVERTER, COMPONENT, FACTORY, APPLICATION);
210         rootElement.addContent(lastIndex, renderKit);
211       }
212       if (!newConverters.isEmpty()) {
213         final int lastIndex = getIndexAfter(rootElement, RENDER_KIT, CONVERTER, COMPONENT, FACTORY, APPLICATION);
214         rootElement.addContent(lastIndex, newConverters);
215       }
216       if (!newValidators.isEmpty()) {
217         rootElement.addContent(newValidators);
218       }
219       final FileObject resource = processingEnv.getFiler().createResource(
220           StandardLocation.SOURCE_OUTPUT, "", targetFacesConfigFile);
221       info("Writing to file: " + resource.toUri());
222       writer = resource.openWriter();
223 
224       final StringWriter facesConfig = new StringWriter(1024);
225       final Format format = Format.getPrettyFormat();
226       format.setLineSeparator(SEPARATOR);
227       final XMLOutputter out = new XMLOutputter(format);
228       out.output(document, facesConfig);
229       writer.append(facesConfig.toString());
230 
231     } finally {
232       IOUtils.closeQuietly(writer);
233     }
234   }
235 
236   private void applyNamespace(final org.jdom.Element parent, final Namespace namespace) {
237     for (final org.jdom.Element element : (List<org.jdom.Element>) parent.getChildren()) {
238       element.setNamespace(namespace);
239       applyNamespace(element, namespace);
240     }
241   }
242 
243   private void addConverter(
244       final TypeElement typeElement, final List<org.jdom.Element> newConverters, final Namespace namespace) {
245     final Converter converterAnn = typeElement.getAnnotation(Converter.class);
246     final org.jdom.Element converter = new org.jdom.Element(CONVERTER, namespace);
247     if (converterAnn.id().length() > 0) {
248       final org.jdom.Element converterId = new org.jdom.Element(CONVERTER_ID, namespace);
249       converterId.setText(converterAnn.id());
250       converter.addContent(converterId);
251     } else if (converterAnn.forClass().length() > 0) {
252       final org.jdom.Element converterForClass = new org.jdom.Element(CONVERTER_FOR_CLASS, namespace);
253       converterForClass.setText(converterAnn.forClass());
254       converter.addContent(converterForClass);
255     }
256 
257     final org.jdom.Element converterClass = new org.jdom.Element(CONVERTER_CLASS, namespace);
258     converterClass.setText(typeElement.getQualifiedName().toString());
259     converter.addContent(converterClass);
260     newConverters.add(converter);
261   }
262 
263   private void addValidator(
264       final TypeElement typeElement, final List<org.jdom.Element> newValidators, final Namespace namespace) {
265     final Validator validatorAnn = typeElement.getAnnotation(Validator.class);
266     final org.jdom.Element validator = new org.jdom.Element(VALIDATOR, namespace);
267     if (validatorAnn.id().length() > 0) {
268       final org.jdom.Element validatorId = new org.jdom.Element(VALIDATOR_ID, namespace);
269       validatorId.setText(validatorAnn.id());
270       validator.addContent(validatorId);
271     } else if (validatorAnn.forClass().length() > 0) {
272       final org.jdom.Element validatorForClass = new org.jdom.Element(VALIDATOR_FOR_CLASS, namespace);
273       validatorForClass.setText(validatorAnn.forClass());
274       validator.addContent(validatorForClass);
275     }
276 
277     final org.jdom.Element validatorClass = new org.jdom.Element(VALIDATOR_CLASS, namespace);
278     validatorClass.setText(typeElement.getQualifiedName().toString());
279     validator.addContent(validatorClass);
280     newValidators.add(validator);
281   }
282 
283   private boolean containsElement(final List<org.jdom.Element> components, final org.jdom.Element newElement) {
284     return getEqualElement(components, newElement) != null;
285   }
286 
287   private org.jdom.Element getEqualElement(final List<org.jdom.Element> components, final org.jdom.Element newElement) {
288     for (final org.jdom.Element element : components) {
289       if (equals(element, newElement)) {
290         return element;
291       }
292     }
293     return null;
294   }
295 
296   private int getIndexAfter(final org.jdom.Element rootElement, final String... tagNames) {
297     for (final String tagName : tagNames) {
298       final int index = getIndexAfter(rootElement, tagName);
299       if (index != 0) {
300         return index;
301       }
302     }
303     return 0;
304   }
305 
306   private int getIndexAfter(final org.jdom.Element rootElement, final String tagName) {
307     final List<org.jdom.Element> components = rootElement.getChildren(tagName, rootElement.getNamespace());
308     if (!components.isEmpty()) {
309       return rootElement.indexOf(components.get(components.size() - 1)) + 1;
310     }
311     return 0;
312   }
313 
314   public boolean equals(final org.jdom.Element element1, final org.jdom.Element element2) {
315     final Namespace namespace = element1.getNamespace();
316     if (element1.getName().equals(element2.getName()) && element1.getNamespace().equals(element2.getNamespace())) {
317       if (element1.getChildText(COMPONENT_CLASS, namespace).equals(element2.getChildText(COMPONENT_CLASS, namespace))) {
318         if (element1.getChildText(COMPONENT_TYPE, namespace).equals(element2.getChildText(COMPONENT_TYPE, namespace))) {
319           return true;
320         }
321       }
322     }
323     return false;
324   }
325 
326   protected org.jdom.Element createComponentElement(
327       final ComponentInfo componentInfo, final UIComponentTag componentTag, final Namespace namespace)
328       throws IOException, NoSuchFieldException, IllegalAccessException {
329     final org.jdom.Element element = new org.jdom.Element(COMPONENT, namespace);
330     final org.jdom.Element elementDisplayName = new org.jdom.Element(DISPLAY_NAME, namespace);
331     elementDisplayName.setText(componentInfo.getComponentClassName());
332     element.addContent(elementDisplayName);
333     final org.jdom.Element elementType = new org.jdom.Element(COMPONENT_TYPE, namespace);
334     elementType.setText(componentInfo.getComponentType());
335     element.addContent(elementType);
336     final org.jdom.Element elementClass = new org.jdom.Element(COMPONENT_CLASS, namespace);
337     elementClass.setText(componentTag.uiComponent());
338     element.addContent(elementClass);
339 
340     return element;
341   }
342 
343   protected void addRendererElement(
344       final ComponentInfo componentInfo, final UIComponentTag componentTag, final List<org.jdom.Element> renderer,
345       final Namespace namespace)
346       throws IOException, NoSuchFieldException, IllegalAccessException {
347     final String rendererType = componentTag.rendererType();
348     if (rendererType != null && rendererType.length() > 0) {
349       final org.jdom.Element element = new org.jdom.Element(RENDERER, namespace);
350       String displayName = componentTag.displayName();
351       if (displayName.equals("")) {
352         displayName = componentInfo.getComponentClassName();
353       }
354       final org.jdom.Element elementDisplayName = new org.jdom.Element(DISPLAY_NAME, namespace);
355       elementDisplayName.setText(displayName);
356       element.addContent(elementDisplayName);
357       final org.jdom.Element elementComponentFamily = new org.jdom.Element(COMPONENT_FAMILY, namespace);
358       elementComponentFamily.addContent(componentInfo.getComponentFamily());
359       element.addContent(elementComponentFamily);
360       final org.jdom.Element elementType = new org.jdom.Element(RENDERER_TYPE, namespace);
361       elementType.setText(rendererType);
362       element.addContent(elementType);
363       final org.jdom.Element elementClass = new org.jdom.Element(RENDERER_CLASS, namespace);
364       final String className = "org.apache.myfaces.tobago.renderkit." + rendererType + "Renderer";
365       elementClass.setText(className);
366       element.addContent(elementClass);
367       renderer.add(element);
368     }
369   }
370 
371 
372   private org.jdom.Element createElementExtension(
373       final TypeElement typeElement, final UIComponentTag uiComponentTag,
374       final Namespace namespace) {
375     final org.jdom.Element elementExtension = new org.jdom.Element(COMPONENT_EXTENSION, namespace);
376     final org.jdom.Element elementAllowedChildComponents = new org.jdom.Element(ALLOWED_CHILD_COMPONENTS, namespace);
377     final String[] allowedChildComponents = uiComponentTag.allowedChildComponenents();
378     String allowedComponentTypes = "";
379     for (final String componentType : allowedChildComponents) {
380       allowedComponentTypes += componentType + " ";
381     }
382     elementAllowedChildComponents.setText(allowedComponentTypes);
383     elementExtension.addContent(elementAllowedChildComponents);
384     final org.jdom.Element elementCategory = new org.jdom.Element(CATEGORY, namespace);
385     elementCategory.setText(uiComponentTag.category().toString());
386     elementExtension.addContent(elementCategory);
387     final Deprecated deprecated = typeElement.getAnnotation(Deprecated.class);
388     if (deprecated != null) {
389       final org.jdom.Element elementDeprecated = new org.jdom.Element(DEPRECATED, namespace);
390       elementDeprecated.setText("Warning: This component is deprecated!");
391       elementExtension.addContent(elementDeprecated);
392     }
393     final org.jdom.Element elementHidden = new org.jdom.Element(HIDDEN, namespace);
394     elementHidden.setText(Boolean.toString(uiComponentTag.isHidden()));
395     elementExtension.addContent(elementHidden);
396 
397     return elementExtension;
398   }
399 
400   protected void addAttribute(
401       final ExecutableElement executableElement, final List<org.jdom.Element> attributes,
402       final List<org.jdom.Element> properties,
403       final Namespace namespace) {
404     final UIComponentTagAttribute componentAttribute = executableElement.getAnnotation(UIComponentTagAttribute.class);
405     if (componentAttribute != null) {
406       final String simpleName = executableElement.getSimpleName().toString();
407       if (simpleName.startsWith("set")) {
408         final String name = simpleName.substring(3, 4).toLowerCase(Locale.ENGLISH) + simpleName.substring(4);
409         if (IGNORED_PROPERTIES.contains(name)) {
410           final org.jdom.Element attribute = new org.jdom.Element(ATTRIBUTE, namespace);
411           final org.jdom.Element attributeName = new org.jdom.Element(ATTRIBUTE_NAME, namespace);
412           final org.jdom.Element attributeClass = new org.jdom.Element(ATTRIBUTE_CLASS, namespace);
413 
414           attributeName.setText(name);
415           addClass(componentAttribute, attributeClass);
416 
417           addDescription(executableElement, attribute, namespace);
418 
419           attribute.addContent(attributeName);
420           attribute.addContent(attributeClass);
421           if (componentAttribute.defaultValue().length() > 0) {
422             final org.jdom.Element defaultValue = new org.jdom.Element(DEFAULT_VALUE, namespace);
423             defaultValue.setText(componentAttribute.defaultValue());
424             attribute.addContent(defaultValue);
425           }
426 
427           attribute.addContent(createPropertyOrAttributeExtension(ATTRIBUTE_EXTENSION, executableElement,
428               componentAttribute, namespace));
429 
430           attributes.add(attribute);
431         } else {
432           final org.jdom.Element property = new org.jdom.Element(PROPERTY, namespace);
433           final org.jdom.Element propertyName = new org.jdom.Element(PROPERTY_NAME, namespace);
434           final org.jdom.Element propertyClass = new org.jdom.Element(PROPERTY_CLASS, namespace);
435 
436           propertyName.setText(name);
437           addClass(componentAttribute, propertyClass);
438 
439           addDescription(executableElement, property, namespace);
440 
441           property.addContent(propertyName);
442           property.addContent(propertyClass);
443           if (componentAttribute.defaultValue().length() > 0) {
444             final org.jdom.Element defaultValue = new org.jdom.Element(DEFAULT_VALUE, namespace);
445             defaultValue.setText(componentAttribute.defaultValue());
446             property.addContent(defaultValue);
447           }
448 
449           property.addContent(
450               createPropertyOrAttributeExtension(PROPERTY_EXTENSION, executableElement, componentAttribute, namespace));
451           properties.add(property);
452         }
453       } else {
454         throw new IllegalArgumentException("Only setter allowed found: " + simpleName);
455       }
456     }
457   }
458 
459   private void addClass(final UIComponentTagAttribute componentAttribute, final org.jdom.Element attributeClass) {
460     if (componentAttribute.type().length > 1) {
461       attributeClass.setText(Object.class.getName());
462     } else if (componentAttribute.type().length == 1) {
463       String className = componentAttribute.type()[0];
464       if (componentAttribute.expression().isMethodExpression()) {
465         className = "javax.el.MethodExpression";
466       }
467       attributeClass.setText(className);
468     } else {
469       if (componentAttribute.expression().isMethodExpression()) {
470         attributeClass.setText("javax.el.MethodExpression");
471       }
472     }
473   }
474 
475   private void addDescription(
476       final ExecutableElement element, final org.jdom.Element attribute, final Namespace namespace) {
477     String comment = processingEnv.getElementUtils().getDocComment(element);
478     if (comment != null) {
479       final int index = comment.indexOf('@');
480       if (index != -1) {
481         comment = comment.substring(0, index);
482       }
483       comment = comment.trim();
484       if (comment.length() > 0) {
485         final org.jdom.Element description = new org.jdom.Element(DESCRIPTION, namespace);
486         description.setText(comment);
487         attribute.addContent(description);
488       }
489     }
490   }
491 
492   private org.jdom.Element createPropertyOrAttributeExtension(
493       final String extensionType, final ExecutableElement executableElement,
494       final UIComponentTagAttribute uiComponentTagAttribute,
495       final Namespace namespace)
496       throws IllegalArgumentException {
497     final org.jdom.Element extensionElement = new org.jdom.Element(extensionType, namespace);
498     final org.jdom.Element valueExpression = new org.jdom.Element(VALUE_EXPRESSION, namespace);
499     valueExpression.setText(uiComponentTagAttribute.expression().toMetaDataString());
500     extensionElement.addContent(valueExpression);
501     final String[] allowedValues = uiComponentTagAttribute.allowedValues();
502     if (allowedValues.length > 0) {
503       final org.jdom.Element propertyValues = new org.jdom.Element(PROPERTY_VALUES, namespace);
504       String values = "";
505       for (final String value : allowedValues) {
506         values += value + " ";
507       }
508       propertyValues.setText(values);
509       extensionElement.addContent(propertyValues);
510     }
511     final Deprecated deprecated = executableElement.getAnnotation(Deprecated.class);
512     if (deprecated != null) {
513       final org.jdom.Element elementDeprecated = new org.jdom.Element(DEPRECATED, namespace);
514       elementDeprecated.setText("Warning: This property is deprecated!");
515       extensionElement.addContent(elementDeprecated);
516     }
517     final org.jdom.Element hidden = new org.jdom.Element(HIDDEN, namespace);
518     hidden.setText(Boolean.toString(uiComponentTagAttribute.isHidden()));
519     extensionElement.addContent(hidden);
520     final org.jdom.Element readOnly = new org.jdom.Element(READONLY, namespace);
521     readOnly.setText(Boolean.toString(uiComponentTagAttribute.isReadOnly()));
522     extensionElement.addContent(readOnly);
523     final TagAttribute tagAttribute = executableElement.getAnnotation(TagAttribute.class);
524     if (tagAttribute != null) {
525       final org.jdom.Element required = new org.jdom.Element(REQUIRED, namespace);
526       required.setText(Boolean.toString(tagAttribute.required()));
527       extensionElement.addContent(required);
528     }
529 
530     return extensionElement;
531   }
532 
533   protected void addAttributes(
534       final TypeElement typeElement, final List<org.jdom.Element> attributes, final List<org.jdom.Element> properties,
535       final Namespace namespace) {
536 
537     for (final javax.lang.model.element.Element element : processingEnv.getElementUtils().getAllMembers(typeElement)) {
538       final ExecutableElement executableElement = (ExecutableElement) element;
539       if (executableElement.getAnnotation(TagAttribute.class) == null
540           && executableElement.getAnnotation(UIComponentTagAttribute.class) == null) {
541         continue;
542       }
543 
544       addAttribute(executableElement, attributes, properties, namespace);
545     }
546   }
547 
548   private void addFacets(final UIComponentTag componentTag, final Namespace namespace, final org.jdom.Element element) {
549     final Facet[] facets = componentTag.facets();
550     for (final Facet facet : facets) {
551       final org.jdom.Element facetElement = new org.jdom.Element(FACET, namespace);
552       final String description = facet.description();
553       if (description != null && description.length() > 0) {
554         final org.jdom.Element facetDescription = new org.jdom.Element(DESCRIPTION, namespace);
555         facetDescription.setText(description);
556         facetElement.addContent(facetDescription);
557       }
558       final org.jdom.Element facetName = new org.jdom.Element(FACET_NAME, namespace);
559       facetName.setText(facet.name());
560       facetElement.addContent(facetName);
561       final org.jdom.Element facetExtension = new org.jdom.Element(FACET_EXTENSION, namespace);
562       final org.jdom.Element elementAllowedChildComponents = new org.jdom.Element(ALLOWED_CHILD_COMPONENTS, namespace);
563       final String[] allowedChildComponents = facet.allowedChildComponenents();
564       String allowedComponentTypes = "";
565       for (final String componentType : allowedChildComponents) {
566         allowedComponentTypes += componentType + " ";
567       }
568       elementAllowedChildComponents.setText(allowedComponentTypes);
569       facetExtension.addContent(elementAllowedChildComponents);
570       facetElement.addContent(facetExtension);
571       element.addContent(facetElement);
572     }
573   }
574 
575   protected void addElement(
576       final TypeElement typeElement, final List<org.jdom.Element> components, final List<org.jdom.Element> renderer,
577       final Namespace namespace) throws Exception {
578     final UIComponentTag componentTag = typeElement.getAnnotation(UIComponentTag.class);
579     if (componentTag != null) {
580       final ComponentInfo componentInfo = new ComponentInfo(typeElement, componentTag);
581       if (!componentTag.isComponentAlreadyDefined()) {
582         final org.jdom.Element element = createComponentElement(componentInfo, componentTag, namespace);
583         if (element != null) {
584           if (!containsElement(components, element)) {
585             addFacets(componentTag, namespace, element);
586             final List<org.jdom.Element> attributes = new ArrayList<org.jdom.Element>();
587             final List<org.jdom.Element> properties = new ArrayList<org.jdom.Element>();
588             addAttributes(typeElement, attributes, properties, namespace);
589             if (!attributes.isEmpty()) {
590               Collections.sort(attributes, new Comparator<org.jdom.Element>() {
591                 public int compare(final org.jdom.Element d1, final org.jdom.Element d2) {
592                   return d1.getChildText(ATTRIBUTE_NAME, namespace).compareTo(
593                       d2.getChildText(ATTRIBUTE_NAME, namespace));
594                 }
595               });
596               element.addContent(attributes);
597             }
598             if (!properties.isEmpty()) {
599               Collections.sort(properties, new Comparator<org.jdom.Element>() {
600                 public int compare(final org.jdom.Element d1, final org.jdom.Element d2) {
601                   return d1.getChildText(PROPERTY_NAME, namespace).compareTo(
602                       d2.getChildText(PROPERTY_NAME, namespace));
603                 }
604               });
605               element.addContent(properties);
606             }
607             element.addContent(createElementExtension(typeElement, componentTag, namespace));
608             components.add(element);
609           } else {
610             // TODO add facet and attributes
611           }
612         }
613       }
614       addRendererElement(componentInfo, componentTag, renderer, namespace);
615     }
616   }
617 
618   private void normaliseLineEndings(final Document document) {
619     final Iterator i = document.getDescendants(new ContentFilter(ContentFilter.COMMENT));
620     while (i.hasNext()) {
621       final Comment c = (Comment) i.next();
622       c.setText(c.getText().replaceAll("\n", SEPARATOR));
623     }
624   }
625 
626   private boolean is20() {
627     return "2.0".equals(jsfVersion);
628   }
629 }