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