diff options
Diffstat (limited to 'ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action')
5 files changed, 481 insertions, 0 deletions
diff --git a/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionDescriptor.java b/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionDescriptor.java new file mode 100644 index 000000000000..27ce9d4b8f90 --- /dev/null +++ b/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionDescriptor.java @@ -0,0 +1,114 @@ +/************************************************************** +* +* 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.openoffice.ooxml.parser.action; + +import java.util.Vector; + +/** Container of all actions that are associated with a single state. + */ +public class ActionDescriptor +{ + public ActionDescriptor ( + final int nStateId, + final String sName) + { + msStateName = sName; + + maElementStartActions = null; + maElementEndActions = null; + maTextActions = null; + } + + + + + public void AddAction ( + final IAction aAction, + final ActionTrigger eTrigger) + { + GetActionsForTrigger(eTrigger, true).add(aAction); + } + + + + + public Iterable<IAction> GetActions ( + final ActionTrigger eTrigger) + { + return GetActionsForTrigger(eTrigger, false); + } + + + + + @Override + public String toString () + { + return "actions for state "+msStateName; + } + + + + + private Vector<IAction> GetActionsForTrigger ( + final ActionTrigger eTrigger, + final boolean bCreateWhenMissing) + { + Vector<IAction> aActions = null; + switch(eTrigger) + { + case ElementStart: + aActions = maElementStartActions; + if (bCreateWhenMissing && aActions==null) + { + aActions = new Vector<>(); + maElementStartActions = aActions; + } + break; + case ElementEnd: + aActions = maElementEndActions; + if (bCreateWhenMissing && aActions==null) + { + aActions = new Vector<>(); + maElementEndActions = aActions; + } + break; + case Text: + aActions = maTextActions; + if (bCreateWhenMissing && aActions==null) + { + aActions = new Vector<>(); + maTextActions = aActions; + } + break; + } + return aActions; + } + + + + + private final String msStateName; + private Vector<IAction> maElementStartActions; + private Vector<IAction> maElementEndActions; + private Vector<IAction> maTextActions; +} diff --git a/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionIterator.java b/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionIterator.java new file mode 100644 index 000000000000..0ca176ad51bb --- /dev/null +++ b/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionIterator.java @@ -0,0 +1,118 @@ +/************************************************************** +* +* 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.openoffice.ooxml.parser.action; + +import java.util.Iterator; + +/** Iterate over two sources of actions, both given as an Iterable<IAction> + * object that can be null. +*/ +public class ActionIterator implements Iterable<IAction> +{ + public ActionIterator ( + final Iterable<IAction> aOneStateActions, + final Iterable<IAction> aAllStateActions) + { + maOneStateActions = aOneStateActions; + maAllStateActions = aAllStateActions; + } + + + + + @Override public Iterator<IAction> iterator() + { + return new Iterator<IAction>() + { + Iterator<IAction> maIterator = null; + int mnPhase = 0; + + @Override + public boolean hasNext() + { + while(true) + { + if (mnPhase == 2) + return false; + else if (mnPhase == 0) + { + if (maIterator == null) + if (maOneStateActions == null) + { + mnPhase = 1; + continue; + } + else + maIterator = maOneStateActions.iterator(); + if (maIterator.hasNext()) + return true; + else + { + maIterator = null; + mnPhase = 1; + } + } + else if (mnPhase == 1) + { + if (maIterator == null) + if (maAllStateActions == null) + { + mnPhase = 2; + return false; + } + else + maIterator = maAllStateActions.iterator(); + if (maIterator.hasNext()) + return true; + else + { + mnPhase = 2; + } + } + } + } + + + + + @Override + public IAction next() + { + return maIterator.next(); + } + + + + + @Override + public void remove() + { + } + }; + } + + + + + private final Iterable<IAction> maOneStateActions; + private final Iterable<IAction> maAllStateActions; +} diff --git a/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionManager.java b/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionManager.java new file mode 100644 index 000000000000..48d78a03977b --- /dev/null +++ b/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionManager.java @@ -0,0 +1,165 @@ +/************************************************************** +* +* 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.openoffice.ooxml.parser.action; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.openoffice.ooxml.parser.NameMap; + +/** Manage actions that are bound to states and XML events. + */ +public class ActionManager +{ + public ActionManager ( + final NameMap aStateNameToIdMap) + { + maStateNameToIdMap = aStateNameToIdMap; + maAllStatesActions = new ActionDescriptor(0,"*"); + maStateToActionsMap = new HashMap<>(); + } + + + + + /** Add an action for an element start. + * @param sStateSelector + * The element is specified via a state name. This allows one element + * that leads to different complex types to have different actions, + * depending on the complex type. + * The selector value can be a full state name (including the namespace + * prefix and CT prefix, e.g. w06_CT_Table) or a regular expression + * (e.g. .*_CT_Table to match w06_CT_Table and w12_CT_Table). + * The action is bound to all matching states. + * @param aAction + * The action to call on entering any of the states that match the + * selector. + */ + public void AddElementStartAction ( + final String sStateSelector, + final IAction aAction) + { + AddAction(sStateSelector, aAction, ActionTrigger.ElementStart); + } + + + + + /** Add an action for an element end. + * @see AddElementStartAction. + */ + public void AddElementEndAction ( + final String sStateSelector, + final IAction aAction) + { + AddAction(sStateSelector, aAction, ActionTrigger.ElementEnd); + } + + + + + /** Add an action for XML text events. + * @see AddElementStartAction. + */ + public void AddTextAction ( + final String sStateSelector, + final IAction aAction) + { + AddAction(sStateSelector, aAction, ActionTrigger.Text); + } + + + + + /** Return an iterable object that gives access to all actions + * bound to the given state and trigger. + * Return value can be null when there are no actions bound to the state + * and trigger. + */ + public Iterable<IAction> GetActions ( + final int nStateId, + final ActionTrigger eTrigger) + { + final ActionDescriptor aOneStateActionsDescriptor = maStateToActionsMap.get(nStateId); + final Iterable<IAction> aOneStateActions = aOneStateActionsDescriptor!=null + ? aOneStateActionsDescriptor.GetActions(eTrigger) + : null; + final Iterable<IAction> aAllStateActions = maAllStatesActions.GetActions(eTrigger); + + if (aOneStateActions == null) + return aAllStateActions; + else if (aAllStateActions == null) + return aOneStateActions; + else + return new ActionIterator(aOneStateActions, aAllStateActions); + } + + + + + private void AddAction ( + final String sStateSelector, + final IAction aAction, + final ActionTrigger eTrigger) + { + if (sStateSelector.equals("*")) + { + // Simple optimization when an action is defined for all states. + maAllStatesActions.AddAction(aAction, eTrigger); + } + else if (sStateSelector.contains("*") || sStateSelector.contains("?")) + { + // The state selector contains wildcards. We have to iterate over + // all state names to find the matching ones. + for (final int nStateId : maStateNameToIdMap.GetMatchingStateIds(sStateSelector)) + { + GetActionDescriptor(nStateId).AddAction(aAction, eTrigger); + } + } + else + { + final int nStateId = maStateNameToIdMap.GetIdForName(sStateSelector); + GetActionDescriptor(nStateId).AddAction(aAction, eTrigger); + } + } + + + + + private ActionDescriptor GetActionDescriptor (final int nStateId) + { + ActionDescriptor aDescriptor = maStateToActionsMap.get(nStateId); + if (aDescriptor == null) + { + aDescriptor = new ActionDescriptor(nStateId, maStateNameToIdMap.GetNameForId(nStateId)); + maStateToActionsMap.put(nStateId, aDescriptor); + } + return aDescriptor; + } + + + + + private final NameMap maStateNameToIdMap; + private final ActionDescriptor maAllStatesActions; + private final Map<Integer,ActionDescriptor> maStateToActionsMap; +} diff --git a/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionTrigger.java b/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionTrigger.java new file mode 100644 index 000000000000..33b781a0b1a6 --- /dev/null +++ b/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/ActionTrigger.java @@ -0,0 +1,31 @@ +/************************************************************** +* +* 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.openoffice.ooxml.parser.action; + +/** An enumeration of all supported action triggers. + */ +public enum ActionTrigger +{ + ElementStart, + ElementEnd, + Text +}
\ No newline at end of file diff --git a/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/IAction.java b/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/IAction.java new file mode 100644 index 000000000000..e784ed7c5537 --- /dev/null +++ b/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/action/IAction.java @@ -0,0 +1,53 @@ +/************************************************************** +* +* 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.openoffice.ooxml.parser.action; + +import javax.xml.stream.Location; + +import org.apache.openoffice.ooxml.parser.ElementContext; + +/** Interface for actions that are bound to states and triggered by XML events. + */ +public interface IAction +{ + /** Callback for a single XML event. + * @param eTrigger + * Equivalent to the XML event type. + * @param aContext + * The context of the element that was just entered (element start), + * is about to be left (element end) or is currently active (all other + * events). + * @param sText + * Contains text for ActionTrigger.Text. Is null for all other + * triggers. + * @param aStartLocation + * The location in the source file where the triggering element starts. + * @param aEndLocation + * The location in the source file where the triggering element ends. + */ + void Run ( + final ActionTrigger eTrigger, + final ElementContext aContext, + final String sText, + final Location aStartLocation, + final Location aEndLocation); +} |