You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2009/12/09 16:01:20 UTC
svn commit: r888822 - in /labs/magma/trunk/foundation-website/src/main:
java/org/apache/magma/website/htmlpieces/ resources/META-INF/
Author: simoneg
Date: Wed Dec 9 15:01:19 2009
New Revision: 888822
URL: http://svn.apache.org/viewvc?rev=888822&view=rev
Log:
LABS-352: html pieces
Added:
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/AddHtmlPiecesOnPropertyInfo.aj
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/BaseOutputPiece.java
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlFormPiece.java
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlOutputPiece.java
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlPieces.java
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/ParsingHtmlFormPiece.java
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/PropertyAwareHtmlPiece.java
labs/magma/trunk/foundation-website/src/main/resources/META-INF/
labs/magma/trunk/foundation-website/src/main/resources/META-INF/magma.default.properties
Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/AddHtmlPiecesOnPropertyInfo.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/AddHtmlPiecesOnPropertyInfo.aj?rev=888822&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/AddHtmlPiecesOnPropertyInfo.aj (added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/AddHtmlPiecesOnPropertyInfo.aj Wed Dec 9 15:01:19 2009
@@ -0,0 +1,42 @@
+package org.apache.magma.website.htmlpieces;
+
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Method;
+
+import org.apache.magma.beans.PropertyInfo;
+
+public aspect AddHtmlPiecesOnPropertyInfo {
+
+ private HtmlOutputPiece PropertyInfo.outputPiece = null;
+ private HtmlFormPiece PropertyInfo.formPiece = null;
+
+ /**
+ * @return the output html piece assigned to this {@link PropertyInfo}.
+ */
+ public HtmlOutputPiece PropertyInfo.getHtmlOutputPiece() {
+ return this.outputPiece;
+ }
+
+ public HtmlFormPiece PropertyInfo.getHtmlFormPiece() {
+ return this.formPiece;
+ }
+
+ /**
+ * Parses annotations on a property getter to assign proper html pieces
+ * @param info The property info being created
+ * @param desc The property descriptor being used
+ */
+ after(PropertyInfo info, PropertyDescriptor desc) :
+ execution(* PropertyInfo.init(PropertyDescriptor, Class)) && target(info) && args(desc,..) {
+ if (info.getType() == null) return;
+
+ Method readMethod = desc.getReadMethod();
+ if (readMethod == null) return;
+
+ info.outputPiece = HtmlPieces.outputFor(info);
+ info.formPiece = HtmlPieces.formFor(info);
+ }
+
+
+
+}
Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/BaseOutputPiece.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/BaseOutputPiece.java?rev=888822&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/BaseOutputPiece.java (added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/BaseOutputPiece.java Wed Dec 9 15:01:19 2009
@@ -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.magma.website.htmlpieces;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.magma.basics.MagmaException;
+import org.apache.magma.beans.PropertyInfo;
+import org.apache.magma.conversion.Converter;
+import org.apache.magma.conversion.Converters;
+import org.apache.magma.i18n.Formatter;
+import org.apache.magma.i18n.Formatters;
+import org.apache.magma.website.Head;
+
+public class BaseOutputPiece implements HtmlOutputPiece<Object>, PropertyAwareHtmlPiece {
+
+ public boolean outputs(Class<?> clazz) {
+ return true;
+ }
+
+ public HtmlOutputPiece<Object> createFor(Class<? extends Object> clazz) {
+ return new BaseOutputPiece();
+ }
+
+ public void head(Object value, Head head) {
+ }
+
+ public String output(Object value) {
+ String ret = defaultConversion(value);
+ if (ret == null) return "";
+ return StringEscapeUtils.escapeHtml(ret);
+ }
+
+ protected String defaultConversion(Object value) {
+ PropertyInfo property = getProperty();
+ String ret = null;
+ if (property == null) {
+ if (value == null) return "";
+ Formatter formatter = Formatters.getFormatterFor(value.getClass());
+ if (formatter != null) {
+ ret = formatter.to(value);
+ }
+ Converter converter = Converters.getConverterFor(value.getClass());
+ if (converter != null) {
+ ret = converter.to(value);
+ }
+ ret = value.toString();
+ }
+ ret = property.toUser(value);
+ return ret;
+ }
+
+}
Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlFormPiece.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlFormPiece.java?rev=888822&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlFormPiece.java (added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlFormPiece.java Wed Dec 9 15:01:19 2009
@@ -0,0 +1,87 @@
+/*
+ * 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.magma.website.htmlpieces;
+
+import java.io.Writer;
+import java.util.Map;
+
+import org.apache.commons.beanutils.Converter;
+import org.apache.magma.i18n.Formatter;
+
+/**
+ * Outputs a form input for a specific type.
+ *
+ * Classes implementing this interface are responsible for
+ * creating a form input suitable for the given type.
+ *
+ * They can optionally, but most of them should, implement
+ * {@link PropertyAwareHtmlPiece} to obtain access
+ * to the property they are connected to. This is useful
+ * for proper formatting of values, and can provide
+ * access to informations about the property.
+ *
+ * Also those implementing {@link PropertyAwareHtmlPiece}
+ * should anyway work when a property is not given.
+ *
+ * By default, the value of the input is expected in an input
+ * field named with the given "id", and in a format suitable
+ * for being parsed by the {@link Formatter} or {@link Converter}
+ * assigne dto the property, or used by default for the given class.
+ *
+ * If that is not the case, {@link ParsingHtmlFormPiece} can be implemented,
+ * customizing the parsing part.
+ *
+ * @author Simone Gianni <si...@apache.org>
+ * @param <T> The type this class handles.
+ */
+public interface HtmlFormPiece<T> {
+
+ /**
+ * Checks whether this instance is able to produce
+ * HTML form inputs for the given type.
+ * @param clazz The type we are checking against
+ * @return true if this instance is able to output the given type
+ */
+ public boolean handles(Class<?> clazz);
+
+ /**
+ * Creates an instance.
+ *
+ * Depending on the specific implementation, different
+ * instances could be returned for different types.
+ *
+ * @param clazz The type that needs to be generated
+ * @return an instance, eventually this instance
+ */
+ public HtmlFormPiece<T> createInputFor(Class<? extends T> clazz);
+
+ /**
+ * Adds to the head scripts or css files.
+ * @param value The value we need to create input for
+ * @param id The id of the form field
+ * @param head The head object to add scripts and CSS
+ */
+ public void formHead(T value, String id);
+
+ /**
+ * Creates a form input.
+ * @param value The value we need to create input for
+ * @param id The id of the field
+ */
+ public String createFormInput(T value, String id);
+
+}
Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlOutputPiece.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlOutputPiece.java?rev=888822&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlOutputPiece.java (added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlOutputPiece.java Wed Dec 9 15:01:19 2009
@@ -0,0 +1,63 @@
+/*
+ * 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.magma.website.htmlpieces;
+
+import java.io.Writer;
+
+import org.apache.magma.website.Head;
+
+/**
+ * Creates the HTML output for a specific type.
+ *
+ * @author Simone Gianni <si...@apache.org>
+ * @param <T> The type this piece handles
+ */
+public interface HtmlOutputPiece<T> {
+
+ /**
+ * Checks whether this instance is able to produce
+ * HTML output for the given type.
+ * @param clazz The type we are checking against
+ * @return true if this instance is able to output the given type
+ */
+ public boolean outputs(Class<?> clazz);
+
+ /**
+ * Creates an instance.
+ *
+ * Depending on the specific implementation, different
+ * instances could be returned for different types.
+ *
+ * @param clazz The type that needs to be formatted in HTML
+ * @return an instance, eventually this instance
+ */
+ public HtmlOutputPiece<T> createFor(Class<? extends T> clazz);
+
+ /**
+ * Adds to the head scripts or css files.
+ * @param value The value we'll need to output
+ * @param head The head object to add scripts and CSS
+ */
+ public void head(T value, Head head);
+
+ /**
+ * Creates the HTML to output the value.
+ * @param value The value to output
+ */
+ public String output(T value);
+
+}
Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlPieces.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlPieces.java?rev=888822&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlPieces.java (added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/HtmlPieces.java Wed Dec 9 15:01:19 2009
@@ -0,0 +1,62 @@
+/*
+ * 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.magma.website.htmlpieces;
+
+import java.util.Iterator;
+
+import org.apache.magma.basics.services.ServiceFinder;
+import org.apache.magma.beans.PropertyInfo;
+import org.apache.magma.conversion.Converter;
+
+public class HtmlPieces {
+
+ public static HtmlOutputPiece outputFor(PropertyInfo prop) {
+ HtmlOutputPiece piece = outputFor(prop.getType());
+ if (piece instanceof PropertyAwareHtmlPiece) {
+ ((PropertyAwareHtmlPiece)piece).setProperty(prop);
+ }
+ return piece;
+ }
+
+ public static HtmlFormPiece formFor(PropertyInfo prop) {
+ HtmlFormPiece piece = formFor(prop.getType());
+ if (piece instanceof PropertyAwareHtmlPiece) {
+ ((PropertyAwareHtmlPiece)piece).setProperty(prop);
+ }
+ return piece;
+ }
+
+ public static <T> HtmlOutputPiece<T> outputFor(Class<T> clazz) {
+ Iterator<HtmlOutputPiece> providers = ServiceFinder.findProviders(HtmlOutputPiece.class);
+ while (providers.hasNext()) {
+ HtmlOutputPiece piece = providers.next();
+ if (piece.outputs(clazz)) return piece.createFor(clazz);
+ }
+ return null;
+ }
+
+ public static <T> HtmlFormPiece<T> formFor(Class<T> clazz) {
+ Iterator<HtmlFormPiece> providers = ServiceFinder.findProviders(HtmlFormPiece.class);
+ while (providers.hasNext()) {
+ HtmlFormPiece piece = providers.next();
+ if (piece.handles(clazz)) return piece.createInputFor(clazz);
+ }
+ return null;
+ }
+
+
+}
Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/ParsingHtmlFormPiece.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/ParsingHtmlFormPiece.java?rev=888822&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/ParsingHtmlFormPiece.java (added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/ParsingHtmlFormPiece.java Wed Dec 9 15:01:19 2009
@@ -0,0 +1,18 @@
+package org.apache.magma.website.htmlpieces;
+
+import java.util.Map;
+
+public interface ParsingHtmlFormPiece<T> extends HtmlFormPiece<T> {
+
+ /**
+ * Parses parameters to reconstruct the right value.
+ *
+ * @param binding A map of request parameters, only needed ones will be used
+ * @param id The id of the field
+ * @param previous The previous value, if any, or null
+ * @return A new value, or the given previous value modified
+ */
+ public T parse(Map<String, String> binding, String id, T previous);
+
+
+}
Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/PropertyAwareHtmlPiece.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/PropertyAwareHtmlPiece.java?rev=888822&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/PropertyAwareHtmlPiece.java (added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/htmlpieces/PropertyAwareHtmlPiece.java Wed Dec 9 15:01:19 2009
@@ -0,0 +1,22 @@
+package org.apache.magma.website.htmlpieces;
+
+import org.apache.magma.beans.PropertyInfo;
+
+public interface PropertyAwareHtmlPiece {
+
+ public PropertyInfo getProperty();
+ public void setProperty(PropertyInfo prop);
+
+ static aspect PropertyAwareHtmlPieceImpl {
+ private PropertyInfo PropertyAwareHtmlPiece.property;
+
+ public PropertyInfo PropertyAwareHtmlPiece.getProperty() {
+ return this.property;
+ }
+ public void PropertyAwareHtmlPiece.setProperty(PropertyInfo prop) {
+ this.property = prop;
+ }
+
+ }
+
+}
Added: labs/magma/trunk/foundation-website/src/main/resources/META-INF/magma.default.properties
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/resources/META-INF/magma.default.properties?rev=888822&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/resources/META-INF/magma.default.properties (added)
+++ labs/magma/trunk/foundation-website/src/main/resources/META-INF/magma.default.properties Wed Dec 9 15:01:19 2009
@@ -0,0 +1,15 @@
+#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.
+org.apache.magma.website.htmlpieces.HtmlOutputPiece.~~default=org.apache.magma.website.htmlpieces.BaseOutputPiece
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org