Added:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PropertiesCommand.java
URL:
http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PropertiesCommand.java?rev=1035848&view=auto
==============================================================================
---
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PropertiesCommand.java
(added)
+++
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PropertiesCommand.java
Tue Nov 16 22:35:40 2010
@@ -0,0 +1,74 @@
+/*
+ * 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.openjpa.persistence.jest;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.HttpURLConnection;
+import java.util.Arrays;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletResponse;
+
+
+/**
+ * @author Pinaki Poddar
+ *
+ */
+public class PropertiesCommand extends AbstractCommand {
+
+ @Override
+ public String getAction() {
+ return "properties";
+ }
+
+ /* (non-Javadoc)
+ * @see
org.apache.openjpa.persistence.jest.JESTCommand#process(org.apache.openjpa.persistence.jest.JPAServletContext)
+ */
+ @Override
+ public void process(JPAServletContext ctx) throws ErrorFormatter,
IOException {
+ HttpServletResponse response = ctx.getResponse();
+ response.setContentType("text/html");
+ PrintWriter writer = response.getWriter();
+ response.setStatus(HttpURLConnection.HTTP_OK);
+ Map<String,Object> properties =
ctx.getPersistenceContext().getProperties();
+ writer.println("<html><body>");
+ writer.println("Configuration Properties for Persistent Unit [" +
ctx.getPersistenceUnitName() + "]<p>");
+ HTMLTable table = new HTMLTable();
+ for (Map.Entry<String,Object> entry : properties.entrySet()) {
+ if (entry.getKey().indexOf('.') == -1) continue;
+ Object value = entry.getValue();
+ String v = "";
+ if (value != null) {
+ if (value.getClass().isArray()) {
+ v = Arrays.toString((Object[])value);
+ } else {
+ v = value.toString();
+ }
+ }
+ HTMLTable.Row row = table.newRow();
+ row.setColumn(0, entry.getKey());
+ row.setColumn(1, v);
+ }
+ table.write(writer);
+ writer.println("</body></html>");
+ }
+
+}
Propchange:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PropertiesCommand.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PropertiesCommand.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PrototypeFactory.java
URL:
http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PrototypeFactory.java?rev=1035848&view=auto
==============================================================================
---
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PrototypeFactory.java
(added)
+++
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PrototypeFactory.java
Tue Nov 16 22:35:40 2010
@@ -0,0 +1,74 @@
+/*
+ * 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.openjpa.persistence.jest;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+/**
+ * A factory for a specific type of objects registered by a key.
+ * The client registers a type indexed by name. The type must have a
no-argument constructor.
+ * The client can get a new instance of the registered type.
+ *
+ * @author Pinaki Poddar
+ *
+ */
+public class PrototypeFactory<T> {
+ private Map<String, Class<? extends T>> _registry = new TreeMap<String,
Class<? extends T>>();
+ private Class<? extends T> _default;
+
+ /**
+ * Register
+ * @param key
+ * @param prototype
+ */
+ public void register(String key, Class<? extends T> prototype) {
+ _registry.put(key, prototype);
+ }
+
+ public T newInstance(String name) {
+ return _registry.containsKey(name) ? newInstance(_registry.get(name))
: getDefault();
+ }
+
+ public void setDefault(Class<? extends T> def) {
+ _default = def;
+ }
+
+ public Set<String> getRegisteredKeys() {
+ return Collections.unmodifiableSet(_registry.keySet());
+ }
+
+ private T getDefault() {
+ if (_default != null)
+ return newInstance(_default);
+ else
+ throw new RuntimeException();
+ }
+
+ private T newInstance(Class<? extends T> type) {
+ try {
+ return type.newInstance();
+ } catch (Exception e) {
+ throw new RuntimeException();
+ }
+ }
+}
Propchange:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PrototypeFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/PrototypeFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/QueryCommand.java
URL:
http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/QueryCommand.java?rev=1035848&view=auto
==============================================================================
---
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/QueryCommand.java
(added)
+++
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/QueryCommand.java
Tue Nov 16 22:35:40 2010
@@ -0,0 +1,81 @@
+/*
+ * 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.openjpa.persistence.jest;
+
+import java.net.HttpURLConnection;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+/**
+ * @author Pinaki Poddar
+ *
+ */
+public class QueryCommand extends AbstractCommand {
+ private static final List<String> _mandatoryArgs = Arrays.asList("q");
+ private static final List<String> _validQualifiers =
Arrays.asList("format", "plan", "named", "single",
+ "first", "max");
+
+ public String getAction() {
+ return "query";
+ }
+
+ @Override
+ protected Collection<String> getMandatoryArguments() {
+ return _mandatoryArgs;
+ }
+
+ @Override
+ protected int getMinimumArguments() {
+ return 0;
+ }
+
+ protected Collection<String> getValidQualifiers() {
+ return _validQualifiers;
+ }
+
+ @Override
+ public void process(JPAServletContext ctx) throws ErrorFormatter {
+ String spec = getMandatoryArgument("q");
+ try {
+ EntityManager em = ctx.getPersistenceContext();
+ Query query = isBooleanQualifier("named") ?
em.createNamedQuery(spec) : em.createQuery(spec);
+ if (hasQualifier("first"))
+ query.setFirstResult(Integer.parseInt(getQualifier("first")));
+ if (hasQualifier("max"))
+ query.setMaxResults(Integer.parseInt(getQualifier("max")));
+
+ Map<String, String> args = getArguments();
+ for (Map.Entry<String, String> entry : args.entrySet()) {
+ query.setParameter(entry.getKey(), entry.getValue());
+ }
+ ctx.getObjectFormatter(this)
+ .encode(ctx.toStateManager(isBooleanQualifier("single")
+ ? Collections.singleton(query.getSingleResult()) :
query.getResultList()), ctx);
+ } catch (Exception e) {
+ throw new ErrorFormatter(e, _loc.get("query-execution-error",
spec));
+ }
+ }
+}
Propchange:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/QueryCommand.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/QueryCommand.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/ResourceCommand.java
URL:
http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/ResourceCommand.java?rev=1035848&view=auto
==============================================================================
---
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/ResourceCommand.java
(added)
+++
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/ResourceCommand.java
Tue Nov 16 22:35:40 2010
@@ -0,0 +1,95 @@
+/*
+ * 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.openjpa.persistence.jest;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+
+/**
+ * A <em>default</em> command that interprets the URI as a resource.
+ * The context root is implicitly resolved to a resource named
<code>index.html</code>
+ *
+ * @author Pinaki Poddar
+ *
+ */
+public class ResourceCommand extends AbstractCommand {
+ private String _resource;
+ private static final String CONTEXT_ROOT = "/";
+ private static final String ROOT_RESOURCE = "/index.html";
+ private static final String RESOURCE_PATH =
ResourceCommand.class.getPackage().getName().replace('.', '/');
+
+ public String getAction() {
+ return "";
+ }
+
+ public void parse(HttpServletRequest request) {
+ super.parse(request);
+ String path = request.getServletPath();
+ if (CONTEXT_ROOT.equals(path))
+ path = ROOT_RESOURCE;
+ _resource = RESOURCE_PATH + path;
+ }
+
+ @Override
+ public void validate(HttpServletRequest request) {
+
+ }
+
+
+ public void process(JPAServletContext ctx) throws ErrorFormatter {
+ HttpServletRequest request = ctx.getRequest();
+ HttpServletResponse response = ctx.getResponse();
+ response.setHeader("Cache-Control", "public");
+ ClassLoader loader = getClass().getClassLoader();
+ InputStream in = loader.getResourceAsStream(_resource);
+ if (in == null) {
+ in =
request.getSession().getServletContext().getResourceAsStream(_resource);
+ if (in == null) {
+ throw new ErrorFormatter(_loc.get("resource-not-found",
_resource), HttpURLConnection.HTTP_NOT_FOUND);
+ }
+ }
+ try {
+ String mimeType =
request.getSession().getServletContext().getMimeType(_resource);
+ response.setContentType(mimeType);
+ OutputStream out = response.getOutputStream();
+ if (mimeType.startsWith("image/")) {
+ byte[] b = new byte[1024];
+ int i = 0;
+ for (int l = 0; (l = in.read(b)) != -1;) {
+ out.write(b, 0, l);
+ i += l;
+ }
+ response.setContentLength(i);
+ } else {
+ for (int c = 0; (c = in.read()) != -1;) {
+ out.write((char)c);
+ }
+ }
+ } catch (IOException e) {
+ throw new ErrorFormatter(e);
+ }
+ }
+}
Propchange:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/ResourceCommand.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/ResourceCommand.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/XMLFormatter.java
URL:
http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/XMLFormatter.java?rev=1035848&view=auto
==============================================================================
---
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/XMLFormatter.java
(added)
+++
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/XMLFormatter.java
Tue Nov 16 22:35:40 2010
@@ -0,0 +1,486 @@
+/*
+ * 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.openjpa.persistence.jest;
+
+import java.io.BufferedReader;
+import java.io.CharArrayWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.HttpURLConnection;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.persistence.metamodel.Attribute;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.openjpa.kernel.OpenJPAStateManager;
+import org.apache.openjpa.kernel.StoreContext;
+import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.meta.FieldMetaData;
+import org.apache.openjpa.meta.JavaTypes;
+import org.apache.openjpa.meta.ValueMetaData;
+import org.apache.openjpa.persistence.meta.Members;
+import org.apache.openjpa.util.InternalException;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Marshals a root instance and its persistent closure as an XML element.
+ * The closure is resolved against the persistence context that contains the
root instance.
+ * The XML document adheres to the <code>jest-instance.xsd</code> schema.
+ *
+ * @author Pinaki Poddar
+ *
+ */
+public class XMLFormatter implements ObjectFormatter {
+ public static final String MIME_TYPE = "text/xml";
+ /**
+ * The element/attribute tags declared in <code>jest-instance.xsd</code>
XML schema.
+ */
+ public static final String ELEMENT_NULL_REF = "null";
+ public static final String ELEMENT_ROOT = "instances";
+ public static final String ELEMENT_INSTANCE = "instance";
+ public static final String ELEMENT_REF = "ref";
+ public static final String ELEMENT_BASIC = "basic";
+ public static final String ELEMENT_LOB = "lob";
+ public static final String ELEMENT_SINGULAR = "singular";
+ public static final String ELEMENT_COLLECTION = "collection";
+ public static final String ELEMENT_MAP = "map";
+ public static final String ELEMENT_MEMBER = "member";
+ public static final String ELEMENT_ENTRY = "entry";
+ public static final String ELEMENT_ENTRY_KEY = "key";
+ public static final String ELEMENT_ENTRY_VALUE = "value";
+
+ public static final String ATTR_TYPE = "type";
+ public static final String ATTR_ID = "id";
+ public static final String ATTR_NAME = "name";
+ public static final String ATTR_NULL = "null";
+ public static final String ATTR_MEMBER_TYPE = "member-type";
+ public static final String ATTR_KEY_TYPE = "key-type";
+ public static final String ATTR_VALUE_TYPE = "value-type";
+
+ public static final InputStream _xsd;
+ static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";
+ static final String W3C_XML_SCHEMA_INSTANCE =
"http://www.w3.org/2001/XMLSchema-instance";
+ static final String JAXP_SCHEMA_SOURCE =
"http://java.sun.com/xml/jaxp/properties/schemaSource";
+ static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
+ static final String JEST_INSTANCE_XSD = "jest-instance.xsd";
+ static final String JEST_INSTANCE_XSD_PATH = "META-INF/" +
JEST_INSTANCE_XSD;
+
+ private static final DocumentBuilder _builder;
+ private static final Transformer _transformer;
+
+ static {
+ try {
+ _builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ _transformer = TransformerFactory.newInstance().newTransformer();
+ _xsd =
Thread.currentThread().getContextClassLoader().getResourceAsStream(JEST_INSTANCE_XSD_PATH);
+ _transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+ _transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ _transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
+ _transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+
_transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
"2");
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+ /**
+ * Gets the content type of the response.
+ */
+ public String getMimeType() {
+ return MIME_TYPE;
+ }
+
+ /* (non-Javadoc)
+ * @see
org.apache.openjpa.persistence.jest.ObjectFormatter#encode(java.lang.Object,
+ * javax.servlet.ServletResponse,
org.apache.openjpa.persistence.jest.JPAServletContext)
+ */
+ @Override
+ public void encode(Object obj, JPAServletContext ctx) {
+ if (obj instanceof OpenJPAStateManager) {
+ Document doc = encodeManagedInstance((OpenJPAStateManager)obj,
ctx);
+ try {
+ _transformer.transform(new DOMSource(doc), new
StreamResult(ctx.getResponse().getWriter()));
+ } catch (Exception ex) {
+ throw new ErrorFormatter(ex);
+ }
+ } else {
+ throw new IllegalArgumentException(obj + " is not a managed
instance");
+ }
+ }
+
+ @Override
+ public void encode(Collection<?> objs, JPAServletContext ctx) throws
IOException {
+ try {
+ Document doc =
encodeManagedInstances((Collection<OpenJPAStateManager>)objs, ctx);
+ _transformer.transform(new DOMSource(doc), new
StreamResult(ctx.getResponse().getWriter()));
+ } catch (Exception e) {
+ throw new IllegalArgumentException(objs + " is not a managed
instance");
+ }
+
+ }
+
+ /**
+ * Encodes a single managed instance in to a XML document according to
JEST XML Schema.
+ *
+ */
+ protected Document encodeManagedInstance(final OpenJPAStateManager sm,
JPAServletContext jtx) {
+ return encodeManagedInstances(Collections.singleton(sm), jtx);
+ }
+
+ /**
+ * Encodes the given managed instance into a new XML element as a child of
the given parent node.
+ *
+ * @param sm a managed instance, can be null.
+ * @param parent the parent node to which the new node be attached.
+ */
+ protected Element encodeManagedInstance(final OpenJPAStateManager sm,
Element parent, JPAServletContext jtx) {
+ return encodeManagedInstance(sm, parent, new
HashSet<OpenJPAStateManager>(), jtx);
+ }
+
+ /**
+ * Encodes the given collection of managed instance into a new XML
document according to JEST XML Schema.
+ *
+ * @param sm a managed instance, can be null.
+ * @param parent the parent node to which the new node be attached.
+ */
+ protected Document encodeManagedInstances(final
Collection<OpenJPAStateManager> sms, JPAServletContext jtx) {
+ Document doc = _builder.newDocument();
+ Element root = doc.createElement(ELEMENT_ROOT);
+ doc.appendChild(root);
+ if (sms != null) {
+ for (OpenJPAStateManager sm : sms) {
+ encodeManagedInstance(sm, root, new
HashSet<OpenJPAStateManager>(), jtx);
+ }
+ }
+ addSchemaToRoot(doc);
+ return doc;
+ }
+
+
+ /**
+ * Encodes the closure of a persistent instance into a XML element.
+ *
+ * @param sm the managed instance to be encoded. Can be null.
+ * @param parent the parent XML element to which the new XML element be
added. Must not be null. Must be
+ * owned by a document.
+ * @param visited the persistent instances that had been encoded already.
Must not be null or immutable.
+ *
+ * @return the new element. The element has been appended as a child to
the given parent in this method.
+ */
+ private Element encodeManagedInstance(final OpenJPAStateManager sm, final
Element parent,
+ final Set<OpenJPAStateManager> visited, JPAServletContext jtx) {
+ if (parent == null)
+ throw new InternalException(_loc.get("format-xml-null-parent"));
+ Document doc = parent.getOwnerDocument();
+ if (doc == null)
+ throw new InternalException(_loc.get("format-xml-null-doc"));
+ if (visited == null)
+ throw new InternalException(_loc.get("format-xml-null-closure"));
+
+ if (sm == null) {
+ Element nullRef = doc.createElement(ELEMENT_NULL_REF);
+ parent.appendChild(nullRef);
+ return nullRef;
+ }
+ boolean ref = !visited.add(sm);
+ Element root = doc.createElement(ref ? ELEMENT_REF : ELEMENT_INSTANCE);
+ parent.appendChild(root);
+ root.setAttribute(ATTR_ID, ior(sm));
+ if (ref)
+ return root;
+
+ Element child = null;
+ BitSet loaded = sm.getLoaded();
+ StoreContext ctx = (StoreContext)sm.getGenericContext();
+ List<Attribute<?, ?>> attrs =
MetamodelHelper.getAttributesInOrder(sm.getMetaData(),
+ jtx.getPersistenceContext().getMetamodel());
+ for (int i = 0; i < attrs.size(); child = null, i++) {
+ FieldMetaData fmd = ((Members.Member<?, ?>) attrs.get(i)).fmd;
+ if (!loaded.get(fmd.getIndex()))
+ continue;
+ Object value = sm.fetch(fmd.getIndex());
+ switch (fmd.getDeclaredTypeCode()) {
+ case JavaTypes.BOOLEAN:
+ case JavaTypes.BYTE:
+ case JavaTypes.CHAR:
+ case JavaTypes.DOUBLE:
+ case JavaTypes.FLOAT:
+ case JavaTypes.INT:
+ case JavaTypes.LONG:
+ case JavaTypes.SHORT:
+
+ case JavaTypes.BOOLEAN_OBJ:
+ case JavaTypes.BYTE_OBJ:
+ case JavaTypes.CHAR_OBJ:
+ case JavaTypes.DOUBLE_OBJ:
+ case JavaTypes.FLOAT_OBJ:
+ case JavaTypes.INT_OBJ:
+ case JavaTypes.LONG_OBJ:
+ case JavaTypes.SHORT_OBJ:
+
+ case JavaTypes.BIGDECIMAL:
+ case JavaTypes.BIGINTEGER:
+ case JavaTypes.DATE:
+ case JavaTypes.NUMBER:
+ case JavaTypes.CALENDAR:
+ case JavaTypes.LOCALE:
+ case JavaTypes.STRING:
+ case JavaTypes.ENUM:
+ child = doc.createElement(ELEMENT_BASIC);
+ child.setAttribute(ATTR_NAME, fmd.getName());
+ if (value == null) {
+ encodeNull(child);
+ } else {
+ encodeBasic(child, value, fmd.getDeclaredType());
+ }
+ break;
+
+ case JavaTypes.OID:
+ child = doc.createElement(ELEMENT_REF);
+ child.setAttribute(ATTR_NAME, fmd.getName());
+ if (value == null) {
+ encodeNull(child);
+ } else {
+ encodeBasic(child, value, fmd.getDeclaredType());
+ }
+ break;
+
+ case JavaTypes.PC:
+ child = doc.createElement(ELEMENT_SINGULAR);
+ child.setAttribute(ATTR_NAME, fmd.getName());
+ child.setAttribute(ATTR_TYPE, typeOf(fmd));
+ OpenJPAStateManager other = ctx.getStateManager(value);
+ encodeManagedInstance(other, child, visited, jtx);
+ break;
+
+ case JavaTypes.ARRAY:
+ Object[] values = (Object[])value;
+ value = Arrays.asList(values);
+ // no break;
+ case JavaTypes.COLLECTION:
+ child = doc.createElement(ELEMENT_COLLECTION);
+ child.setAttribute(ATTR_NAME, fmd.getName());
+ child.setAttribute(ATTR_TYPE, typeOf(fmd));
+ child.setAttribute(ATTR_MEMBER_TYPE,
typeOf(fmd.getElement().getDeclaredType()));
+ if (value == null) {
+ encodeNull(child);
+ break;
+ }
+ Collection<?> members = (Collection<?>)value;
+ boolean basic = fmd.getElement().getTypeMetaData() == null;
+ for (Object o : members) {
+ Element member = doc.createElement(ELEMENT_MEMBER);
+ child.appendChild(member);
+ if (o == null) {
+ encodeNull(member);
+ } else {
+ if (basic) {
+ encodeBasic(member, o, o.getClass());
+ } else {
+ encodeManagedInstance(ctx.getStateManager(o),
member, visited, jtx);
+ }
+ }
+ }
+ break;
+ case JavaTypes.MAP:
+ child = doc.createElement(ELEMENT_MAP);
+ child.setAttribute(ATTR_NAME, fmd.getName());
+ child.setAttribute(ATTR_TYPE, typeOf(fmd));
+ child.setAttribute(ATTR_KEY_TYPE,
typeOf(fmd.getElement().getDeclaredType()));
+ child.setAttribute(ATTR_VALUE_TYPE,
typeOf(fmd.getValue().getDeclaredType()));
+ if (value == null) {
+ encodeNull(child);
+ break;
+ }
+ Set<Map.Entry> entries = ((Map)value).entrySet();
+ boolean basicKey = fmd.getElement().getTypeMetaData() ==
null;
+ boolean basicValue = fmd.getValue().getTypeMetaData() ==
null;
+ for (Map.Entry<?,?> e : entries) {
+ Element entry = doc.createElement(ELEMENT_ENTRY);
+ Element entryKey =
doc.createElement(ELEMENT_ENTRY_KEY);
+ Element entryValue =
doc.createElement(ELEMENT_ENTRY_VALUE);
+ entry.appendChild(entryKey);
+ entry.appendChild(entryValue);
+ child.appendChild(entry);
+ if (e.getKey() == null) {
+ encodeNull(entryKey);
+ } else {
+ if (basicKey) {
+ encodeBasic(entryKey, e.getKey(),
e.getKey().getClass());
+ } else {
+
encodeManagedInstance(ctx.getStateManager(e.getKey()), entryKey, visited, jtx);
+ }
+ }
+ if (e.getValue() == null) {
+ encodeNull(entryValue);
+ } else {
+ if (basicValue) {
+ encodeBasic(entryValue, e.getValue(),
e.getValue().getClass());
+ } else {
+
encodeManagedInstance(ctx.getStateManager(e.getValue()), entryValue, visited,
jtx);
+ }
+ }
+ }
+ break;
+
+ case JavaTypes.INPUT_STREAM:
+ case JavaTypes.INPUT_READER:
+ child = doc.createElement(ELEMENT_LOB);
+ child.setAttribute(ATTR_NAME, fmd.getName());
+ child.setAttribute(ATTR_TYPE, typeOf(fmd));
+ if (value == null) {
+ encodeNull(child);
+ } else {
+ CDATASection data =
doc.createCDATASection(streamToString(value));
+ child.appendChild(data);
+ }
+ break;
+
+ case JavaTypes.PC_UNTYPED:
+ case JavaTypes.OBJECT:
+ System.err.println("Not handled " + fmd.getName() + " of
type " + fmd.getDeclaredType());
+ }
+
+ if (child != null) {
+ root.appendChild(child);
+ }
+ }
+ return root;
+ }
+
+ /**
+ * Sets the given value element as null. The <code>null</code> attribute
is set to true.
+ *
+ * @param element the XML element to be set
+ */
+ void encodeNull(Element element) {
+ element.setAttribute(ATTR_NULL, "true");
+ }
+
+
+ /**
+ * Sets the given value element. The <code>type</code> is set to the given
runtime type.
+ * String form of the given object is set as the text content.
+ *
+ * @param element the XML element to be set
+ * @param obj value of the element. Never null.
+ */
+ private void encodeBasic(Element element, Object obj, Class<?>
runtimeType) {
+ element.setAttribute(ATTR_TYPE, typeOf(runtimeType));
+ element.setTextContent(obj.toString());
+ }
+
+
+
+ /**
+ * Convert the given stream (either an InutStream or a Reader) to a String
+ * to be included in CDATA section of a XML document.
+ *
+ * @param value the field value to be converted. Can not be null
+ * @return
+ */
+ String streamToString(Object value) {
+ Reader reader = null;
+ if (value instanceof InputStream) {
+ reader = new BufferedReader(new
InputStreamReader((InputStream)value));
+ } else if (value instanceof Reader) {
+ reader = (Reader)value;
+ } else {
+ throw new RuntimeException();
+ }
+ CharArrayWriter writer = new CharArrayWriter();
+ try {
+ for (int c; (c = reader.read()) != -1;) {
+ writer.write(c);
+ }
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
+ }
+ return writer.toString();
+ }
+
+ void addSchemaToRoot(Document doc) {
+ Element root = doc.getDocumentElement();
+ String[] nvpairs = new String[] {
+ "xmlns:xsi", W3C_XML_SCHEMA_INSTANCE,
+ "xsi:noNamespaceSchemaLocation", JEST_INSTANCE_XSD,
+ "version", "1.0",
+ };
+ for (int i = 0; i < nvpairs.length; i += 2) {
+ root.setAttribute(nvpairs[i], nvpairs[i+1]);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see
org.apache.openjpa.persistence.jest.ObjectFormatter#configure(java.lang.String,
java.lang.Object)
+ */
+ @Override
+ public void configure(String key, Object value) {
+ // TODO Auto-generated method stub
+
+ }
+
+
+ String ior(OpenJPAStateManager sm) {
+ return typeOf(sm)+"-"+sm.getObjectId().toString();
+ }
+
+ String typeOf(OpenJPAStateManager sm) {
+ return sm.getMetaData().getDescribedType().getSimpleName();
+ }
+
+ String typeOf(Class<?> cls) {
+ return cls.getSimpleName();
+ }
+
+ String typeOf(ClassMetaData meta) {
+ return meta.getDescribedType().getSimpleName();
+ }
+
+ String typeOf(ValueMetaData vm) {
+ if (vm.getTypeMetaData() == null)
+ return typeOf(vm.getType());
+ return typeOf(vm.getTypeMetaData());
+ }
+
+ String typeOf(FieldMetaData fmd) {
+ return fmd.getType().getSimpleName();
+ }
+
+}
Propchange:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/XMLFormatter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
openjpa/sandboxes/jest/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/jest/XMLFormatter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain