1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package org.apache.openoffice.ooxml.framework.part; 23 24 import java.util.HashMap; 25 import java.util.Map; 26 27 import javax.xml.stream.Location; 28 29 import org.apache.openoffice.ooxml.framework.part.parser.ParserFactory; 30 import org.apache.openoffice.ooxml.parser.ElementContext; 31 import org.apache.openoffice.ooxml.parser.Parser; 32 import org.apache.openoffice.ooxml.parser.action.ActionTrigger; 33 import org.apache.openoffice.ooxml.parser.action.IAction; 34 35 public class ContentTypes 36 { ContentTypes(final PartManager aPartManager)37 ContentTypes (final PartManager aPartManager) 38 { 39 maExtensionToContentTypeMap = new HashMap<>(); 40 maPartNameToContentTypeMap = new HashMap<>(); 41 42 // Technically the [Content_Types].xml stream is not a part and 43 // "[Content_Types].xml" is not a part name. But handling it like one 44 // makes the implementation a little bit easier and more readable. 45 final Part aContentTypesPart = new Part( 46 ContentType.ContentTypes, 47 aPartManager, 48 new PartName("/[Content_Types].xml")); 49 50 final Parser aParser = ParserFactory.getParser( 51 aContentTypesPart.getContentType(), 52 aContentTypesPart.getStream(), 53 null); 54 /* 55 DefineContext( 56 CT_Something, 57 int nValue, 58 CallbackObject aObject); 59 60 class CT_Something_Context : public Context 61 { 62 Context parent 63 64 attribute 1 65 ... 66 attribute n 67 int nValue; 68 CallbackObject aObject; 69 } 70 71 DefineElementStartAction( 72 CT_Something_Context, 73 aObject, 74 DoSomething); 75 76 77 case ElementStart of CT_Something: 78 maCurrentContext.aObject.DoSomething(maCurrentContext); // CT_Something_Context 79 80 // 81 CallbackObject.cxx 82 83 class CallbackObject 84 { 85 public: DoSomething(CT_Something_Context aContext) 86 { 87 aContext.attribute1 88 } 89 } 90 91 */ 92 aParser.GetActionManager().AddElementStartAction( 93 ".*_CT_Default", 94 new IAction(){ 95 96 @Override 97 public void Run( 98 final ActionTrigger eTrigger, 99 final ElementContext aContext, 100 final String sText, 101 final Location aStartLocation, 102 final Location aEndLocation) 103 { 104 ProcessDefault( 105 aContext.GetAttributes().GetRawAttributeValue("A_Extension"), 106 aContext.GetAttributes().GetRawAttributeValue("A_ContentType")); 107 108 }}); 109 aParser.GetActionManager().AddElementStartAction( 110 ".*_CT_Override", 111 new IAction(){ 112 113 @Override 114 public void Run( 115 final ActionTrigger eTrigger, 116 final ElementContext aContext, 117 final String sText, 118 final Location aStartLocation, 119 final Location aEndLocation) 120 { 121 ProcessOverride( 122 aContext.GetAttributes().GetRawAttributeValue("A_PartName"), 123 aContext.GetAttributes().GetRawAttributeValue("A_ContentType")); 124 125 }}); 126 127 128 aParser.Parse(); 129 } 130 131 132 133 getTypeForPartName(final PartName aName)134 public ContentType getTypeForPartName (final PartName aName) 135 { 136 ContentType eType = maPartNameToContentTypeMap.get(aName.GetFullname()); 137 if (eType == null) 138 eType = maExtensionToContentTypeMap.get(aName.GetExtension()); 139 if (eType == null) 140 eType = ContentType.Unknown; 141 return eType; 142 } 143 144 145 146 ProcessDefault( final String sExtension, final String sContentTypeName)147 private void ProcessDefault ( 148 final String sExtension, 149 final String sContentTypeName) 150 { 151 final ContentType eType = ContentType.CreateForString(sContentTypeName); 152 maExtensionToContentTypeMap.put(sExtension, eType); 153 } 154 155 156 157 ProcessOverride( final String sPartName, final String sContentTypeName)158 private void ProcessOverride ( 159 final String sPartName, 160 final String sContentTypeName) 161 { 162 final ContentType eType = ContentType.CreateForString(sContentTypeName); 163 maPartNameToContentTypeMap.put(sPartName, eType); 164 } 165 166 167 168 169 private final Map<String,ContentType> maExtensionToContentTypeMap; 170 private final Map<String,ContentType> maPartNameToContentTypeMap; 171 } 172