001/* 002 * (C) Copyright 2006-2013 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020 021package org.nuxeo.ecm.platform.importer.xml.parser; 022 023import java.text.DateFormat; 024import java.text.ParseException; 025import java.text.SimpleDateFormat; 026import java.util.ArrayList; 027import java.util.Calendar; 028import java.util.Date; 029import java.util.List; 030import java.util.Map; 031import java.util.Stack; 032 033import org.dom4j.Element; 034import org.nuxeo.ecm.automation.core.scripting.CoreFunctions; 035import org.nuxeo.ecm.core.api.CoreSession; 036import org.nuxeo.ecm.core.api.DocumentModel; 037import org.nuxeo.ecm.core.api.DocumentNotFoundException; 038 039/** 040 * Some helper function that are injected inside MVEL context 041 * 042 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 043 */ 044public class MVELImporterFunction extends CoreFunctions { 045 046 protected final CoreSession session; 047 048 protected final Stack<DocumentModel> docsStack; 049 050 protected final Map<Element, DocumentModel> elToDoc; 051 052 protected final Element el; 053 054 public MVELImporterFunction(CoreSession session, Stack<DocumentModel> docsStack, 055 Map<Element, DocumentModel> elToDoc, Element el) { 056 super(); 057 this.session = session; 058 this.docsStack = docsStack; 059 this.elToDoc = elToDoc; 060 this.el = el; 061 } 062 063 public Calendar parseDate(String source, String format) throws ParseException { 064 DateFormat df = new SimpleDateFormat(format); 065 Date date = df.parse(source); 066 Calendar result = Calendar.getInstance(); 067 result.setTime(date); 068 return result; 069 } 070 071 public DocumentModel mkdir(DocumentModel parent, String regexp, String data, String typeName) 072 { 073 074 String[] parts = data.split(regexp); 075 List<DocumentModel> result = new ArrayList<DocumentModel>(); 076 DocumentModel root = parent; 077 078 for (String part : parts) { 079 DocumentModel child = null; 080 try { 081 child = session.getChild(root.getRef(), part); 082 } catch (DocumentNotFoundException e) { 083 child = session.createDocumentModel(root.getPathAsString(), part, typeName); 084 child.setPropertyValue("dc:title", part); 085 child = session.createDocument(child); 086 } 087 result.add(child); 088 docsStack.push(child); 089 root = child; 090 } 091 092 if (result.size() > 0) { 093 elToDoc.put(el, result.get(result.size() - 1)); 094 return result.get(result.size() - 1); 095 } 096 return null; 097 } 098 099}