This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-maven-jcrocm-plugin.git
commit 5d73e609dd7f02e82e0ae38afe3f61d08df19c04 Author: Robert Munteanu <[email protected]> AuthorDate: Wed Jul 24 18:13:22 2013 +0000 SLING-2978 - Create tooling top-level directory and move maven and ide under it * moved the maven directory under tooling maven * adjusted module paths in pom.xml * updated the scm information in the pom.xml files * updated the scm information in the README.txt files git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1506645 13f79535-47bb-0310-9956-ffa450edef68 --- LICENSE | 202 ++++++++++++++++ NOTICE | 10 + README.txt | 29 +++ pom.xml | 92 ++++++++ .../maven/jcrocm/AbstractDescriptorElement.java | 69 ++++++ .../sling/maven/jcrocm/AbstractItemDescriptor.java | 124 ++++++++++ .../sling/maven/jcrocm/AbstractNodeDescriptor.java | 107 +++++++++ .../apache/sling/maven/jcrocm/BeanDescriptor.java | 122 ++++++++++ .../apache/sling/maven/jcrocm/ClassDescriptor.java | 254 +++++++++++++++++++++ .../sling/maven/jcrocm/CollectionDescriptor.java | 139 +++++++++++ .../apache/sling/maven/jcrocm/FieldDescriptor.java | 172 ++++++++++++++ .../org/apache/sling/maven/jcrocm/JcrOcmMojo.java | 236 +++++++++++++++++++ .../org/apache/sling/maven/jcrocm/XMLWriter.java | 101 ++++++++ src/main/resources/META-INF/LICENSE | 202 ++++++++++++++++ src/main/resources/META-INF/NOTICE | 25 ++ 15 files changed, 1884 insertions(+) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..b37bc45 --- /dev/null +++ b/NOTICE @@ -0,0 +1,10 @@ +Apache Sling Maven JCROCM Plugin +Copyright 2008-2010 The Apache Software Foundation + +Apache Sling is based on source code originally developed +by Day Software (http://www.day.com/). + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + + diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..954ff12 --- /dev/null +++ b/README.txt @@ -0,0 +1,29 @@ +Apache Sling Maven JCROCM Plugin + +The Maven JcrOCM Plugin supports extracting and generation of +Jackrabbit OCM descriptors from JavaDoc tags embedded in the +Java Classes contained in the project sources + +Getting Started +=============== + +This component uses a Maven 2 (http://maven.apache.org/) build +environment. It requires a Java 5 JDK (or higher) and Maven (http://maven.apache.org/) +2.0.7 or later. We recommend to use the latest Maven version. + +If you have Maven 2 installed, you can compile and +package the jar using the following command: + + mvn package + +See the Maven 2 documentation for other build features. + +The latest source code for this component is available in the +Subversion (http://subversion.tigris.org/) source repository of +the Apache Software Foundation. If you have Subversion installed, +you can checkout the latest source using the following command: + + svn checkout http://svn.apache.org/repos/asf/sling/trunk/tooling/maven/maven-jcrocm-plugin + +See the Subversion documentation for other source control features. + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..633805c --- /dev/null +++ b/pom.xml @@ -0,0 +1,92 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.sling</groupId> + <artifactId>sling</artifactId> + <version>17</version> + <relativePath>../../parent/pom.xml</relativePath> + </parent> + + <artifactId>maven-jcrocm-plugin</artifactId> + <version>2.0.5-SNAPSHOT</version> + <packaging>maven-plugin</packaging> + + <name> + Apache Sling Maven Plugin to create Jackrabbit OCM descriptors + </name> + <description> + The Maven JcrOCM Plugin supports extracting and generation of + Jackrabbit OCM descriptors from JavaDoc tags embedded in the + Java Classes contained in the project sources + </description> + + <scm> + <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/tooling/maven/maven-jcrocm-plugin</connection> + <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/tooling/maven/maven-jcrocm-plugin</developerConnection> + <url>http://svn.apache.org/viewvc/sling/trunk/tooling/maven/maven-jcrocm-plugin</url> + </scm> + + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-plugin-plugin</artifactId> + <version>2.5.1</version> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <configuration> + <!-- No javadocs --> + <excludePackageNames> + org.apache.sling + </excludePackageNames> + </configuration> + </plugin> + </plugins> + </reporting> + + <dependencies> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <version>2.0</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-artifact</artifactId> + <version>2.0</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-project</artifactId> + <version>2.0</version> + </dependency> + + <dependency> + <groupId>com.thoughtworks.qdox</groupId> + <artifactId>qdox</artifactId> + <version>1.9.1</version> + </dependency> + </dependencies> +</project> diff --git a/src/main/java/org/apache/sling/maven/jcrocm/AbstractDescriptorElement.java b/src/main/java/org/apache/sling/maven/jcrocm/AbstractDescriptorElement.java new file mode 100644 index 0000000..0f4c992 --- /dev/null +++ b/src/main/java/org/apache/sling/maven/jcrocm/AbstractDescriptorElement.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.maven.jcrocm; + +import com.thoughtworks.qdox.model.AbstractBaseJavaEntity; +import org.apache.maven.plugin.logging.Log; + +import com.thoughtworks.qdox.model.AbstractJavaEntity; +import com.thoughtworks.qdox.model.DocletTag; +import com.thoughtworks.qdox.model.JavaClass; +import com.thoughtworks.qdox.model.JavaClassParent; + +/** + * The <code>AbstractDescriptorElement</code> serves as the base class for the + * descriptor objects read from the Doclet tags. + */ +abstract class AbstractDescriptorElement { + + private final Log log; + + protected final DocletTag tag; + + protected AbstractDescriptorElement(Log log, DocletTag tag) { + this.log = log; + this.tag = tag; + } + + abstract boolean validate(); + + abstract void generate(XMLWriter xmlWriter); + + JavaClass getJavaClass() { + AbstractBaseJavaEntity aje = tag.getContext(); + if (aje instanceof JavaClass) { + return (JavaClass) aje; + } + + JavaClassParent parent = aje.getParent(); + if (parent instanceof JavaClass) { + return (JavaClass) parent; + } + + return null; + } + + protected void log(String message) { + log.error("@" + tag.getName() + ": " + message + " (" + + tag.getContext().getName() + ", line " + + tag.getLineNumber() + ")"); + } + + protected void warn(String message) { + log.warn(message); + } +} diff --git a/src/main/java/org/apache/sling/maven/jcrocm/AbstractItemDescriptor.java b/src/main/java/org/apache/sling/maven/jcrocm/AbstractItemDescriptor.java new file mode 100644 index 0000000..eea14ad --- /dev/null +++ b/src/main/java/org/apache/sling/maven/jcrocm/AbstractItemDescriptor.java @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.maven.jcrocm; + +import org.apache.maven.plugin.logging.Log; + +import com.thoughtworks.qdox.model.DocletTag; +import com.thoughtworks.qdox.model.JavaMethod; + +/** + * The <code>AbstractItemDescriptor</code> is the base class for the field, + * bean and collection descriptors providing support for the common + * configuration elements. + */ +abstract class AbstractItemDescriptor extends AbstractDescriptorElement { + + /** + * The name of the Doclet Tag attribute naming the field to which the + * descriptor applies (value is "fieldName"). + */ + public static final String FIELD_NAME = "fieldName"; + + /** + * The name of the Doclet Tag attribute naming the JCR item to which the + * descriptor applies (value is "jcrName"). + */ + public static final String JCR_NAME = "jcrName"; + + /** + * The name of the Doclet Tag attribute naming the field to which the + * descriptor applies (value is "fieldName"). + */ + public static final String JCR_AUTO_CREATED = "jcrAutoCreated"; + + public static final String JCR_MANDATORY = "jcrMandatory"; + + public static final String JCR_ON_PARENT_VERSION = "jcrOnParentVersion"; + + public static final String JCR_PROTECTED = "jcrProtected"; + + private String fieldName; + + private String jcrName; + + private boolean isJcrAutoCreated; + + private boolean isJcrMandatory; + + private String jcrOnParentVersion; + + private boolean isJcrProtected; + + /** + * @param log + * @param tag + */ + public AbstractItemDescriptor(Log log, DocletTag tag, String fieldName) { + super(log, tag); + + this.fieldName = fieldName; + jcrName = tag.getNamedParameter(JCR_NAME); + isJcrAutoCreated = Boolean.valueOf( + tag.getNamedParameter(JCR_AUTO_CREATED)).booleanValue(); + isJcrMandatory = Boolean.valueOf(tag.getNamedParameter(JCR_MANDATORY)).booleanValue(); + jcrOnParentVersion = tag.getNamedParameter(JCR_ON_PARENT_VERSION); + isJcrProtected = Boolean.valueOf(tag.getNamedParameter(JCR_PROTECTED)).booleanValue(); + } + + void generate(XMLWriter xmlWriter) { + xmlWriter.printAttribute(FIELD_NAME, fieldName); + xmlWriter.printAttribute(JCR_NAME, jcrName); + xmlWriter.printAttribute(JCR_AUTO_CREATED, isJcrAutoCreated); + xmlWriter.printAttribute(JCR_MANDATORY, isJcrMandatory); + xmlWriter.printAttribute(JCR_ON_PARENT_VERSION, jcrOnParentVersion); + xmlWriter.printAttribute(JCR_PROTECTED, isJcrProtected); + } + + /* + * (non-Javadoc) + * + * @see org.apache.sling.maven.jcrocm.AbstractDescriptorElement#validate() + */ + boolean validate() { + boolean valid = true; + + if (jcrOnParentVersion != null) { + if (!jcrOnParentVersion.equals("COPY") + && !jcrOnParentVersion.equals("VERSION") + && !jcrOnParentVersion.equals("INITIALIZE") + && !jcrOnParentVersion.equals("COMPUTE") + && !jcrOnParentVersion.equals("IGNORE") + && !jcrOnParentVersion.equals("ABORT")) { + log("Invalid JCR Field OnParentVersion: " + jcrOnParentVersion); + valid = false; + } + } + + return valid; + } + + static String getFieldFromMethod(JavaMethod method) { + String fieldName = method.getName(); + if ((fieldName.startsWith("get") || fieldName.startsWith("set")) + && fieldName.length() >= 4) { + fieldName = Character.toLowerCase(fieldName.charAt(3)) + + fieldName.substring(4); + } + return fieldName; + } +} diff --git a/src/main/java/org/apache/sling/maven/jcrocm/AbstractNodeDescriptor.java b/src/main/java/org/apache/sling/maven/jcrocm/AbstractNodeDescriptor.java new file mode 100644 index 0000000..6e4dab1 --- /dev/null +++ b/src/main/java/org/apache/sling/maven/jcrocm/AbstractNodeDescriptor.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.maven.jcrocm; + +import org.apache.maven.plugin.logging.Log; + +import com.thoughtworks.qdox.model.DocletTag; + +/** + * The <code>AbstractNodeDescriptor</code> is the base class for mostly node + * based descriptors (bean, collection). + */ +public class AbstractNodeDescriptor extends AbstractItemDescriptor { + + public static final String PROXY = "proxy"; + + public static final String AUTO_RETRIEVE = "autoRetrieve"; + + public static final String AUTO_UPDATE = "autoUpdate"; + + public static final String AUTO_INSERT = "autoInsert"; + + public static final String JCR_TYPE = "jcrType"; + + public static final String JCR_SAME_NAME_SIBBLINGS = "jcrSameNameSiblings"; + + private boolean isProxy = true; + + private boolean isAutoRetrieve = true; + + private boolean isAutoUpdate = true; + + private boolean isAutoInsert = true; + + private String jcrType; + + private boolean isJcrSameNameSibblings; + + /** + * @param log + * @param tag + */ + public AbstractNodeDescriptor(Log log, DocletTag tag, String fieldName) { + super(log, tag, fieldName); + + jcrType = tag.getNamedParameter(JCR_TYPE); + isJcrSameNameSibblings = Boolean.valueOf( + tag.getNamedParameter(JCR_SAME_NAME_SIBBLINGS)).booleanValue(); + + if (tag.getNamedParameter(PROXY) != null) { + isProxy = Boolean.valueOf(tag.getNamedParameter(PROXY)).booleanValue(); + } + if (tag.getNamedParameter(AUTO_RETRIEVE) != null) { + isAutoRetrieve = Boolean.valueOf( + tag.getNamedParameter(AUTO_RETRIEVE)).booleanValue(); + } + if (tag.getNamedParameter(AUTO_UPDATE) != null) { + isAutoUpdate = Boolean.valueOf(tag.getNamedParameter(AUTO_UPDATE)).booleanValue(); + } + if (tag.getNamedParameter(AUTO_INSERT) != null) { + isAutoInsert = Boolean.valueOf(tag.getNamedParameter(AUTO_INSERT)).booleanValue(); + } + } + + void generate(XMLWriter xmlWriter) { + super.generate(xmlWriter); + + xmlWriter.printAttribute(JCR_TYPE, jcrType); + xmlWriter.printAttribute(JCR_SAME_NAME_SIBBLINGS, + isJcrSameNameSibblings); + + if (!isProxy) { + xmlWriter.printAttribute(PROXY, "false"); + } + if (!isAutoRetrieve) { + xmlWriter.printAttribute(AUTO_RETRIEVE, "false"); + } + if (!isAutoUpdate) { + xmlWriter.printAttribute(AUTO_UPDATE, "false"); + } + if (!isAutoInsert) { + xmlWriter.printAttribute(AUTO_INSERT, "false"); + } + } + + boolean validate() { + boolean valid = super.validate(); + + // do additional validation + + return valid; + } +} diff --git a/src/main/java/org/apache/sling/maven/jcrocm/BeanDescriptor.java b/src/main/java/org/apache/sling/maven/jcrocm/BeanDescriptor.java new file mode 100644 index 0000000..53d79f6 --- /dev/null +++ b/src/main/java/org/apache/sling/maven/jcrocm/BeanDescriptor.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.maven.jcrocm; + +import org.apache.maven.plugin.logging.Log; + +import com.thoughtworks.qdox.model.DocletTag; +import com.thoughtworks.qdox.model.JavaField; +import com.thoughtworks.qdox.model.JavaMethod; + +/** + * The <code>BeanDescriptor</code> class provides support for the + * bean-descriptor element of a class mapping, which has the following attribute + * list definition: + * + * <pre> + * <!ATTLIST bean-descriptor + * fieldName CDATA #REQUIRED + * jcrName CDATA #IMPLIED + * proxy (true | false) "false" + * autoRetrieve (true|false) "true" + * autoUpdate (true|false) "true" + * autoInsert (true|false) "true" + * converter CDATA #IMPLIED + * jcrType CDATA #IMPLIED + * jcrAutoCreated (true | false) "false" + * jcrMandatory (true | false) "false" + * jcrOnParentVersion (COPY | VERSION | INITIALIZE | COMPUTE | IGNORE | ABORT) "COPY" + * jcrProtected (true | false) "false" + * jcrSameNameSiblings (true | false) "false" + * > + * </pre> + */ +public class BeanDescriptor extends AbstractNodeDescriptor { + + public static final String TAG_BEAN_DESCRIPTOR = "ocm.bean"; + + public static final String CONVERTER = "converter"; + + public static final String ELEMENT_BEAN_DESCRIPTOR = "bean-descriptor"; + + private String converter; + + static BeanDescriptor fromField(Log log, JavaField field) { + + DocletTag tag = field.getTagByName(TAG_BEAN_DESCRIPTOR); + if (tag == null) { + return null; + } + + return new BeanDescriptor(log, tag, field.getName()); + } + + static BeanDescriptor fromMethod(Log log, JavaMethod method) { + + DocletTag tag = method.getTagByName(TAG_BEAN_DESCRIPTOR); + if (tag == null) { + return null; + } + + // field name is the method name, unless overwritten with the fieldName + // tag + String fieldName = tag.getNamedParameter(FIELD_NAME); + if (fieldName == null) { + fieldName = getFieldFromMethod(method); + } + + return new BeanDescriptor(log, tag, fieldName); + } + + /** + * @param log + * @param tag + */ + private BeanDescriptor(Log log, DocletTag tag, String fieldName) { + super(log, tag, fieldName); + + converter = tag.getNamedParameter(CONVERTER); + } + + /* + * (non-Javadoc) + * + * @see org.apache.sling.maven.jcrocm.AbstractDescriptorElement#generate(org.apache.sling.maven.jcrocm.XMLWriter) + */ + void generate(XMLWriter xmlWriter) { + xmlWriter.printElementStart(ELEMENT_BEAN_DESCRIPTOR, true); + super.generate(xmlWriter); + + xmlWriter.printAttribute(CONVERTER, converter); + + xmlWriter.printElementStartClose(true); + } + + /* + * (non-Javadoc) + * + * @see org.apache.sling.maven.jcrocm.AbstractDescriptorElement#validate() + */ + boolean validate() { + boolean valid = super.validate(); + + // do additional validation + + return valid; + } + +} diff --git a/src/main/java/org/apache/sling/maven/jcrocm/ClassDescriptor.java b/src/main/java/org/apache/sling/maven/jcrocm/ClassDescriptor.java new file mode 100644 index 0000000..68a77de --- /dev/null +++ b/src/main/java/org/apache/sling/maven/jcrocm/ClassDescriptor.java @@ -0,0 +1,254 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.maven.jcrocm; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.StringTokenizer; + +import org.apache.maven.plugin.logging.Log; + +import com.thoughtworks.qdox.model.DocletTag; +import com.thoughtworks.qdox.model.JavaClass; + +/** + * The <code>ClassDescriptor</code> class provides support for the + * class-descriptor element of a class mapping, which has the following + * attribute list definition: + * + * <pre> + * <!ATTLIST class-descriptor + * className CDATA #REQUIRED + * jcrType CDATA #IMPLIED + * jcrSuperTypes CDATA #IMPLIED + * jcrMixinTypes CDATA #IMPLIED + * extend CDATA #IMPLIED + * abstract (true|false) "false" + * interface (true|false) "false" + * discriminator (true|false) "true" + * > + * <!ATTLIST implement-descriptor + * interfaceName CDATA #REQUIRED + * > + * </pre> + * + * <p> + * Additionally, as can be seen from above, the + * <code>implement-descriptor</code> is also supported by this class. It + * retrieves the list of interfaces from the described class. + * </p> + */ +public class ClassDescriptor extends AbstractDescriptorElement { + + public static final String TAG_CLASS_DESCRIPTOR = "ocm.mapped"; + + public static final String ELEMENT_CLASS_DESCRIPTOR = "class-descriptor"; + + public static final String CLASS_NAME = "className"; + + public static final String JCR_TYPE = "jcrType"; + + public static final String JCR_SUPER_TYPES = "jcrSuperTypes"; + + public static final String JCR_MIXIN_TYPES = "jcrMixinTypes"; + + public static final String EXTEND = "extend"; + + public static final String INTERFACES = "interfaces"; + + public static final String ABSTRACT = "abstract"; + + public static final String INTERFACE = "interface"; + + public static final String DISCRIMINATOR = "discriminator"; + + public static final String ELEMENT_IMPLEMENT_DESCRIPTOR = "implement-descriptor"; + + public static final String INTERFACE_NAME = "interfaceName"; + + private String className; + + private String jcrType; + + private String jcrSuperTypes; + + private String jcrMixinTypes; + + private boolean discriminator = true; + + private String extend; + + private boolean isAbstract; + + private boolean isInterface; + + private Set interfaces; + + private List children; + + static ClassDescriptor fromClass(Log log, JavaClass javaClass) { + + DocletTag tag = javaClass.getTagByName(TAG_CLASS_DESCRIPTOR); + if (tag == null) { + return null; + } + + ClassDescriptor cd = new ClassDescriptor(log, tag); + cd.className = javaClass.getFullyQualifiedName(); + cd.isInterface = javaClass.isInterface(); + cd.isAbstract = !cd.isInterface && javaClass.isAbstract(); + + cd.extend = tag.getNamedParameter(EXTEND); + if (cd.extend == null) { + if (javaClass.getSuperJavaClass() != null) { + cd.extend = javaClass.getSuperJavaClass().getFullyQualifiedName(); + + // do not declare extending Object :-) + if (Object.class.getName().equals(cd.extend)) { + cd.extend = null; + } + } + } else if (cd.extend.length() == 0) { + // explicit empty extend value prevents extend attribute from being + // set + cd.extend = null; + } + + String interfaceList = tag.getNamedParameter(INTERFACES); + if (interfaceList == null) { + if (javaClass.getImplementedInterfaces() != null) { + JavaClass[] implInterfaces = javaClass.getImplementedInterfaces(); + cd.interfaces = new HashSet(); + for (int i = 0; i < implInterfaces.length; i++) { + cd.interfaces.add(implInterfaces[i].getFullyQualifiedName()); + } + } + } else if (interfaceList.length() == 0) { + // empty interface list prevents creation of interface element + cd.interfaces = null; + } else { + // split list and create set for interface elements + StringTokenizer tokener = new StringTokenizer(interfaceList, ","); + cd.interfaces = new HashSet(); + while (tokener.hasMoreTokens()) { + String iface = tokener.nextToken().trim(); + if (iface.length() > 0) { + cd.interfaces.add(iface); + } + } + } + + cd.jcrType = tag.getNamedParameter(JCR_TYPE); + cd.jcrSuperTypes = tag.getNamedParameter(JCR_SUPER_TYPES); + cd.jcrMixinTypes = tag.getNamedParameter(JCR_MIXIN_TYPES); + + // only reset default if explicitly stated + if (tag.getNamedParameter(DISCRIMINATOR) != null) { + cd.discriminator = Boolean.valueOf( + tag.getNamedParameter(DISCRIMINATOR)).booleanValue(); + } + + return cd; + } + + private ClassDescriptor(Log log, DocletTag tag) { + super(log, tag); + } + + void addChild(AbstractDescriptorElement child) { + if (child != null) { + if (children == null) { + children = new ArrayList(); + } + + children.add(child); + } + } + + /* + * (non-Javadoc) + * + * @see org.apache.sling.maven.jcrocm.AbstractDescriptorElement#generate(org.apache.sling.maven.jcrocm.XMLWriter) + */ + void generate(XMLWriter xmlWriter) { + + xmlWriter.println(); + xmlWriter.printComment("Class: " + className); + + xmlWriter.printElementStart(ELEMENT_CLASS_DESCRIPTOR, true); + xmlWriter.printAttribute(CLASS_NAME, className); + xmlWriter.printAttribute(JCR_TYPE, jcrType); + xmlWriter.printAttribute(JCR_SUPER_TYPES, jcrSuperTypes); + xmlWriter.printAttribute(JCR_MIXIN_TYPES, jcrMixinTypes); + xmlWriter.printAttribute(EXTEND, extend); + xmlWriter.printAttribute(ABSTRACT, isAbstract); + xmlWriter.printAttribute(INTERFACE, isInterface); + + // only write discriminator if false, true is the default here + if (!discriminator) { + xmlWriter.printAttribute(DISCRIMINATOR, "false"); + } + + xmlWriter.printElementStartClose(false); + + // interface implementations + if (interfaces != null) { + for (Iterator ii = interfaces.iterator(); ii.hasNext();) { + String iface = (String) ii.next(); + xmlWriter.println(); + xmlWriter.printElementStart(ELEMENT_IMPLEMENT_DESCRIPTOR, true); + xmlWriter.printAttribute(INTERFACE_NAME, iface); + xmlWriter.printElementStartClose(true); + } + } + + // fields, beans and collections + if (children != null) { + for (Iterator ci = children.iterator(); ci.hasNext();) { + AbstractDescriptorElement child = (AbstractDescriptorElement) ci.next(); + xmlWriter.println(); + child.generate(xmlWriter); + } + } + + xmlWriter.printElementEnd(ELEMENT_CLASS_DESCRIPTOR); + } + + /* + * (non-Javadoc) + * + * @see org.apache.sling.maven.jcrocm.AbstractDescriptorElement#validate() + */ + boolean validate() { + boolean valid = true; + + if (children != null) { + if (children != null) { + for (Iterator ci = children.iterator(); ci.hasNext();) { + AbstractDescriptorElement child = (AbstractDescriptorElement) ci.next(); + valid &= child.validate(); + if (!valid) break; + } + } + } + + return valid; + } +} diff --git a/src/main/java/org/apache/sling/maven/jcrocm/CollectionDescriptor.java b/src/main/java/org/apache/sling/maven/jcrocm/CollectionDescriptor.java new file mode 100644 index 0000000..c5cfdfc --- /dev/null +++ b/src/main/java/org/apache/sling/maven/jcrocm/CollectionDescriptor.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.maven.jcrocm; + +import org.apache.maven.plugin.logging.Log; + +import com.thoughtworks.qdox.model.DocletTag; +import com.thoughtworks.qdox.model.JavaField; +import com.thoughtworks.qdox.model.JavaMethod; + +/** + * The <code>CollectionDescriptor</code> class provides support for the + * collection-descriptor element of a class mapping, which has the following + * attribute list definition: + * + * <pre> + * <!ATTLIST collection-descriptor + * fieldName CDATA #REQUIRED + * jcrName CDATA #IMPLIED + * proxy (true | false) "false" + * autoRetrieve (true|false) "true" + * autoUpdate (true|false) "true" + * autoInsert (true|false) "true" + * elementClassName CDATA #IMPLIED + * collectionClassName CDATA #IMPLIED + * collectionConverter CDATA #IMPLIED + * jcrType CDATA #IMPLIED + * jcrAutoCreated (true | false) "false" + * jcrMandatory (true | false) "false" + * jcrOnParentVersion (COPY | VERSION | INITIALIZE | COMPUTE | IGNORE | ABORT) "COPY" + * jcrProtected (true | false) "false" + * jcrSameNameSiblings (true | false) "false" + * > + * </pre> + */ +public class CollectionDescriptor extends AbstractNodeDescriptor { + + public static final String TAG_COLLECTION_DESCRIPTOR = "ocm.collection"; + + public static final String ELEMENT_COLLECTION_DESCRIPTOR = "collection-descriptor"; + + private static final String ELEMENT_CLASS_NAME = "elementClassName"; + + private static final String COLLECTION_CLASS_NAME = "collectionClassName"; + + private static final String COLLECTION_CONVERTER = "collectionConverter"; + + private String elementClassName; + + private String collectionClassName; + + private String collectionConverter; + + static CollectionDescriptor fromField(Log log, JavaField field) { + + DocletTag tag = field.getTagByName(TAG_COLLECTION_DESCRIPTOR); + if (tag == null) { + return null; + } + + return new CollectionDescriptor(log, tag, field.getName()); + } + + static CollectionDescriptor fromMethod(Log log, JavaMethod method) { + + DocletTag tag = method.getTagByName(TAG_COLLECTION_DESCRIPTOR); + if (tag == null) { + return null; + } + + // field name is the method name, unless overwritten with the fieldName + // tag + String fieldName = tag.getNamedParameter(FIELD_NAME); + if (fieldName == null) { + fieldName = getFieldFromMethod(method); + } + + return new CollectionDescriptor(log, tag, fieldName); + } + + /** + * @param log + * @param tag + */ + public CollectionDescriptor(Log log, DocletTag tag, String fieldName) { + super(log, tag, fieldName); + + elementClassName = tag.getNamedParameter(ELEMENT_CLASS_NAME); + collectionClassName = tag.getNamedParameter(COLLECTION_CLASS_NAME); + collectionConverter = tag.getNamedParameter(COLLECTION_CONVERTER); + } + + /* + * (non-Javadoc) + * + * @see org.apache.sling.maven.jcrocm.AbstractDescriptorElement#generate(org.apache.sling.maven.jcrocm.XMLWriter) + */ + void generate(XMLWriter xmlWriter) { + xmlWriter.printElementStart(ELEMENT_COLLECTION_DESCRIPTOR, true); + super.generate(xmlWriter); + + xmlWriter.printAttribute(ELEMENT_CLASS_NAME, elementClassName); + xmlWriter.printAttribute(COLLECTION_CLASS_NAME, collectionClassName); + xmlWriter.printAttribute(COLLECTION_CONVERTER, collectionConverter); + + xmlWriter.printElementStartClose(true); + } + + /* + * (non-Javadoc) + * + * @see org.apache.sling.maven.jcrocm.AbstractDescriptorElement#validate() + */ + boolean validate() { + boolean valid = super.validate(); + + // ensure element class name + if (elementClassName == null) { + elementClassName = Object.class.getName(); + } + + return valid; + } + +} diff --git a/src/main/java/org/apache/sling/maven/jcrocm/FieldDescriptor.java b/src/main/java/org/apache/sling/maven/jcrocm/FieldDescriptor.java new file mode 100644 index 0000000..38d1745 --- /dev/null +++ b/src/main/java/org/apache/sling/maven/jcrocm/FieldDescriptor.java @@ -0,0 +1,172 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.maven.jcrocm; + +import org.apache.maven.plugin.logging.Log; + +import com.thoughtworks.qdox.model.DocletTag; +import com.thoughtworks.qdox.model.JavaField; +import com.thoughtworks.qdox.model.JavaMethod; + +/** + * The <code>FieldDescriptor</code> class provides support for the + * field-descriptor element of a class mapping, which has the following + * attribute list definition: + * + * <pre> + * <!ATTLIST field-descriptor + * fieldName CDATA #REQUIRED + * fieldType CDATA #IMPLIED + * jcrName CDATA #IMPLIED + * id (true | false) "false" + * path (true | false) "false" + * uuid (true | false) "false" + * converter CDATA #IMPLIED + * jcrDefaultValue CDATA #IMPLIED + * jcrValueConstraints CDATA #IMPLIED + * jcrType (String | Date | Long | Double | Boolean | Binary) #IMPLIED + * jcrAutoCreated (true | false) "false" + * jcrMandatory (true | false) "false" + * jcrOnParentVersion (COPY | VERSION | INITIALIZE | COMPUTE | IGNORE | ABORT) "COPY" + * jcrProtected (true | false) "false" + * jcrMultiple (true | false) "false" + * > + * </pre> + */ +public class FieldDescriptor extends AbstractItemDescriptor { + + public static final String TAG_FIELD_DESCRIPTOR = "ocm.field"; + + public static final String ELEMENT_FIELD_DESCRIPTOR = "field-descriptor"; + + public static final String ID = "id"; + + public static final String PATH = "path"; + + public static final String UUID = "uuid"; + + public static final String CONVERTER = "converter"; + + public static final String JCR_DEFAULT_VALUE = "jcrDefaultValue"; + + public static final String JCR_VALUE_CONSTRAINTS = "jcrValueConstraints"; + + public static final String JCR_TYPE = "jcrType"; + + public static final String JCR_MULTIPLE = "jcrMultiple"; + + private boolean isId; + + private boolean isPath; + + private boolean isUuid; + + private String converter; + + private String jcrDefaultValue; + + private String jcrValueConstraints; + + private String jcrType; + + private boolean isJcrMultiple; + + static FieldDescriptor fromField(Log log, JavaField field) { + + DocletTag tag = field.getTagByName(TAG_FIELD_DESCRIPTOR); + if (tag == null) { + return null; + } + + return new FieldDescriptor(log, tag, field.getName()); + } + + static FieldDescriptor fromMethod(Log log, JavaMethod method) { + + DocletTag tag = method.getTagByName(TAG_FIELD_DESCRIPTOR); + if (tag == null) { + return null; + } + + // field name is the method name, unless overwritten with the fieldName + // tag + String fieldName = tag.getNamedParameter(FIELD_NAME); + if (fieldName == null) { + fieldName = getFieldFromMethod(method); + } + + return new FieldDescriptor(log, tag, fieldName); + } + + private FieldDescriptor(Log log, DocletTag tag, String fieldName) { + super(log, tag, fieldName); + + isId = Boolean.valueOf(tag.getNamedParameter(ID)).booleanValue(); + isPath = Boolean.valueOf(tag.getNamedParameter(PATH)).booleanValue(); + isUuid = Boolean.valueOf(tag.getNamedParameter(UUID)).booleanValue(); + + converter = tag.getNamedParameter(CONVERTER); + jcrDefaultValue = tag.getNamedParameter(JCR_DEFAULT_VALUE); + jcrValueConstraints = tag.getNamedParameter(JCR_VALUE_CONSTRAINTS); + jcrType = tag.getNamedParameter(JCR_TYPE); + isJcrMultiple = Boolean.valueOf(tag.getNamedParameter(JCR_MULTIPLE)).booleanValue(); + } + + /* + * (non-Javadoc) + * + * @see org.apache.sling.maven.jcrocm.AbstractDescriptorElement#generate(org.apache.sling.maven.jcrocm.XMLWriter) + */ + void generate(XMLWriter xmlWriter) { + xmlWriter.printElementStart(ELEMENT_FIELD_DESCRIPTOR, true); + + super.generate(xmlWriter); + + xmlWriter.printAttribute(ID, isId); + xmlWriter.printAttribute(PATH, isPath); + xmlWriter.printAttribute(UUID, isUuid); + xmlWriter.printAttribute(CONVERTER, converter); + xmlWriter.printAttribute(JCR_DEFAULT_VALUE, jcrDefaultValue); + xmlWriter.printAttribute(JCR_VALUE_CONSTRAINTS, jcrValueConstraints); + xmlWriter.printAttribute(JCR_TYPE, jcrType); + xmlWriter.printAttribute(JCR_MULTIPLE, isJcrMultiple); + + xmlWriter.printElementStartClose(true); + } + + /* + * (non-Javadoc) + * + * @see org.apache.sling.maven.jcrocm.AbstractDescriptorElement#validate() + */ + boolean validate() { + boolean valid = true; + + if (jcrType != null) { + if (!jcrType.equals("String") && !jcrType.equals("Date") + && !jcrType.equals("Long") && !jcrType.equals("Double") + && !jcrType.equals("Boolean") && !jcrType.equals("Binary")) { + log("Invalid JCR Field Type: " + jcrType); + valid = false; + } + } + + valid &= super.validate(); + + return valid; + } +} diff --git a/src/main/java/org/apache/sling/maven/jcrocm/JcrOcmMojo.java b/src/main/java/org/apache/sling/maven/jcrocm/JcrOcmMojo.java new file mode 100644 index 0000000..523a1fd --- /dev/null +++ b/src/main/java/org/apache/sling/maven/jcrocm/JcrOcmMojo.java @@ -0,0 +1,236 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.maven.jcrocm; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.model.Resource; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; + +import com.thoughtworks.qdox.JavaDocBuilder; +import com.thoughtworks.qdox.model.DocletTag; +import com.thoughtworks.qdox.model.JavaClass; +import com.thoughtworks.qdox.model.JavaField; +import com.thoughtworks.qdox.model.JavaMethod; +import com.thoughtworks.qdox.model.JavaSource; + +/** + * The <code>JcrOcmMojo</code> implements the (default) ocm goal of the + * <em>maven-jcrocm-plugin</em>. It is by default run in the + * <code>generate-resources</code> phase and requires the compile scoped + * dependencies to be resolved. + * + * @goal ocm + * @phase generate-resources + * @description Build Jackrabbit OCM Descriptors from Java Source + * @requiresDependencyResolution compile + */ +public class JcrOcmMojo extends AbstractMojo { + + /** + * @parameter expression="${project.build.directory}/sling-generated" + * @required + * @readonly + */ + private File outputDirectory; + + /** + * The Maven project. + * + * @parameter expression="${project}" + * @required + * @readonly + */ + private MavenProject project; + + /** + * Name and path of the generated descriptor. + * + * @parameter expression="${jcrocm.descriptor.name}" default-value="mappings.xml" + */ + private String finalName; + + public void execute() throws MojoFailureException { + + boolean hasFailures = false; + + // prepare QDox and prime with the compile class path of the project + JavaDocBuilder builder = new JavaDocBuilder(); + try { + builder.getClassLibrary().addClassLoader(this.getCompileClassLoader()); + } catch (IOException ioe) { + throw new MojoFailureException("Cannot prepare QDox"); + } + + // add the sources from the project + for (Iterator i = this.project.getCompileSourceRoots().iterator(); i.hasNext();) { + try { + builder.addSourceTree(new File((String) i.next())); + } catch (OutOfMemoryError oome) { + // this may be the case for big sources and not enough VM mem + builder = null; // drop the builder to help GC now + + // fail with some explanation + throw new MojoFailureException( + "Failed analyzing source due to not enough memory, try setting Max Heap Size higher, e.g. using MAVEN_OPTS=-Xmx128m"); + } + } + + // parse the sources and get them + JavaSource[] javaSources = builder.getSources(); + List descriptors = new ArrayList(); + for (int i = 0; i < javaSources.length; i++) { + JavaClass[] javaClasses = javaSources[i].getClasses(); + for (int j = 0; javaClasses != null && j < javaClasses.length; j++) { + DocletTag tag = javaClasses[j].getTagByName(ClassDescriptor.TAG_CLASS_DESCRIPTOR); + if (tag != null) { + ClassDescriptor descriptor = this.createClassDescriptor(javaClasses[j]); + if (descriptor != null) { + descriptors.add(descriptor); + } else { + hasFailures = true; + } + } + } + } + + // after checking all classes, throw if there were any failures + if (hasFailures) { + throw new MojoFailureException("Jackrabbit OCM Descriptor parsing had failures (see log)"); + } + + // terminate if there is nothing to write + if (descriptors.isEmpty()) { + this.getLog().info("No Jackrabbit OCM Descriptors found in project"); + return; + } + + // finally the descriptors have to be written .... + if (StringUtils.isEmpty(this.finalName)) { + this.getLog().error("Descriptor file name must not be empty"); + return; + } + + // prepare the descriptor output file + File descriptorFile = new File(new File(this.outputDirectory, "SLING-INF"), this.finalName); + descriptorFile.getParentFile().mkdirs(); // ensure parent dir + + this.getLog().info("Generating " + descriptors.size() + + " OCM Mapping Descriptors to " + descriptorFile); + + // write out all the class descriptors in parse order + FileOutputStream descriptorStream = null; + XMLWriter xw = null; + try { + descriptorStream = new FileOutputStream(descriptorFile); + xw = new XMLWriter(descriptorStream, false); + xw.printElementStart("jackrabbit-ocm", false); + for (Iterator di=descriptors.iterator(); di.hasNext(); ) { + ClassDescriptor sd = (ClassDescriptor) di.next(); + sd.generate(xw); + } + xw.printElementEnd("jackrabbit-ocm"); + + } catch (IOException ioe) { + hasFailures = true; + this.getLog().error("Cannot write descriptor to " + descriptorFile, ioe); + throw new MojoFailureException("Failed to write descriptor to " + descriptorFile); + } finally { + IOUtil.close(xw); + IOUtil.close(descriptorStream); + + // remove the descriptor file in case of write failure + if (hasFailures) { + descriptorFile.delete(); + } + } + + // now add the descriptor file to the maven resources + final String ourRsrcPath = this.outputDirectory.getAbsolutePath(); + boolean found = false; + final Iterator rsrcIterator = this.project.getResources().iterator(); + while ( !found && rsrcIterator.hasNext() ) { + final Resource rsrc = (Resource)rsrcIterator.next(); + found = rsrc.getDirectory().equals(ourRsrcPath); + } + if ( !found ) { + final Resource resource = new Resource(); + resource.setDirectory(this.outputDirectory.getAbsolutePath()); + this.project.addResource(resource); + } + // and set include accordingly + this.project.getProperties().setProperty("Sling-Mappings", "SLING-INF/" + this.finalName); + } + + /** + * Creates an URL class loader containing all compile artifacts. + * + * @return The URLClassLoader for the compile artifacts. + * @throws IOException If any error occurrs creating URLs for the compile + * artifact files. + */ + private ClassLoader getCompileClassLoader() throws IOException { + List artifacts = this.project.getCompileArtifacts(); + URL[] path = new URL[artifacts.size()]; + int i = 0; + for (Iterator ai=artifacts.iterator(); ai.hasNext(); ) { + Artifact a = (Artifact) ai.next(); + path[i++] = a.getFile().toURI().toURL(); + } + return new URLClassLoader(path); + } + + private ClassDescriptor createClassDescriptor(JavaClass javaClass) { + + // create the class descriptor for the java class, return early if none + ClassDescriptor cd = ClassDescriptor.fromClass(this.getLog(), javaClass); + if (cd == null) { + return null; + } + + // analyze fields for mapping descriptors + JavaField[] fields = javaClass.getFields(); + for (int i=0; fields != null && i < fields.length; i++) { + cd.addChild(FieldDescriptor.fromField(this.getLog(), fields[i])); + cd.addChild(BeanDescriptor.fromField(this.getLog(), fields[i])); + cd.addChild(CollectionDescriptor.fromField(this.getLog(), fields[i])); + } + + // analyze methods for mapping descriptors + JavaMethod[] methods = javaClass.getMethods(); + for (int i=0; methods != null && i < methods.length; i++) { + cd.addChild(FieldDescriptor.fromMethod(this.getLog(), methods[i])); + cd.addChild(BeanDescriptor.fromMethod(this.getLog(), methods[i])); + cd.addChild(CollectionDescriptor.fromMethod(this.getLog(), methods[i])); + } + + // return nothing if validation fails + return cd.validate() ? cd : null; + } +} diff --git a/src/main/java/org/apache/sling/maven/jcrocm/XMLWriter.java b/src/main/java/org/apache/sling/maven/jcrocm/XMLWriter.java new file mode 100644 index 0000000..53e3e6c --- /dev/null +++ b/src/main/java/org/apache/sling/maven/jcrocm/XMLWriter.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.maven.jcrocm; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; + +import org.codehaus.plexus.util.StringUtils; + +public class XMLWriter extends PrintWriter { + + String indent = ""; + + XMLWriter(OutputStream descriptorStream, boolean append) throws IOException { + super(new OutputStreamWriter(descriptorStream, "UTF-8")); + + if (!append) { + println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); + } + } + + private void indent() { + indent += " "; + } + + private void outdent() { + if (indent.length() >= 2) { + indent = indent.substring(2); + } + } + + void printComment(String message) { + print (indent); + println("<!--"); + print (indent); + print (" "); + println(message); + print (indent); + println("-->"); + } + + void printElementStart(String name, boolean hasAttributes) { + print(indent); + print("<"); + print(name); + if (!hasAttributes) { + printElementStartClose(false); + } + indent(); + } + + void printElementStartClose(boolean isEmpty) { + if (isEmpty) { + print(" /"); + outdent(); + } + println('>'); + } + + void printElementEnd(String name) { + outdent(); + print(indent); + print("</"); + print(name); + println('>'); + } + + void printAttribute(String name, String value) { + if (!StringUtils.isEmpty(name) && value != null) { + println(); + print(indent); + print(' '); + print(name); + print("=\""); + print(value); + print('"'); + } + } + + void printAttribute(String name, boolean value) { + if (value) { + printAttribute(name, "true"); + } + } +} diff --git a/src/main/resources/META-INF/LICENSE b/src/main/resources/META-INF/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/src/main/resources/META-INF/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/src/main/resources/META-INF/NOTICE b/src/main/resources/META-INF/NOTICE new file mode 100644 index 0000000..03044cc --- /dev/null +++ b/src/main/resources/META-INF/NOTICE @@ -0,0 +1,25 @@ +Apache Sling Maven JCROCM Plugin +Copyright 2008-2010 The Apache Software Foundation + +Apache Sling is based on source code originally developed +by Day Software (http://www.day.com/). + + +I. Included Software + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). +Licensed under the Apache License 2.0. + + +II. Used Software + +This product uses software developed at +The Codehaus (http://www.codehaus.org) +Licensed under the Apache License 2.0. + + +III. License Summary +- Apache License 2.0 + + -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
