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